What is the reason for the presence of additional space below when using x-overflow:hidden?

When I place two spans inside each other and use overflow-x:hidden on the inner span, it results in extra space below the inner span. Why does this happen?

<span style="" class="yavbc-color-tip"><span style="">Some text</span></span>

To see an example, you can view the Fiddle here: http://jsfiddle.net/U92ue/

Please note that I have only tested this in the latest version of Chrome.

Answer №1

Visual formatting model - 9.4.1 Block formatting contexts

When floats, absolutely positioned elements, block containers (such as inline-blocks, table-cells, and table-captions) that are not block boxes, and block boxes with 'overflow' other than 'visible' establish new block formatting contexts for their contents.

It is important to note that a new block formatting context is established when the overflow property is changed. Normally, an element's vertical-align property value is baseline, but this can be altered to something like top to resolve any display issues.

See Updated Example Here

span.yavbc-color-tip span {
    display: inline-block;
    padding: 3px;
    border-radius: 8px;   
    border: none;
    background-color:#005e8e;
    color:#7cd3ff;
    overflow-x: hidden; /* This gives extra space under this span. Why? */
    vertical-align:top;
}

This issue does not occur when the element's display is not set to inline-block. It only happens with inline elements - see this example for clarification.

Answer №2

Contrary to what is mentioned in the accepted answer, the behavior discussed here is not related to Block Formatting Context or the "block" aspect of the inline-block value. It primarily concerns the "inline" component.

All inline-* elements contribute to the Inline Formatting Context. This implies that they are placed within "line boxes" alongside text and other inline-level elements. The alignment of these elements and text is such that the height of each line box is calculated from the highest element's top to the lowest one's bottom.

By default, inline-level elements align with the baseline of their fonts (as seen in the first line of the example below). Even if the parent element lacks actual text, the baseline's position and the minimum line box height are computed as if there were text present (referred to as the element's "strut"). Consequently, there is always some space above the baseline for ascenders and diacritics and below it for descenders, as illustrated in the second line.

The complexity arises for inline-block elements due to the overflow property altering what constitutes the baseline for these elements (referenced in section 10.8.1 of the spec):

An 'inline-block' element's baseline is its last line box's baseline in normal flow, unless it lacks in-flow line boxes or has a computed 'overflow' value other than 'visible', in which case the baseline becomes the bottom margin edge.

Hence, even though the space below the baseline is always reserved technically, with the default overflow: visible, the inline-block element positions itself such that its text's baseline aligns with the parent's baseline while its lower part extends beneath it. Though this setup often makes the inline-block the line's lowest element, obscuring space reserved for font descenders, adjusting the overflow reveals this space entirely.

p {
  margin: .5em;
  font: 32px/1.5 serif;
  color: #fff;
  background: #888;
}

span {
  display: inline-block;
  font: 12px/1 sans-serif;
  background: #fff;
  color: #000;
  padding: 2px;
  border: 1px solid green;
}

.ovh {
  overflow: hidden;
  border-color: red;
}
<p>Text with image <img src="http://via.placeholder.com/30x15"> and <span>two</span> <span>inline-block</span>s</p>

<p><img src="http://via.placeholder.com/30x15"> <span>only</span> <span>inline-blocks</span></p>

<p><img src="http://via.placeholder.com/30x15"> <span>regular</span>, the <span class="ovh">overflowed</span> trick</p>

Understanding inline formatting can be challenging. For further insights into its nuances and surprises, check out this article: .

A sensible guideline would be to avoid using display: inline-* solely for side effects unless you plan on incorporating the element within text. In the scenario described by OP, switching the inner span to display: block presents a straightforward solution without intricate line box calculations.

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

How can you use PHP to dynamically create a table with custom row sizes?

Trying to brainstorm a simple way to generate an HTML table with rowspans using PHP. Ideally, I want it to resemble the following structure, with each number representing the cell's rowspan: Upon observation, I've identified a potential pattern ...

Tips on invoking a factory method from a different service method in AngularJS

