W3Newbie
  • HTML Tutorial
  • CSS Tutorial
  • Web Dev
    • VS Code Tutorial
    • Command Line Tutorial
    • Git Tutorial
    • Github Tutorial
    • Sass Tutorial
  • Courses
  • Resources
  • Tutorials
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu

CSS Positioning

CSS positioning refers to the position CSS property that controls how an HTML element is positioned on a web page relative to its normal document flow. It allows you to break free from the usual linear layout and place elements exactly where you want them, creating flexible and dynamic web page layouts.

Key CSS Positioning Examples:

1. Static Positioning (default):

  • Elements flow in the normal document order, one after the other.
  • Not affected by top, left, bottom, or right properties.
  • Properties: position: static

CSS:

.static-box {
  position: static;
}

2. Relative Positioning:

  • Element positioned relative to its normal position in the flow.
  • Can be offset using top, left, bottom, and right properties.
  • Does not affect surrounding elements.
  • Properties: position: relative; top; bottom; left; right

CSS:

.relative-box {
  position: relative;
  top: 20px;
  left: 10px;
}

3. Absolute Positioning:

  • Element removed from the normal document flow and positioned relative to its nearest positioned ancestor (not necessarily its parent).
  • If no positioned ancestor, it’s positioned relative to the viewport.
  • Properties: position: absolute; top; bottom; left; right

CSS:

.absolute-box {
  position: absolute;
  top: 50px;
  right: 0;
}

4. Fixed Positioning:

  • Elements are positioned relative to the viewport (browser window), staying fixed even when scrolling.
  • Properties: position: fixed; top; bottom; left; right

CSS:

.fixed-navbar {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  background-color: #f0f0f0;
}

5. Sticky Positioning:

  • Element positioned based on the user’s scroll position.
  • Becomes fixed when it reaches a specified position within the viewport, scrolling with the page.
  • Properties: position: sticky; top; bottom

CSS:

.sticky-header {
  position: sticky;
  top: 0; /* Becomes fixed when reaching the top of the viewport */
}

Choosing the right CSS positioning method:

  • Use static for elements in the natural flow.
  • Use relative for minor adjustments within the flow.
  • Use absolute for elements independent of the flow, like pop-ups or overlays.
  • Use fixed for elements constantly visible, like navigation bars.
  • Use sticky for elements that switch between fixed and normal flow, like sidebars.

Additional notes:

  • Each positioning method has its own set of rules and properties that govern its behavior.
  • Understanding the stacking order, z-index, defines which element overlaps others when multiple elements are positioned using similar techniques.
  • Remember to test your positioning on different screen sizes and devices to ensure consistent rendering across them.
< Previous
Next Lesson >
  • CSS Tutorial
  • CSS Introduction
  • CSS Syntax
  • CSS Style Sheets
  • CSS Comments
  • CSS Fonts
  • CSS Sizing
  • CSS Colors
  • CSS Backgrounds
  • CSS Borders
  • CSS Outlines
  • CSS Margin
  • CSS Padding
  • CSS Box Model
  • CSS Floats
  • CSS Alignment
  • CSS Positioning
  • CSS Overflow
  • CSS Z-Index
  • CSS Opacity
  • CSS Pseudo-Classes
  • CSS Pseudo-Elements
  • CSS Buttons
  • CSS Icons
  • CSS Media Queries
  • CSS Responsive Web Design
  • CSS Navigations
  • CSS Animations
  • CSS Flexbox
  • CSS Grid
  • CSS Responsive Typography
  • CSS Mobile-First Responsive
  • CSS Fluid Layouts
  • CSS Variables

The Newbie Template Bundle

The 12 Website Bundle Pack

Subscribe on YouTube

Web Development Courses

The HTML Email Mastery Course - Build Responsive HTML Email Templates.

Latest Tutorial Posts

  • The HTML Email Mastery Course - Build Responsive HTML Email Templates.w3newbie.com
    HTML Email Mastery Course CouponOctober 12, 2020 - 8:10 pm
  • Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4w3newbie.com
    Create A 5 Page Website With PHP Includes, HTML5, CSS3 & Bootstrap 4April 22, 2019 - 1:48 pm
© w3newbie.com, 2026. Terms of Use. Privacy Policy.
Scroll to top Scroll to top Scroll to top