display: inline-block brought a new way to create side by side boxes that collapse and wrap properly depending on the available space in the containing element. It makes layouts that were previously accomplished with floats easier to create. No need to clear floats anymore.
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.
Now, the difference between display: inline-block and display: block is that, with display: block, a line break happens after the element, so a block element doesn’t sit next to other elements. Here are some visual examples:
Notice here how the width and height are not respected, and how the padding top and bottom are present, but overlap over the lines above and under.
span.box {
display: inline; /* the default for span */
width: 100px;
height: 160px;
padding: 18px;
}
Cheese and wine ricotta danish fontina. Brie cheesy grin paneer squirty cheese taleggio cheesecake goat taleggio goat taleggio. Bavarian bergkase emmental fromage cheesecake cheese slices cheesy grin queso caerphilly.
Here the width, height and padding are respected, but the two copies of the element can still sit side by side.
span.box {
display: inline-block;
width: 100px;
height: 160px;
padding: 18px;
}
Cheese and wine ricotta danish fontina. Brie cheesy grin paneer squirty cheese taleggio cheesecake goat taleggio goat taleggio. Bavarian bergkase emmental fromage cheesecake cheese slices cheesy grin queso caerphilly.
Here again everything is respected, but the elements don’t sit side by side.
span.box {
display: block;
width: 100px;
height: 160px;
padding: 18px;
}
Cheese and wine ricotta danish fontina. Brie cheesy grin paneer squirty cheese taleggio cheesecake goat taleggio goat taleggio. Bavarian bergkase emmental fromage cheesecake cheese slices cheesy grin queso caerphilly.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
You might want to create a seperate span element with features such as a border of 1px dashed blue; together with the default features.
Possibily create a div of a box class and place the span elements in their respective positions in the paragraph.
Create a P type selector with a border of 1px dashed lightgreen and maybe with a width of 300px.
So inline-box basically gives elements the positioning of inline and the padding of block?