CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Absolute Position


The absolute value for the position property removes an element from the normal document flow. This allows you to place it anywhere on the page using the top, right, bottom, and left properties. The element is positioned relative to its nearest positioned ancestor (an ancestor with a position value other than static). If no positioned ancestor exists, it is positioned relative to the initial containing block, which is usually the <html> element.


Example 1: Basic Absolute Positioning

CSS

.parent {
  position: relative; /* Establishes a positioning context for the child */
  height: 200px;
  border: 2px solid #333;
}

.child-absolute {
  position: absolute; /* Positions the element relative to the parent */
  top: 20px;
  left: 30px;
  background-color: #f2b447;
  padding: 10px;
}

Explanation: The .child-absolute element is taken out of the normal document flow. It is then positioned 20 pixels from the top and 30 pixels from the left of its parent container, .parent, which has position: relative.


Example 2: Centering an Absolutely Positioned Element

CSS

.container {
  position: relative;
  height: 250px;
  border: 2px solid #6a1b9a;
}

.centered {
  position: absolute;
  top: 50%; /* Moves the top edge to the vertical center */
  left: 50%; /* Moves the left edge to the horizontal center */
  transform: translate(-50%, -50%); /* Shifts the element back by half its own width and height */
  background-color: #8e44ad;
  color: white;
  padding: 20px;
}

Explanation: This technique centers an element both vertically and horizontally. Setting top and left to 50% aligns the top-left corner of the element with the center of the parent. The transform: translate(-50%, -50%) then shifts the element up and to the left by half of its own dimensions, perfectly centering it.


Example 3: Overlapping Elements

CSS

.card {
  position: relative;
  width: 300px;
  height: 180px;
  border: 1px solid #ccc;
}

.badge {
  position: absolute;
  top: -10px; /* Positions the badge slightly outside the top edge */
  right: -10px; /* Positions the badge slightly outside the right edge */
  background-color: #e74c3c;
  color: white;
  padding: 5px 10px;
  border-radius: 5px;
}

Explanation: The .badge element is positioned at the top-right corner of the .card. By using negative values for top and right, it is made to overlap and appear slightly outside the boundaries of its container, a common technique for labels or notifications.


Example 4: Full-Screen Overlay

CSS

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.7); /* Semi-transparent black background */
  z-index: 10; /* Ensures the overlay is on top of other content */
}

Explanation: By setting top, left, right, and bottom all to 0, the .overlay element stretches to cover the entire viewport (or its nearest positioned ancestor). The z-index property ensures it sits on top of other elements.


Example 5: Positioning Relative to the Viewport

CSS

/* No positioned ancestor is needed for this example */

.notification-banner {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  background-color: #3498db;
  color: white;
  text-align: center;
  padding: 15px;
}

Explanation: When an absolutely positioned element has no positioned ancestor, it is placed relative to the initial containing block (the viewport). This .notification-banner will appear at the very top of the browser window.


Example 6: Bottom-Right Fixed-Like Button

CSS

body {
  position: relative; /* Or any other container that wraps the content */
  min-height: 100vh;
}

.help-button {
  position: absolute;
  bottom: 20px;
  right: 20px;
  background-color: #2ecc71;
  color: white;
  padding: 15px;
  border-radius: 50%;
  cursor: pointer;
}

Explanation: This example places a .help-button at the bottom-right of the page. It is positioned relative to the <body> element, which acts as the containing block.


Example 7: Using auto for Positioning

CSS

.box-container {
  position: relative;
  height: 200px;
  border: 2px solid #f39c12;
}

.mover {
  position: absolute;
  left: 10px;
  bottom: 10px;
  top: auto; /* Explicitly sets top positioning to be determined by the browser */
  right: auto; /* Explicitly sets right positioning to be determined by the browser */
  background-color: #e67e22;
  padding: 10px;
}

Explanation: While not always necessary to declare, auto is the default value for top, right, bottom, and left. This example explicitly shows that by setting left and bottom, the element is positioned from the bottom-left corner, and top and right are automatically calculated by the browser.