- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Position
The position property is one of the most fundamental tools in a CSS developer's toolkit. It dictates exactly how an element sits on the page and how it interacts with the elements around it. By default, elements follow the "Normal Document Flow" stacking one after another like blocks. The position property allows you to break that flow to create overlays, navigation bars that stay put, or elements that "stick" as you scroll.
position, consider if Flexbox or CSS Grid can achieve your layout. Use position specifically when you need elements to overlap or move independently of the document flow.
Values of position
static:
- This is the default value for every HTML element. Elements are rendered in the order they appear in the HTML. In this state, the element is "unpositioned," meaning properties like top, right, bottom, left, and z-index will have no effect whatsoever.
static element using top: 10px; and wonder why it isn't moving. For these offsets to work, you must change the position value to something else, like relative.
relative:
- A relative element stays in the normal document flow, but you can "nudge" it using offset properties. Crucially, even if you move it, the original space the element occupied remains reserved in the layout it leaves a "ghost" behind that prevents other elements from shifting into its spot.
position: relative; on a parent container when you want to place absolute children inside it. This acts as an "anchor" for the children.
absolute:
- The element is pulled out of the normal document flow, meaning other elements will act as if it doesn't exist. It is positioned relative to its nearest positioned ancestor (the first parent it finds that isn't
static). If it can't find one, it maps itself to the entire document body.
fixed:
- Like absolute, a fixed element is removed from the document flow. However, it is positioned relative to the viewport (the browser window). This means it stays in the exact same spot even when the user scrolls the page.
sticky:
- A hybrid between
relativeandfixed. The element acts likerelativeuntil the user scrolls past a specific threshold (e.g.,top: 0;), at which point it "sticks" to the screen like afixedelement. It remains stuck until it reaches the bottom of its parent container.
position: sticky; will not work if any of its parent containers have overflow: hidden;, overflow: auto;, or overflow: scroll; set.
Examples and Use Cases
static
Since this is the default, you rarely need to declare it unless you are overriding a position value previously set in a media query or a different CSS rule.
<div class="static-element">I follow the natural flow of the page.</div>
<style>
.static-element {
position: static;
/* top: 20px; This would do nothing here */
}
</style>
relative
Use this for minor adjustments or to create a coordinate system for child elements.
<div class="relative-element">I am shifted 10px down from my original spot.</div>
<style>
.relative-element {
position: relative;
top: 10px;
left: 20px;
border: 1px solid blue;
}
</style>
absolute
A classic use case is placing an "Update" badge or an icon in the corner of a card component.
<div class="card">
<span class="badge">New!</span>
<p>Product Description here...</p>
</div>
<style>
.card {
position: relative; /* Anchor for the badge */
width: 200px;
height: 100px;
border: 1px solid #ccc;
}
.badge {
position: absolute;
top: -10px;
right: -10px;
background: red;
color: white;
padding: 5px;
}
</style>
fixed
Perfect for navigation bars or "Back to Top" buttons that must always be accessible.
<nav class="navbar">Main Menu</nav>
<style>
.navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 50px;
background: #333;
color: #fff;
z-index: 1000; /* Ensure it stays above content */
}
</style>
sticky
Commonly used for table headers or category headings in long lists.
<section>
<h2 class="sticky-header">Category: Fruits</h2>
<ul>
<li>Apple</li>
<li>Banana</li>
<!-- imagine many more items here -->
</ul>
</section>
<style>
.sticky-header {
position: sticky;
top: 0;
background: #f4f4f4;
padding: 10px;
}
</style>
Additional Notes
- z-index: When elements overlap,
z-indexdetermines which one is on top. An element withz-index: 10;will appear in front of an element withz-index: 5;. Remember: z-index only works on positioned elements (relative, absolute, fixed, or sticky). - The Stacking Context: Using
positioncan create a new "stacking context." This means a child element'sz-indexis relative to its parent. Even if a child hasz-index: 9999, it will still stay behind a sibling of the parent if the parent has a lowerz-index.
z-index values low and organized. Avoid using z-index: 999999; as it becomes a nightmare to manage as your project grows. Try increments of 10 (10, 20, 30) to leave room for future elements.
Mastering CSS position is the difference between a website that looks "okay" and a professional UI that feels interactive and polished. By understanding how these values interact with the document flow, you can build everything from complex modals to interactive dashboards with confidence.