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
94
votes
6 answers

Easiest CSS for "red notification badge" with count

I need to show the popular red notification indicator with count such as the one shown below. Getting something that looks nice cross browser seems tricky. For example, different browsers seem to treat paddings differently, resulting in weird…
meow
  • 27,476
  • 33
  • 116
  • 177
92
votes
7 answers

Fixed sidebar navigation in fluid twitter bootstrap 2.0

Is it possible to make sidebar navigation stay always fixed on scroll in fluid layout?
sl_bug
  • 5,066
  • 5
  • 21
  • 22
88
votes
2 answers

Position one element relative to another in CSS

I want to position four divs relative to another. I have a rectangle div, and I want to insert 4 divs at its corners. I know that CSS has an attribute "position:relative", but this is relative to the normal position of that element. I want to…
Babak Mehrabi
  • 2,155
  • 4
  • 23
  • 24
85
votes
6 answers

How to make div fixed after you scroll to that div?

How to make a div remain fixed after you scroll to that div? I have a div that is later in a page and you need to scroll to get to that div. If I use: .divname { position: fixed; } The div will appear before it should appear normally. Maybe a…
Jakov Mikić
  • 929
  • 1
  • 8
  • 7
85
votes
14 answers

Browsers scrollbar is under my fixed div

i have a fixed header with 100% width. #header { background: #2e2e2e; width: 100%; z-index: 999; position: fixed; } browsers scrollbar is under my fixed div. How to fix that?
Grufas
  • 1,350
  • 4
  • 21
  • 33
83
votes
4 answers

Why does applying a CSS-Filter on the parent break the child positioning?

So I have this title-screen "animation" that has the title centered on a fullscreen page and when you scroll down it becomes smaller and remains at the top of the page. Here is a working example with the expected behavior, from which I stripped all…
leonheess
  • 16,068
  • 14
  • 77
  • 112
81
votes
11 answers

How to stick table header(thead) on top while scrolling down the table rows with fixed header(navbar) in bootstrap 3?

Bootstrap layout with fixed-navbar. Having table with so many rows in body. Issue? As i scroll the page navigation-bar will be there because it is fixed. as i scroll more i want table header to be fixed under navigation-bar and the content of…
Suresh Karia
  • 17,550
  • 18
  • 67
  • 85
78
votes
4 answers

CSS position absolute and width of parent container in percent

I'm trying to build a HTML/CSS dropdown menu which is flexible in width. Due to the position:absolute for the second level of the navigation, I don't get the width of the first level. Removing the position:absolute will move all following elements…
chris
  • 2,109
  • 2
  • 23
  • 33
76
votes
2 answers

How to make div's percentage width relative to parent div and not viewport

Here is the HTML I am working with.
sean
  • 2,560
  • 3
  • 18
  • 21
75
votes
9 answers

CSS z-index not working (position absolute)

I am trying to make the black div (relative) above the second yellow one (absolute). The black div's parent has a position absolute, too. #relative { position: relative; width: 40px; height: 100px; background: #000; z-index: 1; …
HTMHell
  • 5,761
  • 5
  • 37
  • 79
71
votes
6 answers

absolute positioned anchor tag (with no text) not clickable in IE

I have two anchors positioned absolute on top of an image, the links are clickable in other browsers (Chrome, FF, Safari) but not in IE (tested in 8 & 9 so far) The strange thing is if I give the links a background-color they are clickable, however…
MikeM
  • 27,227
  • 4
  • 64
  • 80
70
votes
7 answers

CSS shorthand for positioning

There are any shorthand for top right bottom left or for width and height ? I have a lot of css like this #topDiv { position:absolute; top:0px; left:0px; right:0px; height:100px; } #centerDiv { position:absolute; …
Vitim.us
  • 20,746
  • 15
  • 92
  • 109
69
votes
6 answers

Position fixed on chrome mobile causing element to move on scroll up/down

I'm facing this strange issue that looks like a bug on Chrome mobile. I have a div with position:fixed; aligned to the top right corner of the screen. On desktop, it works fine (it stays in place), in mobile, however, the div is moving when I scroll…
Lucas Bustamante
  • 15,821
  • 7
  • 92
  • 86
69
votes
7 answers

Overlay Divs Without Absolute Position

What follows is a long explanation, but it's the only way to effectively communicate the issue I'm trying to resolve... I am (somewhat desperately, and entirely unsuccessfully) trying to overlay divs without the use of absolute positioning. The need…
jivers
  • 910
  • 2
  • 9
  • 16
69
votes
6 answers

What happens when nesting elements with position: fixed inside each other?

Okay, I've noticed something, but couldn't find it in the CSS spec. Styling an element with position: fixed will position it absolutely, with respect to the browser viewport. What happens if you place a fixed-position element inside another? Example…
user146570