Duration: 60 minutes | Difficulty: Intermediate | Prerequisites: Lessons 1-5 completed
By the end of this lesson, you will be able to:
The position property specifies how an element is positioned in the document and which properties (top, right, bottom, left) apply to it.
Elements follow normal document flow. Top, right, bottom, left have no effect.
.static {
position: static; /* Default value */
/* top, right, bottom, left are ignored */
}
Element is positioned relative to its normal position in the document flow.
.relative {
position: relative;
top: 20px; /* Move 20px down from normal position */
left: 30px; /* Move 30px right from normal position */
}
Key Points:
Element is positioned relative to nearest positioned ancestor (or document if none).
.container {
position: relative; /* Positioning context */
}
.absolute {
position: absolute;
top: 0; /* 0px from top of container */
right: 0; /* 0px from right of container */
width: 200px;
height: 100px;
}
Key Points:
Element is positioned relative to viewport (browser window).
.fixed-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 60px;
background: white;
z-index: 100;
}
.back-to-top {
position: fixed;
bottom: 20px;
right: 20px;
width: 50px;
height: 50px;
}
Key Points:
Element toggles between relative and fixed based on scroll position.
.sticky-nav {
position: sticky;
top: 0; /* Distance from top when "stuck" */
background: white;
z-index: 10;
}
.sticky-sidebar {
position: sticky;
top: 20px; /* 20px from top when stuck */
height: fit-content;
}
Key Points:
Controls the stacking order of positioned elements.
.modal-backdrop {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
z-index: 100;
}
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
z-index: 101; /* Higher than backdrop */
}
.tooltip {
position: absolute;
z-index: 1000; /* Very high for tooltips */
}
<header class="site-header">
<nav class="navbar">
<div class="nav-brand">Brand</div>
<ul class="nav-menu">
<li><a href="#home">Home</a></li>
<li><a href="#about">About</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
</nav>
</header>
<main class="main-content">
<section class="hero">
<h1>Welcome</h1>
<!-- More content... -->
</section>
</main>
.site-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: rgba(255, 255, 255, 0.95);
backdrop-filter: blur(10px);
border-bottom: 1px solid #e5e7eb;
z-index: 100;
}
.navbar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1rem 2rem;
max-width: 1200px;
margin: 0 auto;
}
.main-content {
padding-top: 80px; /* Account for fixed header height */
}
.hero {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
<div class="modal-overlay" id="modal">
<div class="modal-content">
<div class="modal-header">
<h2>Modal Title</h2>
<button class="modal-close">×</button>
</div>
<div class="modal-body">
<p>Modal content goes here...</p>
</div>
<div class="modal-footer">
<button class="btn btn-secondary">Cancel</button>
<button class="btn btn-primary">Save</button>
</div>
</div>
</div>
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
}
.modal-overlay.active {
opacity: 1;
visibility: visible;
}
.modal-content {
background: white;
border-radius: 8px;
max-width: 500px;
width: 90%;
max-height: 90vh;
overflow-y: auto;
transform: scale(0.8);
transition: transform 0.3s ease;
}
.modal-overlay.active .modal-content {
transform: scale(1);
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 1.5rem;
border-bottom: 1px solid #e5e7eb;
}
.modal-close {
background: none;
border: none;
font-size: 1.5rem;
cursor: pointer;
color: #6b7280;
}
.modal-body {
padding: 1.5rem;
}
.modal-footer {
display: flex;
justify-content: flex-end;
gap: 1rem;
padding: 1.5rem;
border-top: 1px solid #e5e7eb;
}
<div class="layout">
<aside class="sidebar">
<div class="sidebar-content">
<h3>Table of Contents</h3>
<ul>
<li><a href="#section1">Section 1</a></li>
<li><a href="#section2">Section 2</a></li>
<li><a href="#section3">Section 3</a></li>
</ul>
</div>
</aside>
<main class="content">
<section id="section1">
<h2>Section 1</h2>
<!-- Long content... -->
</section>
</main>
</div>
.layout {
display: flex;
max-width: 1200px;
margin: 0 auto;
gap: 2rem;
padding: 2rem;
}
.sidebar {
flex: 0 0 250px;
}
.sidebar-content {
position: sticky;
top: 2rem;
background: #f9fafb;
padding: 1.5rem;
border-radius: 8px;
border: 1px solid #e5e7eb;
}
.content {
flex: 1;
min-width: 0; /* Allows flex item to shrink */
}
.sidebar ul {
list-style: none;
padding: 0;
}
.sidebar li {
margin-bottom: 0.5rem;
}
.sidebar a {
color: #374151;
text-decoration: none;
padding: 0.5rem;
display: block;
border-radius: 4px;
transition: background-color 0.2s;
}
.sidebar a:hover {
background-color: #e5e7eb;
}
<p>Hover over this <span class="tooltip-trigger">text with tooltip</span> to see the effect.</p>
<div class="tooltip">
This is a helpful tooltip!
<div class="tooltip-arrow"></div>
</div>
.tooltip-trigger {
position: relative;
color: #2563eb;
cursor: help;
border-bottom: 1px dashed #2563eb;
}
.tooltip {
position: absolute;
top: -40px;
left: 50%;
transform: translateX(-50%);
background: #1f2937;
color: white;
padding: 0.5rem 0.75rem;
border-radius: 4px;
font-size: 0.875rem;
white-space: nowrap;
opacity: 0;
visibility: hidden;
transition: all 0.2s ease;
z-index: 1000;
}
.tooltip-trigger:hover + .tooltip {
opacity: 1;
visibility: visible;
}
.tooltip-arrow {
position: absolute;
top: 100%;
left: 50%;
transform: translateX(-50%);
border: 5px solid transparent;
border-top-color: #1f2937;
}
In Lesson 7, weโll explore Responsive Design, learning how to create layouts that work beautifully on all devices using media queries, flexible grids, and mobile-first design principles.
Positioning is crucial for creating complex layouts and interactive components. Master these techniques to build professional user interfaces.