Is the CSS 'position: absolute' applied to the element immediately following the body tag

Quoting from msdn:

"The object is placed relative to the position of its parent element—or to the body element if the parent element is not positioned"

Let's consider a scenario where I set a div with specific dimensions to have a bottom value of 0 and left value of 0. In this case, it won't be positioned at the bottom of the body element but rather at the bottom left corner of the viewport. Even when applying a margin to the body element, the div will still remain at the bottom left of the viewport.

I understand how these properties function in CSS, but I am seeking clarification on the underlying rationale behind them. Shouldn't the absolute element be positioned relative to the body when no other ancestor elements are designated as positioned? Thank you for your insights!

For reference, here is the fiddle link: http://jsfiddle.net/picbig/0p6rgv8f/

HTML:

<div id="large_box_greater_than_viewport"></div>
<div id="absolute_cnt"></div>

CSS:

body{
    margin-left: 200px;
}
#large_box_greater_than_viewport{
    width: 900px;
    height: 10000px;
    background: red;
}
#absolute_cnt{
    position: absolute;
    width: 65%;
    bottom: 0;
    left: 0;
    height: 80px;
    background: rgba(0,0,0,.7);
}

Answer №1

Absolutely positioned elements are placed in relation to the block that contains them.

Fixed positioned elements adhere to the initial containing block, which matches the dimensions of the viewport.

Initial containing block

The container within which the root element resides is a rectangle known as the initial containing block. For continuous media, it has the dimensions of the viewport and starts at the canvas origin; for paged media, it equates to the page area.

On the other hand, absolute positioned elements relate to their containing block established by the closest ancestor with a non-static position, such as fixed, absolute, or relative.

The important point to note:

If no such ancestor exists, then the containing block defaults to the initial containing block.

Therefore, an absolutely positioned element inside the <body> will not be aligned with the <body> itself, but rather with the initial containing block, namely the viewport.

http://www.w3.org/TR/CSS2/visudet.html#containing-block-details

Answer №2

To adjust the alignment of the body, it is important to specify its position:

body{
    margin-left: 200px;
    position:relative;
}

Answer №3

After experimenting with different browsers, it's clear that you're correct!

The element doesn't have the expected positioning unless the body also has a defined position.

In cases where the body lacks a specified position, the element will be positioned relative to the viewport instead.

Answer №4

1). Indeed, it is possible to apply absolute positioning to a tag; however, the tag must be nested within another tag with relative positioning.

2). Alternatively, you can achieve this using the after-before feature.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to gradually reveal an element as I scroll to a specific position?