I have a factory method that goes like this.. (function (angular) { "use strict"; angular .module('app') .factory('UserService', ['$rootScope', '$q', function ($rootScope, $q) { ...

Prevent users from saving changes made to dropdown menu values (as well as other user-inputted content) using Inspect Element on the website before it

Recently, I started testing my website for bugs and stumbled upon a major issue that caught me by surprise. I never realized that changes made with Firebug or similar plugins on websites could actually be saved and implemented. Despite my efforts to preve ...

Div element to animate and vanish in a flash

I'm attempting to create a smooth slide effect for a div element before it disappears. Below is the code I am currently using: function slideLeft(element) { $("#" + element).animate({ left: "510" }, { duration: 750 }); document.getEle ...

Using Javascript to Treat an HTML Document as a String

Check out the complete project on GitHub: https://github.com/sosophia10/TimeCapsule (refer to js/experience.js) I'm utilizing JQuery to develop "applications" for my website. I'm encountering a problem where I can't locate the correct synta ...

When a link is clicked, submit a form and send it to several email addresses simultaneously

My form has been styled using the uniform jQuery plugin. Instead of using an input type submit for submitting the form, I have utilized a link and added some effects to it to prevent it from being formatted with uniform. <form> <ul> ...

Creating a cutoff with CSS: A step-by-step guide

Is there a way to create something similar using only CSS? https://i.stack.imgur.com/N9c6W.png I have no clue on how to achieve this. Any assistance would be greatly appreciated. ...

Utilizing XPath in Selenium: Extracting text from elements based on their adjacent elements

I'm facing an issue where I am unable to retrieve the text value from one end. Here is a snippet for better understanding: <div class=""> <span class="address_city">Glenwood</span> <span class="address_state">GA</span> ...

How to Insert a Background Image into Advanced Custom Fields (ACF) using CSS

Hey there, I'm facing a bit of a challenge with this particular section. In the past, I've only included ACF PHP within HTML when the background image was directly in the HTML code. Now, however, I have two shapes specified in my CSS using pseudo ...

Moving specified data from one interactive table to another interactive table

In my setup, there are two tables - one for products and another for customers: <table cellpadding="0" cellspacing="0"> <thead> <tr> <th>Product Code</th> </tr> </thead> ...

Loading tab content on click using jQuery

These tabs have a chrome-like appearance. When the first tab (Facebook) is clicked, it shows Facebook content. Clicking on the second tab (Twitter) displays the content of the first tab. Tabs three, four, and five show their respective contents without any ...

Assistance required: The database is not found upon page reload. How can this be resolved?

My HTML5 code has created a database with create, add, delete, and print operations. However, the ObjectStore is re-created every time I load the page, and the additional values stored are not present upon reloading the page. What could be wrong with my ...

Encountered an error while trying to download a PDF document in React

I'm currently working on adding a button to my website portfolio that allows users to download my CV when clicked. The functionality works perfectly fine on my localhost, but after deploying it to AWS Amplify, I encountered an error. The error occurs ...

What is causing Microsoft Edge to highlight this span and behave as though it's a hyperlink?

I am currently in the process of redesigning a website, and I've encountered an issue with the header markup. While the design looks fine on most browsers, it behaves strangely in Microsoft Edge on Windows 10. The phone number in the header is inexpli ...

Does text disappear when hovered over in CSS?

Having some trouble getting a text to display on hover over the parent container while adjusting the opacity. Despite trying multiple methods, it just won't show up no matter what I do! Here is my current code: .project{ position: relative; ...

"PHP MySQL news error" - An unexpected error occurred

Just incorporated a news system using MySQL/PHP, it functions flawlessly; however, encounters errors when inserting HTML. For instance, inserting a YouTube video results in the addition of / or \ Can someone advise on what changes need to be made in ...

Tips for creating semi-circular shapes using CSS gradients

I am trying to design a div element with a background made up of two colors divided by a half circle: My preference is to use gradients for easier animation purposes. I am looking to avoid using pseudo-elements or border-radius, and my HTML structure limi ...

What impact do varying screen sizes have on the width of CSS?

Can someone explain what this CSS code actually does: .class { width: 800px; } I've noticed that on my laptop screen, the element appears to be exactly 800 pixels wide. However, when I view it on my tablet screen, it shows up as 1600 pixels wide. Th ...

What is the reason behind the Chrome icon flickering momentarily while loading certain web pages?

I recently put together a basic html document, here it is: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8> <title>Just a test.</title> </head> <body> <svg ...

Show me all the posts from the database in a single row

When attempting to display all the entries from my posts table in the database, I noticed that only one entry is showing up. If I try to duplicate the code, it simply repeats the same ID from the posts table. <div class="row"> <?php $query ...