How come when I applied height:100% and position: absolute to .test2, it ended up being 200px instead of the expected 204px

Upon setting the height of a container element to 200px, I encountered an issue when trying to set the height of another element within it to 100%. The height didn't render as expected, as height:100% takes the height of the parent element, which was not explicitly defined. However, when I applied position: absolute, the percentages started reflecting the parent class's height, albeit slightly off at 204px instead of the expected 200px.

My inquiry revolves around the mysterious 4px difference and why setting position: absolute seemed to resolve the percentage issue, even without a set height in the parent class. Could you shed some light on why 100% equated to 204px instead of 200px in this scenario? View image reference here.

* {
  box-sizing: border-box;
  padding: 0px;
  margin: 0px;
}

div {
  display: inline-block;
  width: 50px;
}

.container {
  position: relative;
  width: 100%;
}

.test {
  height: 200px;
  background-color: aqua;
}

.test2 {
  height: 100%;
  position: absolute;
  background-color: blue;
}
<div class="container">
  <div class="test"></div>
  <div class="test2"></div>
</div>

My question remains focused on unraveling the mystery of the 4px discrepancy and the change in percentage behavior after utilizing position: absolute, especially in the absence of a specified height in the parent class.

Answer №1

This is not a problem with the css. The issue lies in the height of your container. The second div is taking up 100% of the container's height, while the first div has a fixed height of 200px. You can add a border: 1px solid green to the container to visualize the behavior of the divs.

I recommend using Flex Boxes for situations like this and being mindful of the css properties you apply to your classes.

Answer №2

The issue has been resolved and I will now clarify the reason for the visible space below. The size of the absolutely positioned element will adjust accordingly. The quotes highlighted pertain to the specifications.

The problem arises from the fact that div.container, being a block element container with only inline-level elements inside, creates a new inline formatting context for its content. This is explained as follows:

An inline formatting context is established by a block container box that contains no block-level boxes.
In this formatting context, all inline-level boxes on one line are combined into a line box:
The rectangular area that contains the boxes that form a line is called a line box
. The vertical-align property applies to inline-level elements, with the default value being baseline:
Align the baseline of the box with the baseline of the parent box
. In our case, this default alignment is being utilized. The baseline of our div.test is aligned relative to the baseline strut of the parent block container element. Alignment works relative to the baseline as explained here:
...only have meaning with respect to a parent inline element, or to the strut of a parent block container element
. For an inline-block without content like our div.test, the baseline is the bottom margin edge:
The baseline of an 'inline-block' is the baseline of its last line box ...... in which case the baseline is the bottom margin edge
. After alignment, the relative baseline height of the line box exceeds that of div.test, resulting in the height of div.container being determined by the height of div.test and the empty space below it. This leads to the height of div.container being:
...distance between the top of the topmost line box and the bottom of the bottommost line box
, which in this case corresponds to the line box of div.test.

Applying vertical-align: top or bottom eliminates the space below in the line box since these values align with respect to the line box:

....values align the element relative to the line box
, causing the unintended space to vanish.

If any character is added to div.test, the vertical-align: baseline behaves differently and the baseline of div.test becomes: ...baseline of its last line box, automatically removing the space below.

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

Ionic: Fixed button located at the bottom of a specific ion-slide

I've been creating a series of slides with questions, and the final slide serves as a summary of the previously answered questions. I want to ensure that the submit button is always visible at the bottom of this last slide. However, I've encounte ...

Align the elements of the contact form to the opposite sides

I am experiencing an issue with my contact form where all elements are flowing horizontally instead of vertically. I would like everything to be floated to the left and aligned vertically, except for the "message" box and submit button which should be on t ...

Trouble with Bootstrap card dimensions: Height and width not functioning correctly

