Child element matching Parent's dimensions causes overflow

I've been searching extensively on Google, but I couldn't find an answer to why children of a parent with the same size are overflowing.

For instance,

<div style="background:blue;width:500px;height:40px">
      <div style="border:1px solid red;height:40px;font-size:0px;display:inline-block;width:30%;">
      </div>
      <div style="border:1px solid black;height:40px;font-size:0px;display:inline-block;width:70%;">
      </div>
    </div>

The code displayed above demonstrates that the first child div tag has a height of 1px more than its parent. Additionally, even when both children's percent widths add up to 100%, the parent still struggles to accommodate the two divs.

Answer №1

Understanding the Box Model

When working with CSS, it's important to consider the box model and how borders can affect the size of your elements. Most developers now use box-sizing as a default setting.

* {
  box-sizing: border-box
}

To save yourself some headaches, I recommend familiarizing yourself with the box model.

Let's look at an example:

<div style="background:blue;width:500px;height:40px">
      <div style="box-sizing:border-box; border:1px solid red;height:40px;font-size:0px;display:inline-block;width:30%;">
      </div>
      <div style="box-sizing:border-box; border:1px solid black;height:40px;font-size:0px;display:inline-block;width:70%">
      </div>
    </div>

Tackling Inline-Block Elements

Dealing with issues related to whitespace and inline-block elements is a common challenge in web development. It's often easier to utilize flexbox or floats instead.

Using Flexbox

<div style="background:blue;width:500px;height:40px;display: flex;">
  <div style="box-sizing:border-box; border:1px solid red;height:40px;font-size:0px;width:30%;">
  </div>
  <div style="box-sizing:border-box; border:1px solid black;height:40px;font-size:0px;width:70%">
  </div>
</div>

Using Float Left

<div style="background:blue;width:500px;height:40px;/* display: flex; */">
  <div style="box-sizing:border-box;border:1px solid red;height:40px;font-size:0px;width:30%;float: left;">
  </div>
  <div style="box-sizing:border-box;border:1px solid black;height:40px;font-size:0px;width:70%;float: left;">
  </div>
</div>

Working with Inline-Block

If you must use inline-block, be prepared for some workarounds that may not completely solve your issues.

<div style="background:blue;width:500px;height:40px">
  <div style="box-sizing:border-box; border:1px solid red;height:40px;font-size:0px;display:inline-block;width:30%;">
  </div><!-- commenting out carriage return
  --><div style="box-sizing:border-box; border:1px solid black;height:40px;font-size:0px;display:inline-block;width:70%">
  </div>
</div>

Answer №2

Remember to take into account borders. In addition, the children are styled as inline-block, so they will have their own width based on the space between them. Apply box-sizing: border-box and eliminate any unnecessary spaces between blocks:

div {
  box-sizing: border-box;
}
<div style="background:yellow;width:500px;height:40px">
  <div style="border:1px solid red;height:40px;font-size:0px;display:inline-block;width:30%;">
  </div><div style="border:1px solid blue;height:40px;font-size:0px;display:inline-block;width:70%;">
  </div>
</div>

Answer №3

Can you please review the following code snippet:

<div style="background-color: blue; width: 500px; height: 41px">
  <div style="border: 1px solid red; height: 40px; width: 29%; float: left;">
  </div>
  <div style="border: 1px solid black; height: 40px; width: 70%; float: left">
  </div>
</div>

Answer №4

If you want to fix this issue, make sure to include box-sizing: border-box in the body tag and white-space: nowrap; in the parent element. Additionally, modify the display: inline-block property of the children.

For example:

<div class="parent">
   <div class="child first"> </div>
    <div class="child second"> </div>
</div>

CSS:

body {
 -webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
 -moz-box-sizing: border-box;    /* Firefox, other Gecko */
  box-sizing: border-box;         /* Opera/IE 8+ */
}
.parent {
  background:blue;
  width:500px;
  height:40px; 
  white-space: nowrap;
}
.first {
  width:30%;
  border:4px solid red;
  display:inline-block;
}
.second {
  width: 70%;
  border:4px solid pink;
  display: inline-block;
}
.child {
 height:40px;
 font-size:0px;
 display:inline-block;
}

Here is an example: https://jsfiddle.net/_ahmed_ab/4301hwmc/1/

Answer №5

One reason for this issue is the box-sizing CSS property. The default value is content-box, which means the element's width and height include only the content, not its padding or border.

To fix this, you can set box-sizing: border-box on the div so that the padding and border are included in the width and height of the element.

The reason your divs are not side by side is due to using display:inline-block. This creates spaces between elements, similar to words when typing. To remove the space, you can either adjust the code to eliminate gaps between the divs or set font-size:0 on the parent div and then reset font-size: initial on the child elements.

An alternative solution is to use Flexbox if browser support allows.

* {
  box-sizing: border-box;
}
<div style="background:blue;width:500px;height:40px;font-size:0;">
  <div style="border:1px solid red;height:40px;font-size:0px;display:inline-block;width:30%;">
  </div>
  <div style="border:1px solid pink;height:40px;font-size:0px;display:inline-block;width:70%;">
  </div>
</div>

Or another option is to utilize Flexbox:

* {
  box-sizing: border-box;
}
<div style="background:blue;width:500px;height:40px;display:flex;">
  <div style="border:1px solid red;height:40px;font-size:0px;display:inline-block;width:30%;">
  </div>
  <div style="border:1px solid pink;height:40px;font-size:0px;display:inline-block;width:70%;">
  </div>
