Tillitsdone
down Scroll to discover

CSS Position A Comprehensive Guide for Web Developers

Learn about the CSS position property and how to use it to create dynamic and visually appealing layouts.

Explore the different options available, such as static, relative, absolute, fixed, and sticky positioning.
thumbnail

Introduction

The position property in CSS is crucial for controlling where an element sits on your web page. It works with properties like top, right, bottom, and left to determine the final spot for an element. Understanding this property is key for web developers because it helps create dynamic and visually appealing layouts.

Values

The position property in CSS can take several values, each affecting how an element is positioned within the document. Here are the main values you can use:

  1. static:
    • The default value. The element follows the normal document flow.
    • The top, right, bottom, left, and z-index properties have no effect.
  2. relative:
    • The element follows the normal document flow but can be offset using top, right, bottom, and left.
    • The offset does not affect the position of other elements.
  3. absolute:
    • The element is removed from the normal document flow, and no space is created for it in the layout.
    • It is positioned relative to its closest positioned ancestor (if any) or to the initial containing block.
    • The top, right, bottom, and left properties determine its final position.
  4. fixed:
    • The element is removed from the normal document flow, and no space is created for it in the layout.
    • It is positioned relative to the viewport, staying in the same position on the screen even when the page is scrolled.
    • The top, right, bottom, and left properties determine its final position.
  5. sticky:
    • The element follows the normal document flow but can be offset relative to its nearest scrolling ancestor and containing block.
    • The offset does not affect the position of other elements.
    • This value creates a new stacking context.
    • Note: At least one inset property (top, right, bottom, left) needs to be set to a non-auto value for the axis on which the element needs to be made sticky.
  6. inherit:
    • The element inherits the position value from its parent element.
  7. initial:
    • Sets the position value to its default value, which is static.
  8. revert:
    • Reverts the position value to the browser’s default stylesheet value.
  9. revert-layer:
    • Reverts the position value to the value established by the user-agent stylesheet.
  10. unset:
    • Resets the position value to its inherited value if it inherits, or to its initial value if it does not.

Description

The position property in CSS is fundamental for controlling the layout and positioning of elements on a web page. It allows you to specify how an element should be placed within its containing block, relative to other elements, or even fixed to the viewport. Here’s a breakdown of the types of positioning you can achieve with the position property:

Types of Positioning

  1. Positioned Element:
    • A positioned element is one whose computed position value is relative, absolute, fixed, or sticky.
    • This type of element can be precisely placed using the top, right, bottom, and left properties.
  2. Relatively Positioned Element:
    • A relatively positioned element has a computed position value of relative.
    • It is positioned according to the normal flow of the document but can be offset using the top, right, bottom, and left properties.
    • The space that the element would normally occupy is preserved, and the offset does not affect the position of other elements.
  3. Absolutely Positioned Element:
    • An absolutely positioned element has a computed position value of absolute or fixed.
    • It is removed from the normal document flow, meaning no space is created for it in the layout.
    • The element is positioned relative to its closest positioned ancestor (if any) or to the initial containing block.
    • The top, right, bottom, and left properties specify offsets from the edges of the element’s containing block.
    • The element establishes a new block formatting context (BFC) for its contents.
  4. Stickily Positioned Element:
    • A stickily positioned element has a computed position value of sticky.
    • It is positioned according to the normal flow of the document but can be offset relative to its nearest scrolling ancestor and containing block.
    • The offset does not affect the position of other elements.
    • The element behaves as relatively positioned until it crosses a specified threshold, at which point it becomes “stuck” until meeting the opposite edge of its containing block.

Most of the time, absolutely positioned elements with height and width set to auto are sized to fit their contents. However, non-replaced, absolutely positioned elements can be made to fill the available vertical space by specifying both top and bottom and leaving height unspecified (i.e., auto). They can likewise be made to fill the available horizontal space by specifying both left and right and leaving width as auto.

Conflict Resolution:

  • If both top and bottom are specified (technically, not auto), top wins.
  • If both left and right are specified, left wins when the direction is ltr (English, horizontal Japanese, etc.), and right wins when the direction is rtl (Persian, Arabic, Hebrew, etc.).

Examples

Relative Positioning

