What causes an element to gain an additional 1px of internal height when a border is applied?

Have you ever wondered why the apparent visual height of an element inside a border increases by 1 when a border is placed around it? The reported offsetHeight remains the same, equal to the desired height plus the border widths.

This issue becomes especially troublesome when trying to make an inner block element the same height as the outer element. You may notice that in some cases there is an extra 1px of height on the outer element, causing the inner element's background color to not fill the entire container:

#outer { height: 10px; width: 100px; border: 1px solid red; }
#inner { height: 10px; width: 50%; background-color: black; }
/* Setting #inner height to 100% has the same problem. */
<div id="outer"><div id="inner"></div></div>

Some devices may show a one-pixel-high gap between the black background and the red border.

https://i.sstatic.net/zkVkN.png

What causes this discrepancy, and how can you eliminate the "extra" bit of height inside the outer element so that the inner element occupies the full visual height of its container?

Could subpixel rendering be at play here?

Edit: This question isn't specifically about the box model. While understanding the box model, the concern is with the element being rendered with additional height inside its borders. Even Chrome's developer inspector may not display this extra height in its box model, but it's visible on screen.

Answer №1

By default, the box-sizing property of an element is set to content-box, which means that the width and height values only apply to the content box of the element.

Changing it to border-box will include the border in the calculation of the width and height as well.

#without,
#with {
  width: 40%;
  float: left;
  display: inline-block;
  margin-left: 2px;
}

#without .inner {
    height: 30px;
    border-bottom: 5px solid green;
    background: yellow;
}

#with .inner {
    height: 30px;
    border-bottom: 5px solid green;
    background: yellow;
    box-sizing: border-box;
}
<div id="without">
  <div class="inner">Without</div>
</div>

<div id="with">
  <div class="inner">With</div>
</div>

Answer №2

