Why is the combination of position: absolute; and bottom: 0; not aligning with the bottom of the body element as expected

update: After some research, I discovered that adding a position: relative; to the body element will cause the element to be positioned at the bottom of the body. However, I did not find any information on what happens when a non-static parent is not found - what would the position then be relative to? Therefore, I believe this question is not a duplicate of this one. Special thanks to @Ben Kolya Mansley for their input.

====

While working on a component, I encountered an issue. I set an element's position to absolute and bottom to 0, and appended it to the document.body (which has a height greater than the screen's height), but instead of being at the bottom of the body, it is positioned at the bottom of the viewport. Why does this happen?

I consulted the MDN documentation on position and bottom properties. It states that an element with position absolute will seek a non-static positioned parent, and if none is found, it will be positioned relative to the body.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>
    <style>
      html, body {
        margin: 0;
        padding: 0;
      }
      body {
        height: 1500px;
      }
      .bar {
        position: absolute;
        bottom: 0;
      }
    </style>
    <div class="bar">this is bar</div>
  </body>
</html>

Answer №1

According to MDN's guidance on absolute positioning:

An absolutely positioned element is placed in relation to its closest positioned ancestor (i.e., the nearest ancestor that is not static). If there is no such ancestor, it is positioned relative to the ICB (initial containing block), which serves as the container block for the document's root element.

This clarification emphasizes that it isn't the body that serves as the reference point, but rather the initial containing block, which takes on the dimensions of the viewport instead of the body.

When position: relative; is applied to the body, the element will align itself to the bottom.

Further details regarding the Initial Containing Block can be explored in the W3 specification

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

swapping out the text of a link for mobile viewing

This piece of code is responsible for generating the back and next links on my Wordpress website. function.php " class=""> <?php if ( is_single() ) : // navigation links for single posts ?> <?php next_post_link( '<div class=" ...

Outputting PHP Array as an HTML Table

I need help displaying products from a specific category in an HTML table with only three columns. My current code is: <table> <?php $catagory=$_GET["q"]; $con = mysql_connect("localhost","cl49-XXX","XXX"); if (!$con) { die('Co ...

Creating a responsive dropdown submenu with Bootstrap 4, utilizing HTML, CSS, and jQuery

My HTML code includes CSS, jQuery Bootstrap 4 elements. I am attempting to add a dropdown submenu to my main menu when hovering over it. However, I am facing issues in getting the dropdown submenu to work properly. When I hover over main menu items like Co ...

Codec for Web Playback with OpenCV2 Fourcc

I need help finding an OpenCV codec for fourcc that is compatible with cv2.VideoWriter and allows the video to be played back in an HTML file. I've experimented with using 'DIVX' and 'mp4v' through cv2.VideoWriter_fourcc, but they ...

The Jade template is not rendering the page correctly as anticipated

I'm working with Node.js & express, using the Jade template for the view. My issue is that the Test text is currently hidden under the black navigation bar. How can I move it to the bottom, below the navbar? What am I missing in my code? Here is the ...

I am in search of a way to implement horizontal scrolling in Bootstrap 4

I am trying to implement a horizontal scroll feature in Bootstrap 4 with the following code, but I'm having difficulty achieving it. <div class="scroll-horz"> <div class="row"> <div class="col-md-4"> Test </div> < ...

Ways to modify the background color of the entire body excluding a sidebar div

How do I implement an overlay and dim the screen after clicking on a menu button? <ul id="gn-menu" class="gn-menu-main"> <li class="gn-trigger"> <a class="gn-icon gn-icon-menu"><span>Menu</spa ...

Having issues with my CodePen React code and its ability to display my gradient background

I will provide detailed descriptions to help explain my issue. Link to CodePen: https://codepen.io/Yosharu/pen/morErw The problem I am facing is that my background (and some other CSS elements) are not loading properly in my code, resulting in a white bac ...

Give <tbody> the class from the first <tr> using knockout powers

Using the jquery template below results in knockout properly evaluating the databind, but the alert-message block-message class ends up on the <tbody> instead of the targeted <tr>. This issue only occurs when the row with the data-bind is the ...

Unexpected Results from Div Positioning

Here is the PHP code snippet I am working on: <?php include 'header-adminpanel.php'; ?> <div class="container"> <div class="body-content"> <div class="side-left"><?php include 'adminproduct_sidebar.ph ...

Export the Excel file with hyperlinks to HTML format so that they can be easily opened in a file explorer instead of a web browser

I've got an excel spreadsheet that contains links to local folders. In order to optimize its compatibility with various devices, I convert it to HTML format. Everything seems to be working smoothly, except for one minor issue - when clicking on the li ...

What is the correct way to link to a SCSS file within a component's directory?

The structure of my directories is as follows: stylesheets ..modules ...._all.scss ...._colors.scss ..partials ...._all.scss ...._Home.scss ..main.scss In the _Home.scss file, I have the following: @import '../modules/all'; .headerStyle { c ...

Using PHP to extract information from a database and displaying it in an HTML file

In order to create a commenting system that can save comments to a MYSQL database and retrieve them once they have been submitted through the HTML form, I am looking for an efficient method to accomplish this task. Despite my efforts, I have not been able ...

Is there a way to apply CSS styles to a specific word within a div that corresponds to the word entered in the search box?

Using a searchbox, you can enter words to find pages with the searched-for word in the page description. If the word is found in the description, a link to the page along with the highlighted word will be displayed. However, I am encountering an issue wher ...

Creating an icon menu using Bootstrap 4: A step-by-step guide

I'm in the process of creating a navigation bar that includes SVG icons. What's the most effective method to achieve this using Bootstrap 4? This is my desired outcome: The closest I've come (without specifying a fixed size for the icons ...

Unable to properly display the message in Angular

<!DOCTYPE html> <html ng-app> <head> <script data-require="angular.js@*" data-semver="1.4.3" src="https://code.angularjs.org/1.4.3/angular.js"></script> <link rel="stylesheet" href="style.css" /> ...

Tips for styling an asp:DropDownList to resemble a bootstrap dropdown list without relying on the form-control class

As I work on updating an old application with Bootstrap styling, one challenge that keeps arising is how to style an asp:DropDownList to look like a Bootstrap dropdown. I found a helpful Stack Overflow post suggesting the use of the form-control class. Wh ...

Issue with responsive canvas height in Particle-JS

Recently, I've been working on a Bootstrap portfolio where I am experimenting with implementing particle.js over a profile picture. I placed the particles inside a DIV and set a background image as the profile photo. The issue arises when I reload th ...

Getting error messages for form validation in CodeIgniter when using Fileupload via Ajax can be tricky. Here are

Hello, I am currently working on retrieving form errors for an input type file in CodeIgniter. I have created a Javascript code that utilizes Ajax to submit the details as shown below: var name = $("#name").val(); var des = $("#des").val(); var img=$("#fi ...

Scrolling and hovering now triggers the fixed button to toggle class seamlessly

Currently, I am attempting to modify the class of a button on my website. The initial state of the button is wide, but as the user scrolls, it should shrink in size. When hovering over the shrunken button, it should expand back to its original width. Alt ...