What is causing an increase in height for an inline-block element when using overflow: hidden?

When exploring this specific example, the mysteries of CSS and HTML unfold...

HTML Structure

<body>
  <div>
    <div>foo bar</div>
  </div>
</body>

CSS Style Rules

body, html, div {
  height: 100%;
  margin: 0;
}
div div {
  display: inline-block;
  overflow: hidden;
}

Curiosity arises as to why setting overflow: hidden results in a mysterious vertical scrollbar. Furthermore, where does this set height go unnoticed on the page? It almost seems like an invisible boundary.

The deliberate use of 100% height for all elements aims to expand the innermost div to fill the entire viewport. This behavior appears to be successful until the interference of overflow: hidden. But why?

Answer №1

The additional height corresponds to the difference in height between vertical-align: baseline and vertical-align: bottom. This is known as the "descender line." The seemingly arbitrary "5 extra pixels" can be attributed to this factor. If the font size is increased by a factor of 10, this gap will also increase by 10 times.

It appears that in cases where overflow: hidden is not applied, the baseline of an inline-block element aligns with the baseline of its last line of text.

This observation suggests that overflow: hidden actually enforces the baseline of the entire inline-block element to be at the bottom of the element. Even in the absence of visible text, the parent container of the inline-block reserves space for the descender line. In the provided example, due to the container having a height: 100%, the overflow of the descender line remains unseen.

The persistent presence of this space, despite no visible text being present, may be due to the fact that the inline-block element establishes an inline formatting context, leading to this occurrence. Had the element been a block, such a context would only be created upon encountering inline elements or text.

While just a hypothesis, this explanation seems plausible. It also clarifies the effectiveness of @Jonny Synthetic's resolution: by adding overflow: hidden to the parent container, the surplus space assigned for the descender line is concealed.

Credit goes to @Hashem Qolami for providing the jsbins which inspired this theory.

Answer №2

Due to restrictions, I couldn't apply the overflow: hidden style to the parent div. Furthermore, it was ineffective within the structure of my HTML.

Fortunately, after reading a comment by @Tony Gustafsson in the original post, I discovered a solution:

div div {
    vertical-align: bottom;
}

Answer №3

To ensure that items with a height of 100% display correctly, it is important to include the overflow: hidden property in the CSS. The first rule in the CSS snippet targets the outermost div element, while the overflow hidden property should be applied to the inner div element.

I tested this CSS on Jsfiddle and it worked as expected:

body, html, div {
    height: 100%;
    margin: 0px;
    padding: 0px;
    overflow: hidden;
}
div div {
    display: inline-block;
    overflow: hidden;
}

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

enhancing the appearance of the initial sentence in the closing passage through CSS styling

Is there a way to make only the first sentence in the final paragraph bold using CSS? Specifically, I would like to add extra padding to the top of this last paragraph so that it wraps around an image. However, when I increase the bottom padding of the flo ...

Measuring the space between components

I am looking for a way to dynamically calculate distance on a canvas, similar to the example shown in the figure. I want to be able to drag the highlighted joints in order to adjust the span for calculating distances. Are there any plugins available for th ...

What is the best way to target all div elements containing a child with the class "test-class"?

How can you choose all div elements that contain a child element with the class "test-class"? This question has been identified as a duplicate of Is there a CSS parent selector?. After reading that question, it is evident that this question is indeed redu ...

JavaScript | Determining the coordinates of where on the image X and Y were clicked

My goal is to determine the position of an x and y location when a user clicks on an image. This information will be used to add a spot to the image that displays relevant information. The content for the spot will be dynamically loaded from a SQL database ...

The magic of coding is in the combination of Javascript

My goal is to create a CSS animation using jQuery where I add a class with a transition of 0s to change the color, and then promptly remove that class so it gradually returns to its original color (with the original transition of 2s). The code below illus ...

When selecting an option in the burger menu, the dropdown does not react properly

