CSS Layout & Positioning
CSS positioning lets you place elements exactly where you want them. Understanding position types is essential for complex layouts.
Display Property
Controls how elements behave:
/* Block: Starts on new line, takes full width */
div {
display: block;
}
/* Inline: Flows with text */
span {
display: inline;
}
/* Inline-block: Flows inline but accepts width/height */
button {
display: inline-block;
}
/* Flexbox */
.container {
display: flex;
}
/* Grid */
.grid {
display: grid;
}
/* Hide element */
.hidden {
display: none;
}
Position Property
Static (Default)
Normal document flow:
.element {
position: static; /* Default - no positioning */
}
Relative
Offset from normal position:
.element {
position: relative;
top: 10px; /* 10px down from normal position */
left: 20px; /* 20px right from normal position */
bottom: 5px; /* 5px up from normal position */
right: 10px; /* 10px left from normal position */
}
Use for: Creating positioning context for children
Absolute
Removed from flow, positioned relative to nearest positioned ancestor:
.parent {
position: relative; /* Creates positioning context */
}
.child {
position: absolute;
top: 0;
right: 0;
width: 100px;
height: 100px;
}
Use for: Overlays, badges, icons
Fixed
Removed from flow, positioned relative to viewport:
.fixed-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 100;
}
.back-to-top {
position: fixed;
bottom: 20px;
right: 20px;
}
Use for: Sticky headers, floating buttons
Sticky
Hybrid of relative and fixed:
.sticky-nav {
position: sticky;
top: 0;
background: white;
z-index: 50;
}
Use for: Sticky navigation, table headers
Z-Index
Controls stacking order (higher = on top):
.front {
position: absolute;
z-index: 10; /* On top */
}
.back {
position: absolute;
z-index: 1; /* Behind */
}
Note: Only works on positioned elements (not static)
Overflow
Controls what happens when content exceeds container:
/* Hidden: Content is clipped */
.container {
overflow: hidden;
}
/* Scroll: Always shows scrollbar */
.container {
overflow: scroll;
}
/* Auto: Scrollbar only when needed */
.container {
overflow: auto;
}
/* Individual axes */
.container {
overflow-x: hidden;
overflow-y: auto;
}
Complete Example: Sticky Header
/* Sticky header */
.header {
position: sticky;
top: 0;
z-index: 100;
background-color: #333;
color: white;
padding: 1rem 2rem;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
/* Page content */
.main {
padding: 2rem;
min-height: 100vh;
}
<header class="header">
<h1>Sticky Header</h1>
</header>
<main class="main">
<p>Scroll down to see sticky header...</p>
</main>
Complete Example: Modal Overlay
/* Modal overlay */
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.7);
display: flex;
align-items: center;
justify-content: center;
z-index: 1000;
}
/* Modal content */
.modal {
background: white;
padding: 2rem;
border-radius: 10px;
max-width: 500px;
width: 90%;
position: relative;
}
/* Close button */
.modal-close {
position: absolute;
top: 10px;
right: 10px;
background: none;
border: none;
font-size: 1.5rem;
cursor: pointer;
}
<div class="modal-overlay">
<div class="modal">
<button class="modal-close">×</button>
<h2>Modal Title</h2>
<p>Modal content goes here...</p>
</div>
</div>
Complete Example: Tooltip
/* Tooltip container */
.tooltip {
position: relative;
display: inline-block;
}
/* Tooltip text */
.tooltip-text {
position: absolute;
bottom: 125%;
left: 50%;
transform: translateX(-50%);
background-color: #333;
color: white;
padding: 5px 10px;
border-radius: 5px;
white-space: nowrap;
opacity: 0;
visibility: hidden;
transition: opacity 0.3s;
}
/* Show tooltip on hover */
.tooltip:hover .tooltip-text {
opacity: 1;
visibility: visible;
}
<span class="tooltip">
Hover me
<span class="tooltip-text">This is a tooltip!</span>
</span>
Complete Example: Back to Top Button
.back-to-top {
position: fixed;
bottom: 30px;
right: 30px;
width: 50px;
height: 50px;
background-color: #3498db;
color: white;
border: none;
border-radius: 50%;
cursor: pointer;
opacity: 0;
visibility: hidden;
transition: opacity 0.3s, transform 0.3s;
z-index: 99;
}
.back-to-top.visible {
opacity: 1;
visibility: visible;
}
.back-to-top:hover {
transform: translateY(-5px);
}
<button class="back-to-top" onclick="window.scrollTo(0,0)">
↑
</button>
Layout Techniques
Centering
/* Flexbox centering */
.flex-center {
display: flex;
align-items: center;
justify-content: center;
}
/* Grid centering */
.grid-center {
display: grid;
place-items: center;
}
/* Absolute centering */
.absolute-center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
Sticky Footer
/* Flexbox sticky footer */
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin: 0;
}
main {
flex: 1;
}
footer {
padding: 1rem;
background: #333;
color: white;
}
Best Practices
- Use flexbox/grid for layouts — Not positioning
- Use position: fixed sparingly — Can cause scrolling issues
- Always set z-index — Prevent stacking issues
- Use transform for animations — Better performance
- Test on mobile — Fixed elements behave differently
Common Mistakes
- Using position for layouts — Use flexbox/grid instead
- Forgetting z-index — Elements overlap unexpectedly
- Not setting position on parent — Absolute children float away
- Overusing fixed position — Causes scrolling problems
- Ignoring transform — Better than top/left for animations