I've recently been trying to figure out when it is appropriate to use inline-block
s. They seem to be far more useful than just a block
element. In fact, an inline-block element seems to be able to do anything a block element can do, but with a little extra styling.
Is there any reason an element with display: inline-block; width: 100%;
is any different than an element with display: block;
? (Aside from the fact that one is longer?)
I've been researching this topic by reading the w3c recommendation. I can't seem to find a difference.
The display: inline-block Value Compared to display: block , the major difference is that display: inline-block does not add a line-break after the element, so the element can sit next to other elements.
Block Elements occupy the full width irrespective of their sufficiency. Inline elements don't start in a new line. Block elements always start in a line. Inline elements allow other inline elements to sit behind.
Compared to display: inline , the major difference is that inline-block allows to set a width and height on the element. Also, with display: inline , top and bottom margins & paddings are not respected, and with display: inline-block they are.
inline-block It's formatted just like the inline element, where it doesn't start on a new line. BUT, you can set width and height values. block The element will start on a new line and occupy the full width available.
What you said is pretty much correct: "an inline-block element seems to be able to do anything a block element can do, but with a little extra styling." This is mostly due to the fact that the one thing both have in common is the fact that they are both block containers.
However, there's a catch.
A block box participates in a block formatting context, and an inline-block participates in an inline formatting context (although it establishes a block formatting context for its descendants, just like a block box does under certain conditions). See section 9.4. Basically, this means an inline-block is treated as though it were text, which in turn means most properties that usually apply to text would also apply to an inline-block. These properties include text-indent
, text-align
and vertical-align
, among others.
This can cause undesired side effects which you can easily prevent by not using display: inline-block
in the first place. See this question for an interesting example of what can happen.
The box model for inline-blocks also differs somewhat from that of block boxes. Section 10 contains all the nitty gritty details, but the main differences are:
Without the width: 100%
declaration, as you may have suspected, an inline-block will shrink to fit its contents.
Because an inline-block flows inline, you can't center it with auto left and right margins. You use text-align: center
instead. It goes without saying that it must then be on its own line with no text surrounding it on the same line, but if you're setting width: 100%
then this won't be a problem.
Inline-blocks are never affected by margin collapse.
If you're trying to create a block-based layout, you should be using display: block
. Generally speaking, when deciding between the two, you should always default to display: block
, and only choose display: inline-block
if you have a very good reason to (and no, blocking margin collapse is not what I would consider a good reason).
I'd echo everything said by @BoltClock; he makes a lot of good points.
I would also add to this that because an inline-block
is treated as text, the surrounding white space is also treated as text and thus comes into play in ways that it wouldn't for a block
element. This frequently catches people out when trying to use inline-block
for layout. This is probably the biggest 'gotcha' for using inline-block
.
Another slightly more subtle point applies specifically in your case, ie when setting width:100%
. In this case, you need to beware of which box model you're using, as the standard box model puts your borders, padding and margins outside of the element's width. Thus if you use borders, padding or margin your element will actually take up space a little bit more than 100% width.
This point applies equally to block
and inline-block
elements, but is more likely to occur with inline-block
because the difference is that block
doesn't normally need to be set to width:100%
because it defaults to expand to fill the width anyway, and without the box model causing it to go over the edge.
To avoid this, you could switch the box model by using box-sizing:border-box
, so that the borders etc are placed inside the box, and thus if you ask for with:100%
, that's what you'll get. See the MDN box-sizing page for more info.
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