Sub topic 2
Width, height, margin, and padding on block and inline boxes
Assume you know basic HTML, CSS selectors, and the CSS box model. The display value changes how an element participates in layout, so it changes which box-model properties have a visible effect.
Block and inline sizing
A block box starts on a new line. Its default width is auto, which usually fills the available width of its parent. Its height grows to fit its content. You can set both width and height.
An inline box stays within the current line of text. For example, span is inline by default. Its width and height come from its content. Setting width or height on an inline box normally has no effect. Horizontal margin and padding work, but vertical margin does not change the line's layout in the same way.
<!doctype html>
<html lang="en">
<head>
<style>
.block {
display: block;
width: 12rem;
height: 3rem;
margin: 1rem 0;
padding: 0.5rem;
background: lightblue;
}
.inline {
display: inline;
width: 12rem;
height: 3rem;
margin: 1rem;
padding: 0.5rem;
background: lightgreen;
}
</style>
</head>
<body>
<div class="block">Block box</div>
<span class="inline">Inline box</span>
<span class="inline">Another inline box</span>
</body>
</html>Inline-block
inline-block combines both behaviors. The element sits beside other inline-level content, but its width, height, margin, and padding behave more like a block box.
This is useful for badges, compact buttons, and navigation links that should remain on one line while having a controlled clickable area.
Whitespace gaps
Whitespace in the HTML source becomes a small visible gap between inline-block elements:
<ul class="row">
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>The line breaks and indentation between the li elements create gaps. Use flexbox when you need exact spacing:
.row {
display: flex;
gap: 0;
list-style: none;
padding: 0;
}Baseline alignment
Inline-block elements align on the text baseline by default. Elements with different heights can therefore appear vertically uneven. Set vertical-align when you need another alignment:
.badge {
display: inline-block;
vertical-align: middle;
}Exercise
Why does width: 200px usually fail to resize a span?
Show answer
A span is inline by default, and inline boxes ignore author-set width. Use display: inline-block or display: block.