If the window top position is about 100px from the bottom, I want to create a fading in/out effect for the element overlay. Check out this image Any ideas on how to achieve this? .section-card{ position: relative; height: 500px; line-heigh ...

Tips for customizing css for image tags

Here is the CSS code I used for images: img { border: none; max-width: 100%; height: auto !important } To override specific image styles, I added this at the bottom: .locals-list>img { height: 35px; } Below is the HTML code I am work ...

The Bootstrap Modal consistently shows the contents of the first record when used within a SQL While Statement

As a newcomer, I am in the process of converting an operational Bootstrap modal that houses a post form. Initially, the modal would open using a button with a specific Id and would always display the FORM contents from the first record found in the SQL w ...

How to rename a class of an image tag using jQuery

I need to update the class name in my image tag <img src="img/nex2.jpeg" class="name"> When hovering over with jquery, I want to add a new class to it. After hovering, the tag should appear as follows: <img src="img/nex2.jpeg" class="name seco ...

What steps can I take to ensure the image remains fixed in its position when the browser width is reduced?

Recently, I encountered a challenge with my media query where I wanted to align a button next to another one when the browser width was reduced to 991px. However, instead of staying in place, the button started sliding underneath the other button as I cont ...

The canvas is responsive to keyboard commands, however, the image remains stationary and unaffected

As I delved into the basics, incorporating canvas, CSS, and JavaScript, a question arose: should the image be housed in the HTML or the JavaScript code? Following this, I added a background color to the canvas and defined properties of the canvas in JavaSc ...

Adding a slight 1px right margin between an HTML table and its enclosing div in Bootstrap 3

Help! I'm trying to troubleshoot some HTML issues and can't figure out where this pesky 1px gap between my table and the wrapping div is coming from... Just for reference, I am using Bootstrap. I suspect that it could be a glitch in the Bootst ...

Adjusting the positioning of my webpage's content towards the header?

As a beginner in the world of HTML and CSS, I am currently working on customizing a Tumblr page. The template provided by the platform has been modified to include additional elements. However, I have encountered an issue where there seems to be a gap bet ...

Tips for clicking the close button on a modal using Selenium

I have tested various combinations, but I am unable to successfully click on the X button in order to close the modal window. Looking at the provided html code: <div class="modal-close" data-dismiss="modal"> & ...

Issue with animated underline not appearing after link is selected

Utilizing Bootstrap and SCSS to tailor my CSS file, I've implemented a nav-link with the following style: .sidebar .nav-pills .nav-link:hover, .sidebar .nav-pills .show > .nav-link { @extend %underline-link; background: linear-gradient(120d ...

What is the best way to incorporate a background image while using floated divs?

Is there a way to align divs next to each other without them overlapping when the container-div has a background? I've tried using "float: left" but encountered issues with the inner divs being positioned outside the container-div once a background co ...

How can you align an icon to the right in a Bootstrap4 navbar while keeping it separate from the toggle menu?

Looking to utilize the Twitter Bootstrap framework for structuring my navbar with distinct "left", "middle", and "right" sections, where the middle portion collapses beneath the navbar-toggler (burger menu) when space is limited. For a self-contained exam ...

Combining all CSS files into one and consolidating all JavaScript files into a single unified file

Whenever I need to add a new CSS or JS file, I always place it in the header section like this Add CSS files <link rel="stylesheet" href="<?php echo URL; ?>public/css/header.css" /> <link rel="stylesheet" href="<?php echo URL; ?> ...

How can I format and send an email message using HTML?

I'm currently working on a form and encountering an issue. HTML does not natively support sending emails without a specific email program installed. Is there perhaps a way to send a message that can be converted into an email? Are there any services a ...

A method for aligning two different font sizes vertically within a single <p> element

My question is, how can I vertically align the following: Goal: - - | - | - - Currently, the vertical alignment defaults to baseline. Current Alignment: _ _ | _ | _ _ HTML: <div class="moduleResult"> <p>You have <span class= ...

Struggling to keep my image buttons aligned in a horizontal line at the center, but they keep stacking vertically instead

I'm struggling to keep a row of image buttons centered horizontally, regardless of the window size. However, when I manage to center the buttons, they end up stacking vertically instead of aligning in a single line. I've experimented with differ ...

"Achieving Alignment: Placing a Div Element Inside an H

I am in the process of building a portfolio for freecodecamp, but I am having trouble with centering the button and row div on the page. The row div is necessary because I will be adding more buttons later on, but for now I just need the FCC button. Below ...

Upgrade the arrow accordion in bootstrap by incorporating images instead

I have implemented a Bootstrap accordion with a toggle arrow. Here is an example: http://jsfiddle.net/zessx/r6eaw/12/ My question is, if I want to change the arrow with an image, what do I need to edit? Or how can I implement it? This is the image I want ...

Troubleshooting Problem with Bootstrap 4 Navigation Drop-Down Menu

I am working on developing some navigation forms for my university projects. I encountered an issue where I selected the Book item, and the navigation was working fine. However, when I selected Child items and then clicked on the Organization item, the nav ...

The overflow scroll feature seems to be malfunctioning and not working as intended in the

I'm trying to implement the overflow scroll property in a div to display a scroll bar for the user, but I'm facing issues. I'm unsure what the problem could be. The code I'm working with can be found here : http://fiddle.jshell.net/Gg ...