CSSLesson 6 of 10Jump to practice
The display property
Switch elements between block, inline, inline-block and none.
In the HTML course you saw that elements are either block or inline. The display property lets you change that.
| Value | Starts a new line? | Accepts width and height? |
|---|---|---|
block |
Yes | Yes |
inline |
No | No, it’s as big as its content |
inline-block |
No | Yes |
none |
The element is gone. It takes no space at all |
Later lessons add two more values, flex and grid, for layout.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
span { background: #ffe08a; width: 120px; height: 40px; margin: 4px; }
.block { display: block; }
.inline-block { display: inline-block; }
</style>
</head>
<body>
<p>inline (the default): width and height are ignored</p>
<span>One</span><span>Two</span><span>Three</span>
<p>block: each one on its own line</p>
<span class="block">One</span><span class="block">Two</span>
<p>inline-block: side by side, with a size</p>
<span class="inline-block">One</span><span class="inline-block">Two</span><span class="inline-block">Three</span>
</body>
</html>A horizontal menu
Menus are lists, and list items are blocks. inline-block lines them up in a row:
<!DOCTYPE html>
<html>
<head>
<style>
ul { list-style: none; padding: 0; margin: 0; background: #123b3c; }
li { display: inline-block; }
a { display: block; padding: 12px 16px; color: white; text-decoration: none; font-family: sans-serif; }
a:hover { background: #0b7a75; }
</style>
</head>
<body>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">Courses</a></li>
<li><a href="#">About</a></li>
</ul>
</body>
</html>Notice display: block on the links. It makes the whole padded area clickable, not just the text.
Hiding things
<!DOCTYPE html>
<html>
<head>
<style>
div { background: #cfeee9; padding: 8px; margin: 4px 0; font-family: sans-serif; }
.gone { display: none; }
.invisible { visibility: hidden; }
</style>
</head>
<body>
<div>Box 1</div>
<div class="gone">Box 2 (display: none)</div>
<div>Box 3</div>
<div class="invisible">Box 4 (visibility: hidden)</div>
<div>Box 5</div>
</body>
</html>Box 2 leaves no gap. Box 4 leaves an empty space where it would be.
Practice
Check what you just learned. Fill in the missing code, then answer the quiz.
Exercise 1Solved
Hide the pop-up completely, so it takes up no space.
.popup { display: ; }Exercise 2Solved
Make the list items sit side by side, but still accept a width and height.
li { display: ; width: 100px; }It's a mix of the two display types from the HTML course.
Finish the practice to complete this lesson.
Sign in to keep your progress on any device.