What exactly is the difference between the inline
and inline-block
values of CSS display
?
相关问题
- Adding a timeout to a render function in ReactJS
-
Why does the box-shadow property not apply to a
- Add animation to jQuery function Interval
- jQuery hover to slide?
- Issue with star rating css
One thing not mentioned in answers is inline element can break among lines while inline-block can't (and obviously block)! So inline elements can be useful to style sentences of text and blocks inside them, but as they can't be padded you can use line-height instead.
All answers above contribute important info on the original question.
However there is a generalization that seems wrong.
It is possible to set width and height to at least one (I can think of) inline element.
Both accepted answers here and on this duplicate state that this is not possible but this seems not like a valid general rule.
Example?
display: inline;
is a display mode to use in a sentence. For instance, if you have a paragraph and want to highlight a single word you do:The
<em>
element has adisplay: inline;
by default, because this tag is always used in a sentence. The<p>
element has adisplay: block;
by default, because it's neither a sentence nor in a sentence, it's a block of sentences.An element with
display: inline;
cannot have aheight
or awidth
or a verticalmargin
. An element withdisplay: block;
can have awidth
,height
andmargin
.If you want to add a
height
to the<em>
element, you need to set this element todisplay: inline-block;
. Now you can add aheight
to the element and every other block style (theblock
part ofinline-block
), but it is placed in a sentence (theinline
part ofinline-block
).A visual answer
Imagine a
<span>
element inside a<div>
. If you give the<span>
element a height of 100px and a red border for example, it will look like this withdisplay: inline
display: inline-block
display: block
Code: http://jsfiddle.net/Mta2b/
Elements with
display:inline-block
are likedisplay:inline
elements, but they can have a width and a height. That means that you can use an inline-block element as a block while flowing it within text or other elements.Difference of supported styles as summary:
margin-left
,margin-right
,padding-left
,padding-right
margin
,padding
,height
,width