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
1506
votes
37 answers

How can I center an absolutely positioned element in a div?

I want to place a div (with position:absolute;) element in the center of the window. But I'm having problems doing so, because the width is unknown. I tried the following CSS code, but it needs to be adjusted because the width is responsive. .center…
Ish
  • 28,486
  • 11
  • 60
  • 77
951
votes
31 answers

How to center a "position: absolute" element

I'm having a problem centering an element that has the attribute position set to absolute. Does anyone know why the images are not centered? body { text-align: center; } #slideshowWrapper { margin-top: 50px; text-align:…
user1098278
  • 9,527
  • 3
  • 14
  • 3
815
votes
31 answers

Fixed position but relative to container

I am trying to fix a div so it always sticks to the top of the screen, using: position: fixed; top: 0px; right: 0px; However, the div is inside a centered container. When I use position:fixed it fixes the div relative to the browser window, such as…
Zach Nicodemous
  • 9,097
  • 9
  • 45
  • 68
591
votes
24 answers

Center a position:fixed element

I would like to make a position: fixed; popup box centered to the screen with a dynamic width and height. I used margin: 5% auto; for this. Without position: fixed; it centers fine horizontally, but not vertically. After adding position: fixed;,…
saturngod
  • 24,649
  • 17
  • 62
  • 87
413
votes
37 answers

How does the "position: sticky;" property work?

I want to make the navigation bar stick to the top of the viewport once a user scrolls the page, but it's not working and I have no clue why. If you can please help, here is my HTML and CSS code: .container { min-height: 300vh; } .nav-selections…
Harleyoc1
  • 4,141
  • 2
  • 9
  • 10
330
votes
12 answers

Make div stay at bottom of page's content all the time even when there are scrollbars

I am looking to implement the opposite behaviour to the following question: CSS Push Div to bottom of page. I.e., when content overflows to the scrollbars, I would like the footer to be at the bottom of the page, like Stack Overflow. I have a div…
H Bellamy
  • 22,405
  • 23
  • 76
  • 114
254
votes
6 answers

Relatively position an element without it taking up space in document flow

How can I relatively position an element, and have it not take up space in the document flow?
Web_Designer
  • 72,308
  • 93
  • 206
  • 262
254
votes
9 answers

My position: sticky element isn't sticky when using flexbox

I was stuck on this for a little bit and thought I'd share this position: sticky + flexbox gotcha: My sticky div was working fine until I switched my view to a flex box container, and suddenly the div wasn't sticky when it was wrapped in a flexbox…
bholtbholt
  • 11,281
  • 6
  • 22
  • 32
252
votes
9 answers

Absolute positioning ignoring padding of parent

How do you make an absolute positioned element honor the padding of its parent? I want an inner div to stretch across the width of its parent and to be positioned at the bottom of that parent, basically a footer. But the child has to honor the…
d512
  • 32,267
  • 28
  • 81
  • 107
223
votes
5 answers

Have a fixed position div that needs to scroll if content overflows

I have actually two issues, but lets resolve the primary issue first as I believe the other is easier to address. I have a fixed position div on the left side of the scroll for a menu. Right side is a standard div that scrolls properly. The issue is…
TCD Factory
  • 2,344
  • 2
  • 15
  • 12
194
votes
6 answers

Targeting position:sticky elements that are currently in a 'stuck' state

position: sticky works on some mobile browsers now, so you can make a menu bar scroll with the page but then stick to the top of the viewport whenever the user scrolls past it. But what if you want to restyle your sticky menu bar slightly whenever…
callum
  • 34,206
  • 35
  • 106
  • 163
180
votes
6 answers

Position absolute and overflow hidden

We have two DIVs, one embedded in the other. If the outer DIV is not positioned absolute then the inner DIV, which is positioned absolute, does not obey the overflow hidden of the outer DIV. #first { width: 200px; height: 200px; …
medihack
  • 16,045
  • 21
  • 90
  • 134
175
votes
21 answers

Position fixed doesn't work when using -webkit-transform

I am using -webkit-transform (and -moz-transform / -o-transform) to rotate a div. Also have position fixed added so the div scrolls down with the user. In Firefox it works fine, but in webkit based browsers it's broken. After using the…
iSenne
  • 2,656
  • 5
  • 26
  • 26
170
votes
16 answers

Center aligning a fixed position div

I'm trying to get a div that has position:fixed center aligned on my page. I've always been able to do it with absolutely positioned divs using this "hack" left: 50%; width: 400px; margin-left: -200px; ...where the value for margin-left is half…
Kyle
  • 10,839
  • 17
  • 53
  • 63
162
votes
7 answers

In jQuery how can I set "top,left" properties of an element with position values relative to the parent and not the document?

.offset([coordinates]) method set the coordinates of an element but only relative to the document. Then how can I set coordinates of an element but relative to the parent? I found that .position() method get only "top,left" values relative to the…
Max
  • 4,965
  • 17
  • 49
  • 64
1
2 3
99 100