Relatively positioned elements are offset a given amount from their normal position within the document, but without the offset affecting other elements. In the example below, note how the other elements are placed as if “Two” were taking up the space of its normal location.

HTML
<div class="box" id="one">One</div>
<div class="box" id="two">Two</div>
<div class="box" id="three">Three</div>
<div class="box" id="four">Four</div>
CSS
* {
box-sizing: border-box;
}
.box {
display: inline-block;
width: 100px;
height: 100px;
background: red;
color: white;
}
#two {
position: relative;
top: 20px;
left: 20px;
background: blue;
}

Absolute Positioning

Elements that are relatively positioned remain in the normal flow of the document. In contrast, an element that is absolutely positioned is taken out of the flow; thus, other elements are positioned as if it did not exist. The absolutely positioned element is positioned relative to its nearest positioned ancestor (i.e., the nearest ancestor that is not static). If a positioned ancestor doesn’t exist, it is positioned relative to the initial containing block (ICB), which is the containing block of the document’s root element.

HTML
<h1>Absolute positioning</h1>
<p>
I am a basic block-level element. My adjacent block-level elements sit on new
lines below me.
</p>
<p class="positioned">
By default, we span 100% of the width of our parent element, and we are as tall
as our child content. Our total width and height are our content + padding +
border width/height.
</p>
<p>
We are separated by our margins. Because of margin collapsing, we are
separated by the width of one of our margins, not both.
</p>
<p>
Inline elements <span>like this one</span> and <span>this one</span> sit on
the same line as one another, and adjacent text nodes, if there is space on
the same line. Overflowing inline elements
<span>wrap onto a new line if possible — like this one containing text</span>,
or just go on to a new line if not, much like this image will do:
<img src="long.jpg" />
</p>
CSS
* {
box-sizing: border-box;
}
body {
width: 500px;
margin: 0 auto;
}
p {
background: aqua;
border: 3px solid blue;
padding: 10px;
margin: 10px;
}
span {
background: red;
border: 1px solid black;
}
.positioned {
position: absolute;
background: yellow;
top: 30px;
left: 30px;
}

Fixed Positioning

Fixed positioning is similar to absolute positioning, with the exception that the element’s containing block is the initial containing block established by the viewport, unless any ancestor has transform, perspective, or filter property set to something other than none. This can be used to create a “floating” element that stays in the same position regardless of scrolling. In the example below, box “One” is fixed at 80 pixels from the top of the page and 10 pixels from the left. Even after scrolling, it remains in the same place relative to the viewport.

HTML
<div class="box" id="one">One</div>
<div class="box" id="two">Two</div>
<div class="box" id="three">Three</div>
<div class="box" id="four">Four</div>
CSS
* {
box-sizing: border-box;
}
.box {
display: inline-block;
width: 100px;
height: 100px;
background: red;
color: white;
}
#one {
position: fixed;
top: 80px;
left: 10px;
background: blue;
}

Sticky Positioning

Sticky positioning is a hybrid of relative and fixed positioning. The element is positioned according to the normal flow of the document, but it becomes “stuck” to a specified position within its containing block. This is particularly useful for creating elements that remain visible as the user scrolls, such as sticky navigation bars.

HTML
<div class="box" id="one">One</div>
<div class="box" id="two">Two</div>
<div class="box" id="three">Three</div>
<div class="box" id="four">Four</div>
CSS
* {
box-sizing: border-box;
}
.box {
display: inline-block;
width: 100px;
height: 100px;
background: red;
color: white;
}
#two {
position: sticky;
top: 20px;
background: blue;
}

By understanding and applying these different types of positioning, you can create dynamic and visually appealing web designs that enhance the user experience.

Accessibility

Ensure your positioned elements are accessible:

  1. Avoid Obscuring Content:
    • Make sure positioned elements don’t hide important content. Provide a way to hide or dismiss fixed elements.
  2. Zooming and Scaling:
    • Test your layout at different zoom levels to ensure all content remains accessible.
  3. Focus Management:
    • Ensure keyboard navigation isn’t disrupted by fixed elements. Use tabindex to manage focus order.
  4. Screen Reader Compatibility:
    • Ensure positioned elements are announced correctly by screen readers. Use ARIA roles and properties.