</div>

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 I dynamically swap one div with another within the same div using Javascript?

I have a piece of code that I am working with, and I need it to replace every div element whenever a specific div is clicked. function updateContent(target, replacementID) { document.getElementById(target).innerHTML = document.getElementById(replaceme ...

Tips on incorporating animations into a class design?

I'm trying to figure out how to make an image disappear by adding a class However, when I try this, the element vanishes without any animation I'd like it to fade away slowly instead I've heard there are CSS3 properties that can help achi ...

Any tips on creating a smooth fade effect for Bootstrap tabs when sliding to the edges on smaller screens?

Is it possible to make the text fade when it reaches the red highlighted area and also on the search icon on the left? These tabs slide left and right on smaller screens as they are bootstrap tabs. I attempted to add opacity to the text at (min-width: 320p ...

The minimum height increases as the inner divs expand

Can you help me with the code below? html <div class="content"> <div class="col"> </div> <div class="col"> </div> <div class="col"> </div> <div class="col"> </div> ...

Utilizing Node.JS Nodemailer for sending emails with extensive HTML code

I am encountering an issue. I am working with Node.JS nodemailer. const url = `${url}`; var mailOptions = { from: 'stackoverflow', to: email, subject: 'test', html: 'Please click this email to confirm your email: &l ...

Do not apply hover effect to a particular child div when the hover effect is already applied to the parent

I am encountering a problem with the hover effect. I have 3 child div's structured as follows: <div class="parent"> <div class="child1">A</div> <div class="child2">B</div> <div class="child3">C</div ...

Is there a way to incorporate an animated element within the track of my circular progress bar?

My circular progress bar features two paths, with one path increasing in length as data is received, eventually turning the entire circle red. Here is the SVG HTML code: <path d="M 50,50 m 0,-47 a 47,47 0 1 1 0,94 a 47,47 0 1 1 0,-94" stroke="#A9B0B7" ...

Automatically inserting a row into an HTML table

My table structure looks like this: <form name="frm_data_nasabah2" method="post" enctype="application/x-www-form-urlencoded" action="<?php echo $page_action;?>"> <table class="table table-bordered table-hover" id="tab_logic_ ...

Unique style CSS (Flex)Grid with spacing and uniform aspect ratio

Hello everyone, typically I'm quite skilled when it comes to CSS but this particular challenge has pushed me to my limits. I have a desire to create a grid where all elements maintain the same aspect ratio (with images that already share this aspect ...

Maintaining equal height for two columns using CSS techniques

I'm facing a challenge with two columns in a container, both having dynamic heights and different color backgrounds. I want the columns to always be the size of the taller one regardless of the content. How can I achieve this using CSS? Here is the H ...

Customize the Address Bar with Google Extension

I recently developed a unique Chrome extension that modifies the appearance of the new tab page by incorporating my site's index.html. However, I am facing an issue where the address bar displays https://example.com/index.html/. Is there a way to have ...

Efficiently Displaying Multiple HTML Elements in React Native WebView

I am currently working on developing an Epub reader in React Native and facing a challenge. I need to find a way to efficiently transfer multiple html content files from the Epub container to the webview in order to achieve seamless pagination. ...

Achieve overlapping divs using the Grid Layout

My objective is to have the div overlap the next row(s) without pushing them down when it increases in size. I attempted to use position: absolute; but this caused issues with the Grid Layout structure. Preserving the shape of the grid is crucial to me ...

The MUI Modal is too large for the display

I'm using a Modal component from MUI in my project and I am facing an issue with displaying a large JSON object inside the modal. The problem is, the top of the modal extends beyond the screen height and even after making it scrollable, I am unable t ...

Adding the active class in Bootstrap can be easily achieved in CodeIgniter by using the

I am seeking guidance on how to add an active class in CodeIgniter when dividing the view into three separate files. These files include: header.php page1.php ... page10.php footer.php According to this thread, there seems to be no clear solution prov ...

Incorporating and verifying unseen reCAPTCHA

I'm looking to implement reCAPTCHA on a form. After registering, I obtained the necessary keys. <form> <input type="text" name="name" /> <input type="email" name="email" /> <textarea name="message"></textarea&g ...

Assess a nested variable using Thymeleaf syntax

My current task involves receiving bin data in JSON format. The number of bins in each section varies, like this example: var jsonmsg={"1": "SIXTY", "2": "DISCONNECTED", "3": "TWENTY", "4&quo ...

Creating a Functional Dropdown Menu with CSS and HTML

I recently created a test site without a drop-down menu, and here is how it looks: Check out my site here: Now, I want to add a simple drop-down menu for "serwery". Can anyone help me figure out what's wrong with my code? Here's the HTML: < ...

Utilize the cakePHP HTML input helper to assign specific values from 2 arrays

I am working on creating 2 arrays from a model for an edit page. One array contains values already selected by the user, while the second array contains available values that the user can choose from. My goal is to populate a multi-select input box with ...

Combining CSS3 transform scale with jQuery UI resizable for enhanced functionality

I am currently working on factoring in the impact of scaling an element while utilizing jquery ui resizable. Although there have been some discussions regarding css3 transform issues with resizing/dragging, I have not come across a solution that works eff ...