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

Top Element Position


The CSS top property specifies the vertical position of a positioned element from the top edge of its containing block. This property only works on elements with a position value of absolute, relative, fixed, or sticky. You can use length values (like px, em), percentages, or the keyword auto.


Example 1: top with Relative Positioning

/* style.css */
.box-relative {
  position: relative; /* Set the positioning context */
  top: 20px; /* Move the element 20px down from its normal position */
  background-color: lightblue;
  width: 150px;
  padding: 10px;
}

Explanation With position: relative, the top property moves the element down from its original place in the document flow. The space it would have occupied is preserved.


Example 2: top with Absolute Positioning

/* style.css */
.container {
  position: relative; /* Establishes the containing block */
  height: 200px;
  border: 2px solid #333;
}
.box-absolute {
  position: absolute; /* Position relative to the nearest positioned ancestor */
  top: 10px; /* Place 10px from the top of .container */
  left: 10px;
  background-color: lightcoral;
  width: 100px;
  padding: 10px;
}

Explanation When using position: absolute, the top property positions the element relative to its nearest ancestor that has a position other than static. Here, .box-absolute is placed 10px from the top edge of .container.


Example 3: top with Fixed Positioning

/* style.css */
.fixed-header {
  position: fixed; /* Position relative to the viewport */
  top: 0; /* Stick to the very top of the browser window */
  left: 0;
  width: 100%;
  background-color: #333;
  color: white;
  padding: 10px;
  text-align: center;
}

Explanation An element with position: fixed is positioned relative to the browser window, or viewport. Setting top: 0 makes the element stick to the top of the screen, even when the user scrolls.


Example 4: top with a Percentage Value

/* style.css */
.parent-container {
  position: relative;
  height: 300px; /* Height is required for percentage-based top to work */
  border: 2px solid lightgreen;
}
.percent-box {
  position: absolute;
  top: 50%; /* Position the top edge at 50% of the parent's height */
  background-color: lightgreen;
  width: 100px;
}

Explanation A percentage value for top is calculated based on the height of the containing block. In this case, top: 50% pushes the element down by half the height of its parent.


Example 5: top with a Negative Value

/* style.css */
.box-negative {
  position: relative;
  top: -15px; /* Move the element 15px up from its normal position */
  background-color: gold;
  padding: 10px;
  width: 150px;
}

Explanation Using a negative value for top moves the positioned element in the opposite direction, which is upward. This can be useful for creating overlapping effects.


Example 6: top with Sticky Positioning

/* style.css */
.sticky-nav {
  position: sticky; /* Acts like relative until it hits the offset */
  top: 0; /* Becomes fixed at the top when scrolling past */
  background-color: slateblue;
  color: white;
  padding: 15px;
}

Explanation An element with position: sticky scrolls with the page until it reaches the offset defined by top. It then "sticks" to that position as the user continues to scroll.


Example 7: Vertical Centering with top and transform

/* style.css */
.center-container {
  position: relative;
  height: 250px;
  border: 2px solid orange;
}
.centered-item {
  position: absolute;
  top: 50%; /* Move the top edge to the vertical midpoint */
  left: 50%;
  transform: translate(-50%, -50%); /* Shift element up by half its own height */
  background-color: orange;
}

Explanation This is a modern technique for perfect vertical and horizontal centering. The top: 50% moves the element's top edge to the container's midpoint, and transform: translateY(-50%) then pulls the element up by half of its own height to align its center.