I am facing an issue with the burger menu where one of the options should trigger a dropdown, but the content is not adjusting properly. The dropdown menu should push the other content downward, but currently, it overlaps. I have included the code for th ...

Is it possible to maintain tab focus instead of visual focus when navigating past the skip navigation menu for improved accessibility?

My website features an accessibility skip navigation menu that utilizes named anchors to navigate to different content areas. These content areas, including the top menu, left menu, main body content, and footer menus, contain links within them. When I u ...

The webpage appears to be operating smoothly when tested locally and on JSFiddle, however it is encountering

Just joined and brand new to this game. Completed a course at work over the last few weeks, and I must say, I'm really enjoying it. However, when I tried to create a page using Bootstrap styling and added an accordion and a carousel, both features fai ...

Displaying images in a horizontal row instead of a vertical column when using ng-repeat

I am currently using ng-repeat to showcase a series of images. While I believe this may be related to a CSS matter, I am uncertain. <div ng-repeat="hex in hexRow track by hex.id"> <img ng-src="{{hex.img}}"/> </div> https://i.sstatic ...

The navigation bar alignment to the right is malfunctioning

I'm puzzled as to why the navbar-right class is not properly closing out the two navigation bar elements on the right. Despite adding the correct code, it doesn't seem to be working as expected. <nav class="navbar fixed-top navbar-toggleable- ...

Tips on utilizing Twitter Boootstrap's form validation tooltip messages

Before anything else, let me clarify that I am not inquiring about creating tooltips from scratch using Bootstrap's javascript guide, which can be found in the documentation. I recently utilized type="email" with version v3.3.1. It automatically vali ...

How can I place my container above my header in the layout?

I'm facing difficulty positioning the container above my header. Here is an example of what I am aiming for: No matter what I attempt, strange occurrences like the NAV disappearing or divs overlapping keep happening. HTML: <!DOCTYPE html PUBLIC ...

Why is it that Lotus Notes is unable to render this HTML code properly?

I am having trouble with an email that displays properly on every platform except Lotus Notes. Can anyone provide insight as to why this email is not rendering correctly in Notes? Here is a screenshot: img (please note the images are for demonstration pu ...

The functionality of jQuery is not functioning properly on dynamically created identical divs

I am currently working on creating a comment reply section. I have managed to load the same div for reply that I use for commenting by utilizing $('#1').append($('.enterComment').html());. In this case, 1 represents the id of the div th ...

Arrange the HTML elements in a line, one following the next

I need assistance with aligning two dropdown lists in the jsbin provided. How can I position them one after the other, center them on the page, and ensure there is enough space between the two elements without manually adding spaces using   In additi ...

The input placeholder text in Safari 5.1.7 is unable to be centered

Check out my code snippet below: HTML: <input type="text" name="Surname" placeholder="Surname" id="surname"> CSS: ::i-block-chroms, input[placeholder]::-webkit-input-placeholder { text-align:center; } ::-webkit-input-placeholder { t ...

Easily automate button clicks on a new tab website

Seeking assistance below, following codes are sourced from "example.com" as assumed: <a href="http://www.example.org" target="vo1" onclick="gp(1)" rel="nofollow">Click Me</a> Upon clicking on ...

An issue occurred during the building of the module (from ./node_modules/sass-loader/dist/cjs.js) and resulted in

Within the app directory, I've created a new folder called styles > custom > loader.scss. In the styles.scss file, I included the following import: @import "styles/custom/loader.scss"; However, upon doing so, an error message appeared ...

Is there a way to modify the style value within AngularJS?

<div class="meter"> <span style="width: 13%"></span> </div> I have the following HTML code snippet. I am looking to update the width value using AngularJS. Does anyone have a solution for this issue? ...

Tips for positioning a mat-form-field beside an h1 tag

I've been attempting to place an h1 next to a mat-form-field from Angular Material, but I'm encountering some difficulties. Here's what I've attempted so far: <div class="mat-elevation-z8"> <mat-form-field> <mat-l ...