html
/

CSS Position

Last Sync: Today

On this page

12
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

html

CSS Position

What is CSS Position?

The position property controls how an element is positioned in the document using top, right, bottom, and left values.

Position Types

ValueDescription
staticDefault position
relativeRelative to itself
absoluteRelative to nearest positioned parent
fixedFixed to viewport
stickySwitches between relative and fixed

Static Position

CSSRead-only
1
div {
  position: static;
}

Relative Position

Moves relative to its original position.

CSSRead-only
1
div {
  position: relative;
  top: 10px;
  left: 20px;
}

Absolute Position

Positions element relative to nearest positioned ancestor.

CSSRead-only
1
div {
  position: absolute;
  top: 0;
  right: 0;
}

Fixed Position

Stays fixed relative to viewport.

CSSRead-only
1
div {
  position: fixed;
  bottom: 10px;
  right: 10px;
}

Sticky Position

Acts like relative until a scroll threshold is reached.

CSSRead-only
1
div {
  position: sticky;
  top: 0;
}

Z-Index

Controls stacking order of positioned elements.

CSSRead-only
1
div {
  position: absolute;
  z-index: 10;
}

Positioning Example

CSSRead-only
1
.parent {
  position: relative;
}

.child {
  position: absolute;
  top: 0;
  left: 0;
}

Best Practices

  • Use relative for parent containers
  • Use absolute inside positioned parent
  • Use fixed for UI elements like buttons
  • Use sticky for headers/navigation

Common Mistakes

  • Using absolute without parent positioning
  • Overusing fixed elements
  • Forgetting z-index stacking issues
  • Misunderstanding sticky behavior

Conclusion

CSS positioning is essential for layout control. Understanding each type helps build flexible and precise UI designs.

Try it yourself

.box {
  position: absolute;
  top: 20px;
  left: 20px;
}

Test Your Knowledge

Q1
of 3

Which is default?

A
relative
B
absolute
C
static
D
fixed
Q2
of 3

Which sticks on scroll?

A
fixed
B
absolute
C
sticky
D
relative
Q3
of 3

Which controls layering?

A
display
B
z-index
C
position
D
float

Frequently Asked Questions

What is position relative?

Moves element relative to itself.

What is position absolute?

Positions relative to nearest positioned parent.

What is z-index?

Controls stacking order.

Previous

css display

Next

css float

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.