thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

05.01 · Advanced CSS / Learn + practice · 20 min + video

Compare block, inline and inline-block

Why did that word jump onto its own line? Use display to understand the difference between a block, an inline word and a sized inline-block label.

Activity 1 of 5 in Advanced CSS

All courses

CSS Basics / 05.01

Compare block, inline and inline-block

Not started

See why some boxes stack, some stay in a sentence and others accept dimensions while flowing inline. Watch the video, explore the demo and practice in six guided steps.

More options

Your lesson progress

Reading & video: not startedDisplay labels practice: not startedDisplay repair: not started

Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

Rob compares stacked orange block boxes, blue inline text and sized purple inline-block boxes side by side.
Rob’s comic strip · Advanced CSS

ORIGINAL WALKTHROUGH · 9:36

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.

Open video & code editor (opens in a new tab)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

index.html
index.htmlhtml · example
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Display Property</title>
    <style>
      * {
        outline: 1px solid red;
      }
    </style>
  </head>
  <body>
    <h1>Display Property</h1>
    <p>
      The display property in CSS specifies the display behavior of an element.
      It can take values such as block, inline, and inline-block.
    </p>
  </body>
</html>

Display changes the box, not the meaning

The browser lays out HTML as boxes. The display property tells it how a box participates in layout. You can change a span’s display without turning that span into a heading, paragraph or section. Choose meaningful HTML first, then choose its visual behavior.

This lesson uses normal document flow. Flexbox and Grid change how their children are laid out, so they are intentionally absent from these examples.

Block: start a new line

A block box starts on a new line in normal flow. With an automatic width, it normally fills the available width of its containing block. That might be a small section, not the entire browser.

You can set its width and height. A block with a smaller width still starts on a new line, even if another block could fit beside it. Paragraphs, headings and div elements normally start as blocks.

Two block notices start on separate lines
index.htmlhtml · example
<div class="notice">Bring a notebook.</div>
<div class="notice">Take a break.</div>

Inline: highlight a word without splitting the sentence

An ordinary span starts inline. It flows with neighboring words rather than forcing a block-level line break. Use it to style a word or short phrase while keeping that text in its sentence.

For an ordinary non-replaced inline span, width and height do not size its box. A blue word can remain small even when those declarations are in your CSS. That does not mean the stylesheet is disconnected. This rule is about text spans, not every inline element, such as an image.

A span highlights part of a sentence
index.htmlhtml · example
<p>Bring a <span class="highlight">curious</span> mind.</p>

Inline-block: a sized box within the line

Use display: inline-block when a small label should participate in the line but also accept width, height and box spacing. Several labels can sit side by side when the line has enough room. They can wrap when the line is too narrow.

Do not use this as a promise that everything will always stay on one row. Later, Flexbox and Grid will give you more tools for whole layouts. Here we are learning the behavior of individual boxes.

A sized label without a flex parent
styles.csscss · example
.tag {
  display: inline-block;
  width: 96px;
  height: 32px;
}

Explore one change in the preview

Use Next to switch the same blue label between the three display values. The highlighted declaration is the only change. Watch whether the sentence splits and whether the requested 100px by 60px box becomes visible.

One span, one sentence and the same requested dimensions. Switch display to compare normal-flow layout. No Flexbox or Grid parent.

index.htmlhtml · example
<p>Pick a <span class="label">study</span> partner.</p>
styles.csscss · example
.label {
  background-color: #b9e8fa;
  width: 100px;
  height: 60px;
  display: inline;
}

YOUR PAGE PREVIEW

Inline: no forced new line; these dimensions are ignored

Stage 1 of 3

Inline follows the sentence

The blue study label stays in the sentence. Its width: 100px and height: 60px declarations do not size this ordinary inline span. The blue background hugs the word.

Use Next to explore at your pace, or Play demo for a five-second pause between stages.

Your turn: arrange study-session labels

Practice with orange notices, one blue word in a sentence and two purple snack labels. You will compare line breaks, try dimensions on an inline span, then give the snack labels sized boxes and spacing.

The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.

Start mini-lesson(opens in a new tab)

6 steps · Saved work · Instant feedback

Padding is inside; margin is outside

Padding adds room inside the colored box; margin separates it from neighboring boxes. In the default content-box sizing used in this practice, a width of 100px plus 8px of padding on each side produces a box 116px wide, before borders. Margin-right adds outside space beyond that box.

Inline vertical padding can paint outside a text line without making the line taller in the way you expect. When the exercise needs a predictable sized label, use inline-block.

Debugging task: repair the study-room labels

The page below has two wrong display values. The highlighted word splits its sentence, and the purple labels ignore their dimensions. Change only the two display declarations named in the task checklist. The checks measure both labels, including their visible box dimensions.

YOUR TURN

Repair the study-room labels

Two display values are wrong. Repair the sentence and the sized labels in styles.css.

To pass this challenge
  • In styles.css, find the rule with selector .highlight. Change its display property to inline so curious stays in the sentence.
  • Find the rule with selector .label. Change its display property to inline-block.
  • Keep the label width at 100px and height at 40px.
  • Keep the supplied page content, colors and stylesheet link.

Edit styles.css and watch the preview update after you pause typing. Open index.html to inspect its stylesheet link. Run or Check saves immediately. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Need a nudge?

Fix the sentence first

The highlighted word is an ordinary span. Inline keeps it in the sentence without forcing a new block line.

Keep a sized box in the line

Inline-block accepts the supplied width and height. Inline does not size an ordinary span, while block forces a new line.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

Choose display for the behavior you need. Blocks create a new line, ordinary inline spans follow the text without accepting width or height, and inline-block gives a box dimensions within inline flow.