Performance & Accessibility

Performance Considerations

  1. Repainting and Reflow:
    • Fixed or sticky elements can cause performance issues. Use will-change: transform to improve repaint speed.
  2. Optimizing CSS:
    • Avoid overusing positioning. Use CSS Grid or Flexbox for layouts.
  3. Minimizing Layout Thrashing:
    • Avoid frequent changes to the position property to minimize layout recalculations.

Accessibility Considerations

  1. Avoid Obscuring Content:
    • Ensure positioned elements don’t hide important content. Provide a way to hide fixed elements.
  2. Zooming and Scaling:
    • Test your layout at different zoom levels to ensure all content remains accessible.
  3. Focus Management:
    • Ensure keyboard navigation isn’t disrupted by fixed elements. Use tabindex to manage focus order.
  4. Screen Reader Compatibility:
    • Ensure positioned elements are announced correctly by screen readers. Use ARIA roles and properties.

By keeping these considerations in mind, you can ensure a smooth and inclusive user experience.

Resources for Performance and Accessibility

  1. MDN Understanding WCAG, Guideline 1.4 explanations:

    • [Perceivable Guidelines]WebsiteUrl
  2. Visual Presentation: Understanding SC 1.4.8 | Understanding WCAG 2.0:

    • [Visual Presentation Guidelines]WebsiteUrl

By addressing browser compatibility issues, you ensure a consistent experience across different browsers and devices. This is crucial for creating reliable web designs that work for everyone.

Browser Compatibility Tips

  1. Test Across Browsers:

    • Always test your website on multiple browsers and devices for consistent behavior.
    • Use tools like BrowserStack or CrossBrowserTesting to simulate different environments.
  2. Use Vendor Prefixes:

    • Add vendor prefixes for newer CSS features to support older browser versions.
    • Example: position: -webkit-sticky; position: sticky;
  3. Fallbacks:

    • Provide fallbacks for older browsers that don’t support certain CSS properties.
    • Example: Use position: fixed as a fallback for position: sticky.
  4. Progressive Enhancement:

    • Design your site to be functional without advanced CSS features.
    • Add advanced features progressively to enhance the user experience.

Resources for Browser Compatibility

  1. MDN Web Docs Browser Compatibility Tables:

    • Detailed compatibility tables for CSS properties, including position.
    • [MDN Web Docs - CSS position]WebsiteUrl
  2. Can I Use:

    • Check browser compatibility for various web technologies, including CSS properties.
    • [Can I Use - CSS position]WebsiteUrl

By addressing browser compatibility issues, you ensure a consistent experience across different browsers and devices. This is crucial for creating reliable web designs that work for everyone.

See Also

For more information on related CSS properties and techniques, refer to these resources:

  1. MDN Web Docs - CSS top Property Reference:

    • [MDN Web Docs - CSS top]WebsiteUrl
  2. MDN Web Docs - CSS right Property Reference:

    • [MDN Web Docs - CSS right]WebsiteUrl
  3. MDN Web Docs - CSS left Property Reference:

    • [MDN Web Docs - CSS left]WebsiteUrl
  4. MDN Web Docs - CSS bottom Property Reference:

    • [MDN Web Docs - CSS bottom]WebsiteUrl
  5. MDN Web Docs - CSS z-index Property Reference:

    • [MDN Web Docs - CSS z-index]WebsiteUrl
  6. MDN Web Docs - CSS Flexbox Guide:

    • [MDN Web Docs - CSS Flexbox]WebsiteUrl
  7. MDN Web Docs - CSS Grid Layout Guide:

    • [MDN Web Docs - CSS Grid Layout]WebsiteUrl
  8. W3C CSS Positioned Layout Module Level 3:

    • [CSS Positioned Layout Module Level 3]WebsiteUrl
  9. Can I Use:

    • [Can I Use - CSS position]WebsiteUrl
  10. MDN Web Docs - CSS Accessibility:

    • [MDN Web Docs - Accessibility]WebsiteUrl

By exploring these resources, you’ll deepen your understanding of the position property and related CSS techniques, helping you create more dynamic, responsive, and accessible web designs.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.