#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
CSS3 Fundamentals

CSS Position

The position property controls whether an element stays in the normal flow of the page or is placed at specific coordinates. Combined with the offset properties top, right, bottom and left, it lets you build things like sticky headers, notification badges on icons, dropdown menus and floating chat buttons.

The five values

  • static is the default. The element follows normal flow and the offset properties are ignored.
  • relative keeps the element's original space in the flow but lets you nudge it visually with offsets. Its main job, though, is to act as an anchor for absolutely positioned children.
  • absolute takes the element out of the flow, so neighbours move up as if it were not there. Offsets are measured from the nearest ancestor that has a position other than static.
  • fixed is like absolute, but measured from the browser viewport. The element stays put while the page scrolls.
  • sticky behaves like relative until scrolling reaches the offset you set, then it sticks like fixed, but only within its parent.

Placing a badge on a button

The pairing of a relative parent with an absolute child is the most useful pattern to learn. The parent sets the reference box, and the child is placed precisely within it.

.icon-button {
  position: relative;
  width: 44px;
  height: 44px;
}

.icon-button .badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 20px;
  padding: 2px 6px;
  border-radius: 10px;
  background: crimson;
  color: white;
  font-size: 12px;
}

.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: white;
}

How stacking order works

Positioned elements can overlap, and z-index decides which one sits on top. A higher number appears in front, but only when compared with elements in the same stacking context. A parent with its own z-index, an opacity below 1 or a transform creates a new context, and its children can never rise above an element outside that context, no matter how large their number. This explains many "my z-index of 9999 does not work" questions.

Why things end up in the wrong place

When an absolutely positioned element flies to the corner of the page, it almost always means no ancestor has a position set. The browser falls back to the initial containing block, which is roughly the page itself. Add position: relative to the intended parent.

Sticky elements have their own trap. If any ancestor has overflow: hidden or overflow: auto, the sticky element sticks to that ancestor's scrolling box instead of the page, which often looks like it does not stick at all. Sticky also needs at least one offset, typically top: 0.

Finally, avoid using absolute positioning for whole page layouts. Content of unknown length will overlap other elements. Flexbox and grid handle those layouts far better.

For centring an absolute child inside its parent, set inset: 0 and margin: auto with a fixed size, or use top: 50%, left: 50% and transform: translate(-50%, -50%).

A quick way to choose

Use relative to create an anchor, absolute for small overlays inside it, fixed for elements tied to the screen, and sticky for headers or sidebars that should follow the reader through a section.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course