iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

CSS Inline-block

display: inline-block is the middle ground between inline and block. The element flows inline with text, but you can set its width, height, and vertical padding.

How it compares

inlineinline-blockblock
New line before/after?NoNoYes
Respects width / height?NoYesYes
Vertical margin/padding pushes neighbours?NoYesYes
Typical for…span, aButtons, nav links, badgesdiv, section

Three pill buttons in a row

Save Save & New Discard changes
Fig 1. Different-width pills line up horizontally because they're inline-block.
Note: Inline-block elements inherit a small gap from the whitespace between them in HTML. Either remove the whitespace, use comments, or move to Flexbox to control spacing precisely.

Example

Example
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Verdana, sans-serif; }
.box { background: #04AA6D; color: #fff; padding: 20px; border-radius: 6px; }
</style>
</head>
<body>

<h1>CSS Inline-block</h1>
<div class="box">Edit the CSS on the left, then click Run &raquo;</div>

</body>
</html>
Try it Yourself »

Exercise

Lay these tags out side-by-side while still being able to set width.

.tag { display: -block; width: 80px; }

Test yourself

Q1. Inline-block differs from inline because it…
Q2. Inline-block differs from block because it…
Q3. Why do inline-block elements sometimes have unexpected gaps between them?

Discussion

Loading…