CSS Position Get Certified
What is CSS Position?
The CSS position property controls how an element is placed on the page. It decides whether an element stays in normal document flow or can be moved using top, right, bottom, and left values. Understanding position is essential for overlays, badges, sticky headers, and floating buttons.
Why Use CSS Position?
Position lets you place elements precisely, create sticky navigation, floating help buttons, image badges, tooltips, and overlays without relying only on margin or float.
Step 1: Using position: static
static is the default position value. Elements appear in the normal document flow, and the top, right, bottom, and left properties have no effect.
What this code does: Shows a blue box using the default static position — it stays in normal document flow.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-box {
position: static;
background: #007BFF;
color: white;
padding: 15px;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-box">Static Position</div>
</body>
</html>
Step 2: Using position: relative
relative moves an element from its original position while keeping its original space reserved in the document.
What this code does: Moves a green box 20px down and 40px right from its normal place using relative positioning.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-box {
position: relative;
top: 20px;
left: 40px;
background: #28a745;
color: white;
padding: 15px;
width: 180px;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="demo-box">Relative Position</div>
</body>
</html>
Step 3: Using position: absolute
An absolutely positioned element is placed relative to its nearest parent that has a position other than static.
What this code does: Places a red child box in the top-right corner of a relatively positioned parent.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-parent {
position: relative;
width: 300px;
height: 180px;
background: #eee;
border: 2px solid #333;
border-radius: 6px;
}
.demo-child {
position: absolute;
top: 20px;
right: 20px;
background: #dc3545;
color: white;
padding: 12px;
border-radius: 4px;
}
</style>
</head>
<body>
<div class="demo-parent">
<div class="demo-child">Absolute</div>
</div>
</body>
</html>
Step 4: Using position: fixed
A fixed element stays in the same place even when the webpage is scrolled. It is commonly used for floating buttons and fixed navigation bars.
What this code does: Pins a blue “Help” button to the bottom-right corner of the viewport so it stays visible while scrolling.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
min-height: 800px;
}
.demo-button {
position: fixed;
bottom: 20px;
right: 20px;
background: #007BFF;
color: white;
padding: 15px;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="demo-button">Help</div>
<p>Scroll the page to see the button remain fixed.</p>
<p>More content below…</p>
<p>Keep scrolling to test the fixed button.</p>
</body>
</html>
Step 5: Using position: sticky
A sticky element behaves like a normal element until the page is scrolled to a specified position, after which it remains fixed.
What this code does: Makes an orange header stick to the top of the viewport after you scroll past it.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.demo-header {
position: sticky;
top: 0;
background: #ff9800;
color: white;
padding: 15px;
}
.demo-content {
height: 700px;
padding: 20px;
}
</style>
</head>
<body>
<div class="demo-header">Sticky Header</div>
<div class="demo-content">Scroll down to see the sticky effect.</div>
</body>
</html>
Complete CSS Position Example
What this code does: Places a red “New” badge in the top-right corner of an image card using relative parent + absolute child.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
padding: 24px;
}
.demo-card {
position: relative;
width: 320px;
margin: auto;
}
.demo-card img {
width: 100%;
display: block;
border-radius: 8px;
}
.demo-badge {
position: absolute;
top: 10px;
right: 10px;
background: red;
color: white;
padding: 8px 12px;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="demo-card">
<img src="https://picsum.photos/320/200" alt="Image">
<div class="demo-badge">New</div>
</div>
</body>
</html>
Advantages of CSS Position
Common Mistakes
Beginners often use position:absolute without creating a positioned parent, forget to set values for top, left, right, or bottom, or confuse fixed and sticky.
Best Practices
- Use static for normal page layout.
- Use relative for small position adjustments.
- Use absolute inside a positioned parent.
- Use fixed for floating buttons and fixed navigation.
- Use sticky for headers and navigation bars.
- Avoid unnecessary absolute positioning.
- Always test positioned elements on different screen sizes.
- Keep layouts responsive by combining Position with Flexbox or Grid when appropriate.
Web Resources on CSS Position
1. MDN Web Docs : position CSS property
2. MDN Web Docs : Positioning
3. Dev.to : CSS, Position Property
4. CSS Tricks.com : position
Click “Run Code” to see the result here.
CSS Syntax Example
The CSS syntax consists of a selector and a declaration block.
h1 { color: blue; font-size: 36px;}Selector: h1 – targets all <h1> elements
Property: color and font-size
Value: blue and 36px
Your CSS Applied Below:
This is an H1 heading with your CSS
This is an H2 heading (not affected)
This paragraph is not affected by the h1 selector.
Welcome to CSS
This page is styled using CSS.
The heading and paragraph use your CSS rules.
- List item with your styles
- Another list item
🧪 Test Your CSS Code
Edit the CSS code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.