Skip to content
Codewilds

Lesson 18 of 29 · Layout with Flexbox

CSS display Property: Block, Inline and Inline-Block

easy10 XP3 min read

Every element on a web page is a rectangular box, and the CSS display property decides how that box behaves: does it start on a new line, can you give it a width, is it shown at all? Before you build layouts with flexbox and grid, you need to know the two basic box types every browser uses: block and inline.

Block and inline elements

Most HTML elements are either block or inline by default.

Block elements such as <div>, <p>, <h1>, <ul>, <li>, <section> and <form> always start on a new line and stretch across the full width of their parent. width, height, margin and padding all work the way you'd expect.

Inline elements such as <a>, <span>, <strong>, <em> and <label> flow inside a line of text, like words in a sentence. They are only as wide as their content, they never start a new line, and the browser ignores width and height on them.

<p>This paragraph is a block. <strong>This is inline</strong> and <a href="#">so is this link</a>.</p>
<p>A second block always starts on a new line.</p>

<img> is a special case: an image sits in the line like inline content, but it does accept width and height.

Changing the display value

With the display property you can switch any element to a different box type:

nav a {
  display: block;        /* each link on its own line */
}

.button {
  display: inline-block; /* stays in the line, but accepts a size */
  width: 160px;
  padding: 12px;
}

li {
  display: inline;       /* list items flow side by side like text */
}

inline-block is the best of both worlds. The element stays in the line next to its neighbours, like inline content, but you can give it a width, height, padding and margin, like a block. That makes it a classic choice for buttons, tags and badges.

Hiding elements with display: none

display: none removes an element from the page completely. It isn't drawn, and the space it would take up closes as if the element weren't in the HTML at all:

.spoiler {
  display: none;
}

Compare this with visibility: hidden, which makes an element invisible but keeps its empty space. Use display: none for things that should truly disappear, like a closed menu or an answer that is revealed later.

Why layout starts here

Every layout technique in CSS is a value of display. In the next lessons you'll write display: flex, and later display: grid. Those values change how an element arranges its children: in rows, in columns or in a grid. Once you can picture block and inline boxes, it's much easier to see what flexbox and grid change.

Common mistakes

  • Setting a width on an inline element. span { width: 200px; } does nothing. Make the element inline-block or block first.
  • Expecting two <div>s to sit side by side. Block elements always start a new line. Putting blocks next to each other is exactly what flexbox (next lesson) is for.
  • Using display: none when you only want something invisible. A hidden element can't be seen or clicked, and screen readers skip it too. If it should keep its space, use visibility: hidden.

Assignment

The page has a menu, a quest with three tags and a spoiler paragraph. Write CSS so that:

  1. The links in .menu are block elements (display: block), so each link starts on its own line.
  2. Every .tag is an inline-block element (display: inline-block) with a width of 100px. The tags must still sit side by side inside their paragraph.
  3. The .spoiler paragraph is hidden with display: none.

The starter already colours the links and tags. Write your rules below the comment in the <style> element and watch the Preview change.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.

Keep learning

Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.

Lesson last updated on .

Write code ↓
HTML & CSS+10 XP

Lesson 18 is part of the full course. The first 7 lessons are free.

Premium lesson

Unlock the full Learn HTML & CSS course

  • All 29 lessons, bosses and quizzes — lifetime access
  • One payment of $35, no subscription
  • Finish within 30 days and claim $10.50 (30%) back
Play the 7 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.