Questions tagged [css-position]

The "position" property in CSS allows you to control the location of an element on the page by setting its value to static (the default setting), relative, absolute, fixed, or sticky.

The position property in CSS allows you to control the location of an element. Next to the inherit value, there are four specific values it can be set to:

  • static for positioning an element according to the "normal flow". This is the default and does not need to be set.

  • relative for positioning similar to static, albeit with a specific offset relative to its normal position

  • absolute for positioning elements along a coordinate system with respect to the element's "containing block"

  • fixed for positioning elements using a coordinate system that is fixed to the origin of the display surface (usually the origin of the client area of the enclosing browser window)

  • sticky for positioning an element as relative until a certain scrolling threshold has been reached, at which point the element is positioned as a fixed element.

CSS positioning often relies on additional layout properties (such as top, bottom, left, right, z-index, and clip) in order to achieve the desired outcome.

Example

/* static */
element {
  position: static;
}

/* relative */
element {
  position: relative;
  top: 65px; left: 65px;
}

/* absolute */
element {
  position: absolute;
  top: 40px; left: 40px;
}

/* sticky */
element {
  position: sticky;
  top: 20px;
}

References

  1. Positioning Schemes - W3C Spec
  2. CSS Position - MDN Link
6247 questions
68
votes
5 answers

iOS 9 Safari: changing an element to fixed position while scrolling won't paint until scroll stops

I've been developing a site and taking advantage from the rather good jQuery Sticky Kit plugin. It operates by switching the position property to fixed and back when appropriate. Runs very smoothly in desktop and acceptably so in mobile. Or at least…
instanceofnull
  • 1,071
  • 1
  • 7
  • 15
68
votes
10 answers

Is it possible to keep the width of the parent element when position: fixed is applied?

When we apply position:fixed to an element, it's taken out of the normal flow of the document, therefore it doesn't respect it's parent's element width. Are there ways to make it inherit it's parent's width if this is declared as a percentage ?…
George Katsanos
  • 13,524
  • 16
  • 62
  • 98
67
votes
7 answers

Position by center point, rather than top-left point

Is it possible to tell the code to position by the center point of an element, rather than by the top-left point? If my parent element has width: 500px; and my child element has /*some width, for this example let's say it's 200px*/ position:…
kaboom1
  • 809
  • 2
  • 8
  • 9
65
votes
12 answers

Set Google Maps Container DIV width and height 100%

I loaded Google Maps API v3 and print Google Map in div. But when set width & height to 100% and auto I can't see the Map. Here is HTML code snippet.
BBKing
  • 2,279
  • 8
  • 38
  • 44
61
votes
8 answers

How to place a div on the right side with absolute position

I've a page where a dynamic message box has to be displayed without disturbing the actual page. This message box has to appear at the top right corner of the page overlapping the existing contents. I've tried to use position: absolute but then I'm…
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
59
votes
4 answers

Placing absolute behind relative positioned element

I have two elements in the same container, the first has position: absolute, the second position: relative. Is there a way to set the "z-index" of an absolute element so that its in the background? The relative positioned element should be on the…
Luca Nate Mahler
  • 1,292
  • 2
  • 13
  • 28
58
votes
3 answers

pure CSS multiple stacked position sticky?

Is it possible to have multiple sticky elements stacked on top of each other in pure CSS? The desired behavior can be seen here: https://webthemez.com/demo/sticky-multi-header-scroll/index.html Only I'd prefer to use pure CSS, instead of a…
Ro Achterberg
  • 2,504
  • 2
  • 17
  • 17
57
votes
4 answers

Absolute positioning in gmail emails

I have a client who wants to send gift certificates to people who sign up on their site. They want it all designed out, so I can't just send a text email. I'm trying to position text over the image so that it can still be dynamic. I'm trying to do…
hookedonwinter
  • 12,436
  • 19
  • 61
  • 74
57
votes
10 answers

How to position a Bootstrap popover?

I'm trying to position a Bootstrap popover for a trigger that sits on the far top-right corner of a 960px wide web page. Ideally, I'd position it on the bottom and move the arrow with CSS (so the arrow is on the top-right of the popover).…
koichirose
  • 1,815
  • 4
  • 22
  • 30
52
votes
11 answers

body { overflow-x: hidden; } breaks position: sticky

I have an element that I am making sticky with position sticky: #header { position: sticky; width: 100vw; top: 0; } And that works fine, but I realised that if I use: body { overflow-x:…
Leff
  • 1,968
  • 24
  • 97
  • 201
52
votes
8 answers

Div with scrollbar inside div with position:fixed

I have a div with position:fixed that is my container div for some menus. I've set it to top:0px, bottom:0px to always fill the viewport. Inside that div I want to have 2 other divs, the lower one of which contains lots of lines and has…
Einar Egilsson
  • 3,438
  • 9
  • 36
  • 47
51
votes
9 answers

Sticky top div with absolute positioning

I'm using absolute positioning to have a div fill up the entire browser window. However, I wan't to combine this with a sticky div that sometimes is there and sometimes not. To make things a little clearer, check out this jsFiddle:…
Henrik Janbell
  • 1,784
  • 3
  • 21
  • 30
49
votes
4 answers

Why can't an element with a z-index value cover its child?

Today after hours of debugging, I learned in the hard way that: A parent element is never able to cover (stack on top of) its child element if the parent has a z-index of any value, no matter how you change the child's CSS How can I understand this…
ZYinMD
  • 3,602
  • 1
  • 23
  • 32
48
votes
4 answers

how to use z-index with relative positioning?

I have a problem with z-index and my code. I want to have a popup on every row, positioned relative to that row. So I created this code: .level1 { position:relative; z-index:2; } .level2 { …
Chris Vaarhorst
  • 497
  • 1
  • 4
  • 4
48
votes
4 answers

CSS absolute centering

Recently i've came across this method used to position an element both horizontally and vertically to the center. However, I wasn't able to figure out what each of the property is doing. Would someone be able to explain to me what is the effect upon…
Xenon Thong
  • 435
  • 3
  • 6