Introduction
These three display types control how elements appear and behave on a webpage. Knowing the difference helps you arrange content clearly and nicely.
Jump into concepts and practice - no test required
These three display types control how elements appear and behave on a webpage. Knowing the difference helps you arrange content clearly and nicely.
selector {
display: block | inline | inline-block;
}block makes the element a block box.
inline makes the element flow inside text.
inline-block makes the element flow inline but behave like a block box.
p {
display: block;
}a {
display: inline;
}button {
display: inline-block;
width: 8rem;
height: 2.5rem;
}This page shows three types of display:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Block vs Inline vs Inline-block</title> <style> .block { display: block; background-color: #a0d8f0; margin-bottom: 0.5rem; padding: 0.5rem; } .inline { display: inline; background-color: #f0c0a0; padding: 0.5rem; } .inline-block { display: inline-block; background-color: #a0f0c0; width: 8rem; height: 2rem; line-height: 2rem; text-align: center; margin-right: 0.5rem; } </style> </head> <body> <div class="block">I am block - full width, new line</div> <span class="inline">I am inline - flow inside text</span> <span class="inline">I am also inline</span> <br /> <div> <div class="inline-block">Inline-block 1</div> <div class="inline-block">Inline-block 2</div> <div class="inline-block">Inline-block 3</div> </div> </body> </html>
Block elements always start on a new line and take full width by default.
Inline elements do not accept width or height properties.
Inline-block elements combine inline flow with block box features like width and height.
Block: New line, full width.
Inline: Flows inside text, no width/height.
Inline-block: Side by side, can set size.
inline-block with a hyphen.display: block; sets block, not inline-block. block-inline and inlineblock are invalid.<div class='box'>Box 1</div>
<div class='box'>Box 2</div>
<style>
.box { display: inline-block; width: 100px; height: 50px; background: lightblue; margin: 5px; }
</style><button class='btn'>One</button>
<button class='btn'>Two</button>
<button class='btn'>Three</button>
<style>
.btn { display: inline; width: 100px; height: 40px; }
</style>display: inline causes buttons to ignore width and height, so they don't size as expected.display: inline ignores width and height properties. -> Option B