Web browsers calculate sizes starting with the content and then add padding and border (except for Internet Explorer, but that's a whole other story).

You have the option to change the box-sizing property to inform the browser that when you specify width: 100px, you actually mean "I want a total width of 100px including padding and border."

.outer { height: 50px; border: 10px solid red; }
.inner { height: 50px; width: 50%; background-color: black; }

.border * { box-sizing: border-box; }
.content * { box-sizing: content-box; }

.border, .content {
  margin: 1em 0;
}
<p>Content based</p>
<div class="content">
  <div class="outer"><div class="inner"></div></div>
</div>
<p>Border based</p>
<div class="border">
  <div class="outer"><div class="inner"></div></div>
</div>

When using box-sizing: border-box and both div elements having the same height, the inner one will overflow the outer due to the border being included in the width calculation.

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 to center align SVG with text in a unique way

Having trouble vertical aligning my inline SVG within a label. Any suggestions? This is how I've attempted it: <label for="menu-check-box">More<svg class="more-icon"><use xlink:href="#more-chevron"></use></svg></labe ...

Attempting to implement a secondary level of dropdown menu using CSS

As I begin, I want to mention that I have thoroughly reviewed all the sub sub menu questions, but unfortunately, I couldn't find anything that aligns with the code I currently have in place. Any assistance will be greatly appreciated. My current task ...

What is the best way to conceal floated elements with overflow in CSS?

How can I ensure that my block container element only displays the content of block elements, cutting off any floated elements that are taller than the parent container? Applying overflow: hidden seemed like a simple solution, but it created a new block f ...

The calendar on the Datetimepicker is not appearing in the proper position

I have encountered an issue while using Eonasdan bootstrap datetimepicker in a paramquery grid. Whenever I open the datetimepicker, the calendar appears hidden inside the grid. To tackle this problem, I added the following CSS condition: .dr ...

Sliding list in Jquery Mobile

I'm attempting to create a list using jQueryMobile similar to the one found in the Twitter application. Here's a video demonstrating what I'm aiming for: http://www.youtube.com/watch?v=l7gTNpPTChM However, I am facing two issues: 1) Each ...

Excessive Width Issue Caused by Bootstrap 4 Navbar Items

Having trouble with my Bootstrap 4 Navbar menu implementation. When I add more items, they overflow the container instead of floating correctly. Any CSS suggestions to temporarily fix this issue? <!DOCTYPE html> <html lang="en"> <head> ...

Guide to Designing a Three-Column Layout Using Only CSS

I'm currently facing an issue with the alignment of the content section on my webpage which consists of three columns. The DIVs are not floating as expected. Below is the code I am using: <div id="content"> <asp:ContentPlaceHolder ID="Ma ...

Creating interactive and adaptable maps

Currently, I am working on making a Joomla website responsive. However, I have encountered an issue with the SobiPro Map. The problem arises when displaying a map (background img) and points (a link + img) over it with an absolute position. As a result, wh ...

The appearance of HTML in JSP and CSS output in a Spring application is different from the local environment

For my web application's landing page, I created the design using HTML and CSS. Typically, I first design it on scratchpad.io before implementing it into my STS IDE. However, when I run the application, the output of the page appears different from th ...

How can I modify the CSS of every fourth element using jQuery?

Is there a way for jQuery to update the styling of every fourth item (div) within another div? I've been searching without success... Any guidance would be greatly appreciated :) ...

What is the best way to retrieve the color of a WebElement when dealing with a complex CSS structure in Webdriver

I am currently trying to determine if the color of a specific page changes immediately after clicking on a button designed to alter the color of an element within that page. My goal is to extract the RGB value stated as rgb(183,168,168); in this particul ...

Can you explain the meaning of 1__qem?

While looking at the default styles applied to HTML elements for Google Chrome, I came across this information on this page: p { display: block; -webkit-margin-before: 1__qem; -webkit-margin-after: 1__qem; -webkit-margin-start: 0; -web ...

Using FlexBox for Vertical Alignment of Elements

Experimenting with FlexBox (for demonstration purposes only). I am facing a challenge in vertically centering text within the parent .top-nav-bar. While using justify-content: space-between, I have successfully aligned two elements horizontally, but I am s ...

Finding out the exact number of pixels a user will scroll when using the mouse wheel in Javascript

Is there a way to calculate the number of pixels that will be moved by the vertical scroll bar when a user scrolls using the mouse wheel in JavaScript? In my situation, the user's scrolling will result in an offsetTop change between 200px to 1000px, ...

When hovering over items in the navigation bar, the entire bar expands seamlessly

I've spent countless hours trying to troubleshoot an issue with my dropdown menu that expands whenever I hover over it. The bar stretches out to accommodate the list of dropdown contents and then reverts back to normal when I move my cursor away. My a ...

Resizing the width to fit truncated content

When displaying text within a span element inside a fixed-width structure, I want to use ellipsis if the text overflows. To show the full text, I have initialized a Bootstrap tooltip in these elements. However, the tooltip is visually misplaced due to the ...

Constructing markup for text and subtext in a meaningful manner

I am in the process of creating a roster of Employees and their dependents on a webpage and I could use some guidance on the best way to structure this information. Here is an example of the content on my page: <div> John Smith Employee - 03/01/198 ...

What is the best way to incorporate TypeScript variables into CSS files?

In my Angular project, I am aiming to utilize a string defined in Typescript within a CSS file. Specifically, I want to set the background image of a navbar component using a path retrieved from a database service. Although I came across suggestions to use ...

Issue with IE7 causing rollover navigation blocks to disappear when hovering over content

While conducting browser testing on the website The website's top navigation menu includes rollover effects for building services, exterior services, and commercial categories. The CSS-triggered navigation works seamlessly in all browsers except for ...

Modify the background color of a particular TD element when clicking a button using JavaScript and AJAX

When I click on the "Active account" button inside the designated td with the <tr id="tr_color" >, I want the value "1" to be added to the 'active_account' column in the database. I have been successful with this functionality. However, I a ...