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

CSS Display

The display property is the master switch for how an element participates in layout. Change it and the rest of CSS behaves differently for that box.

The values you reach for most

ValueBehaviourTypical for
blockStarts on a new line, fills the available width, respects width/height.div, section, p.
inlineFlows inside text, ignores width/height, vertical padding doesn't push siblings.span, a, em.
inline-blockFlows inline but respects width/height.Nav links, buttons.
flexChildren lay out along an axis (see Flexbox).Toolbars, cards in a row.
gridTwo-dimensional layout (see Grid).Page layouts, image walls.
noneRemoves the element from the layout entirely — no box, no space.Hiding via JS state.
contentsRemoves the box but keeps the children visible.Restructuring without an extra wrapper.

block vs inline at a glance

display: block display: inline
Fig 1. Block elements stack; inline elements flow horizontally.
Tip: To hide something but keep it in the layout (still occupying space), use visibility: hidden. display: none removes the box entirely.

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 Display</h1>
<div class="box">Edit the CSS on the left, then click Run &raquo;</div>

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

Exercise

Turn this list item into a block so width and height take effect.

li.tile { display: ; width: 200px; }

Test yourself

Q1. Which value removes the element from layout entirely?
Q2. Which value lets you set width and height, while still flowing inline?
Q3. Default display for `<span>` is…

Discussion

Loading…