I've got the follow HTML code:
<section class="container">
<div class="search"></div>
</section>
And these CSS rules:
*, *:before, *:after {
box-sizing: border-box;
}
body, section, div {
margin: 0;
padding: 0;
}
html, body { height: 100%; }
.container {
display: inline-block;
position: relative;
}
.search {
display: inline-block;
padding: 3em;
border: 1px solid black;
}
If you inspect the section
element, you'll see that it has 5px of size... I didn't establish the size. I leave that the browser calculates this automatically with the child's size.
Why this behavior?
The reason is because of the inline-block
that you are using for the container
and search
elements. The idea is that inline-block
treats the elements as text in a paragraph; that is inline
. So while the elements themselves are block
elements, the browser adds some space between the elements because of the whitespace you actually have in your HTML.
Just like how adding spaces in a p
tag puts spaces between words, adding whitespace between two inline-block
elements put spaces between the elements.
That being said, there are a few ways to fix this:
<section class="container"><div class="search"></div></section>
. (Warning: this is untested.)margin:-4px;
. (By default, the browser will apply 4px of margin space between inline-block
elements.) If you use em or a percentage, this could work with responsive designs.vertical-align
, then inline-block
is not always the way to go. Here's a good resource for that: http://phrogz.net/css/vertical-align/.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With