I specified a height and width for all card images, but the heights are inconsistent with one being too large and another too small. I want each card to have the same height and width. How can I achieve this? Here is what I tried. .card { height: 50%; ...

What steps should be taken to advance a group of students from one semester to the next?

In a scenario where I need to promote multiple students from semester 1 to semester 2 in one go, the current code logic seems to only handle promotion for single students. How can I modify it to accommodate batch promotion? -------------------Controller ...

Background not covering full height despite setting HTML and body heights to 100%

I've been struggling to set my wrapper to 100% height, despite setting the height to 100%. Any solutions? At the moment, my main_wrapper is empty and should have a background color of red. While I'm looking to add a footer using position: fixed ...

What is the best way to automatically direct users to their profile page after logging in?

After logging in, I need to redirect users to their profile page. The issue I'm encountering is figuring out how to pass the user_id into the URL for successful redirection. I've researched various solutions for this common problem without succe ...

Learn how to display specific text corresponding to a selected letter from the alphabet using Vanilla JavaScript

I am extracting text from a JSON file. My goal is to enable users to click on a specific letter and have the corresponding text displayed. For example, if someone clicks on 'A', I want to show the text associated with 'A'. An example of ...

Troubleshooting the issue with dynamically adding form fields in AngularJS

I am currently working on an online course application and facing an issue with adding form fields dynamically to include additional video lectures for each course. When I attempt to click on the "Add Another URL" button, nothing happens. https://i.sstatic ...

Names of VueJs components

Currently, I am coding in VueJS and encountering the following scenario <li @click = "selectedComponent = 'appManagment'"><i class="ion-clipboard"></i>Management</li> My goal is to have the name displayed as {{selectedCo ...

What is the best way to mark a MenuItem as active within a Material UI Drawer component?

Here is the code snippet I am working with: <Drawer docked = {false} width = {330} open = {this.state.drawerOpen} onRequestChange = {(drawerOpen) => this.setState({drawerOp ...

Pass the form data to the next page with javascript in HTML

While working on a website for a power plant, I encountered some issues that require assistance. The main problem is that our client does not want to set up a database on their server. This means I can only use html, javascript, and a bit of php. There is ...

Google App Engine displaying a blank screen error

I've been playing around with this concept where there's a table of 'events' on a '/search' page. When the 'GO' button of an event is clicked, it should increase the 'RSVP' count for that event and redirect ...

Managing the width of an HTML table column based on its text content dynamically

Here is an example of an html table : <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="480px" class="societe" ><div style="float:left;font-size:12px;padding-top:2px" ><?php echo $_SESSION ...

Can you align a row under its corresponding column in Bootstrap?

Hello, I'm facing a dilemma with my layout. I have a grid consisting of 2 Rows and 4 Columns (col-md-8 & col md-4 in each row). The structure is as follows: Within the container Row 1 Column 1 (md-8) Row 1 Column 2 (md-4) [Accordion] Row 2 Colum ...

Having trouble implementing styles for an element selected using the document.getElementsByClassName() method

In order to adjust the style of a dropdown menu, I need to change its top value to align it properly. The element is being generated by Drupal (a CMS tool) and is configured with classes for styling purposes in the admin view where content is authored. How ...

occupying half of the screen

Having trouble displaying two videos in an ionic grid. My goal is to have both videos take up the entire screen, with each occupying 50% height and 100% width. However, when I try to achieve this, the ion-row only takes up 50% of the screen and the video i ...

What is the best approach in Ruby for managing an unordered list with split columns and counting each

I am currently in the process of strategizing the layout for a webpage utilizing Bootstrap 4 within Ruby on Rails. Specifically, I need to create a sidebar submenu that can expand and collapse, feature bullet points, and include a counter. The desired out ...

extracting the identifiers and class names from an HTML document

I have successfully extracted tags from an HTML file using getElementByTagName. However, I am also interested in parsing the IDs and class names within the same HTML file. This is my current approach: $html = new DOMDocument(); $html->loadHTML ...

What is the best way to ensure the div expands to the width of the screen as it is being

What changes should I make to the CSS in order to adjust the div class="breadcrumbs" when the table width exceeds the screen length and the background does not extend all the way to the end? See the screenshot for reference. I want the dark blue bar to str ...

What is the best way to show the initial 20 words on the screen, followed by the next 20 words using

Within a single tag, I have a string as shown in the example below. My goal is to display the first 20-25 words on screen, then continue with the next set of words, and so forth. Once 20 words are displayed, the remaining text should be hidden. <p>Lo ...