CSS Position
Master element positioning with CSS position property
Understanding CSS Position
The CSS position property specifies how an element is positioned in a document. It's one of the most powerful tools for creating complex layouts and controlling element placement.
Position Values:
- static: Default positioning (normal document flow)
- relative: Positioned relative to its normal position
- absolute: Positioned relative to nearest positioned ancestor
- fixed: Positioned relative to the viewport
- sticky: Switches between relative and fixed based on scroll
1. Static Position (Default)
/* Static positioning - default behavior */
.static-element {
position: static;
/* top, right, bottom, left properties have no effect */
}
/* Elements follow normal document flow */
.normal-flow {
/* No position property needed - static is default */
margin: 10px;
padding: 20px;
}
2. Relative Position
/* Relative positioning */
.relative-element {
position: relative;
top: 20px; /* Move 20px down from normal position */
left: 30px; /* Move 30px right from normal position */
}
/* Original space is preserved */
.relative-container {
position: relative;
/* Child elements can be positioned relative to this */
}
/* Practical example */
.badge {
position: relative;
top: -5px;
background: red;
color: white;
padding: 2px 6px;
border-radius: 10px;
font-size: 12px;
}
3. Absolute Position
/* Absolute positioning */
.absolute-element {
position: absolute;
top: 50px; /* 50px from top of positioned parent */
right: 20px; /* 20px from right of positioned parent */
}
/* Positioned relative to nearest positioned ancestor */
.container {
position: relative; /* Creates positioning context */
width: 300px;
height: 200px;
}
.absolute-child {
position: absolute;
top: 10px;
left: 10px;
/* Positioned relative to .container */
}
/* Common use cases */
.close-button {
position: absolute;
top: 10px;
right: 10px;
background: none;
border: none;
font-size: 20px;
cursor: pointer;
}
4. Fixed Position
/* Fixed positioning - relative to viewport */
.fixed-header {
position: fixed;
top: 0;
left: 0;
width: 100%;
background: white;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
z-index: 1000;
}
.fixed-sidebar {
position: fixed;
top: 60px; /* Below fixed header */
left: 0;
width: 250px;
height: calc(100vh - 60px);
background: #f5f5f5;
overflow-y: auto;
}
/* Fixed floating action button */
.fab {
position: fixed;
bottom: 20px;
right: 20px;
width: 60px;
height: 60px;
border-radius: 50%;
background: #007bff;
color: white;
border: none;
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
cursor: pointer;
}
5. Sticky Position
/* Sticky positioning */
.sticky-header {
position: sticky;
top: 0; /* Stick when reaching top of viewport */
background: white;
padding: 10px 0;
border-bottom: 1px solid #eee;
z-index: 100;
}
/* Sticky sidebar */
.sticky-sidebar {
position: sticky;
top: 20px; /* 20px from top when sticky */
height: fit-content;
}
/* Sticky table header */
.sticky-table th {
position: sticky;
top: 0;
background: #f8f9fa;
z-index: 10;
}
6. Z-Index and Stacking Context
/* Z-index controls stacking order */
.modal-backdrop {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.5);
z-index: 1000;
}
.modal {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: white;
padding: 20px;
border-radius: 8px;
z-index: 1001; /* Above backdrop */
}
/* Dropdown menu */
.dropdown {
position: relative;
}
.dropdown-menu {
position: absolute;
top: 100%;
left: 0;
background: white;
border: 1px solid #ccc;
border-radius: 4px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
z-index: 100;
}
7. Practical Examples
Card with Badge
.card {
position: relative;
background: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.card-badge {
position: absolute;
top: -10px;
right: -10px;
background: #ff4757;
color: white;
padding: 5px 10px;
border-radius: 12px;
font-size: 12px;
font-weight: bold;
}
Image Overlay
.image-container {
position: relative;
overflow: hidden;
border-radius: 8px;
}
.image-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(
to bottom,
transparent,
rgba(0,0,0,0.7)
);
display: flex;
align-items: flex-end;
padding: 20px;
}
.overlay-text {
color: white;
font-size: 18px;
font-weight: bold;
}
Navigation with Dropdown
.nav {
position: relative;
display: flex;
align-items: center;
gap: 20px;
}
.nav-item {
position: relative;
}
.dropdown-toggle {
background: none;
border: none;
cursor: pointer;
padding: 10px;
}
.dropdown-menu {
position: absolute;
top: 100%;
left: 0;
min-width: 200px;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
opacity: 0;
visibility: hidden;
transform: translateY(-10px);
transition: all 0.3s ease;
}
.nav-item:hover .dropdown-menu {
opacity: 1;
visibility: visible;
transform: translateY(0);
}
8. Centering Techniques
/* Absolute centering */
.absolute-center {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* Fixed centering */
.fixed-center {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* Centering with known dimensions */
.center-known-size {
position: absolute;
top: 50%;
left: 50%;
width: 300px;
height: 200px;
margin-left: -150px; /* -width/2 */
margin-top: -100px; /* -height/2 */
}
Common Issues and Solutions
Positioning Problems:
- Z-index not working: Element needs position other than static
- Absolute element not positioning correctly: Check for positioned parent
- Fixed element covering content: Add appropriate margins/padding to body
- Sticky not working: Check for overflow:hidden on parent
Best Practices
Position Guidelines:
- Use sparingly: Prefer flexbox/grid for layout when possible
- Create positioning context: Use position: relative on containers
- Mind the z-index: Keep stacking order organized
- Consider accessibility: Ensure positioned elements are keyboard accessible
- Test on mobile: Fixed/absolute positioning can cause issues on small screens