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>