EthicsComputerCourses
Join training +91 90532 10052

Block vs Inline Elements

CSS · Lesson 46 of 240 Beginner 10 min read Updated

By the end of this lesson you will be able to predict how any block or inline box will behave before you run the page. You will know why width does nothing on a <span>, why padding on a link overlaps the line above, and how the browser builds each line of text. You will also change an element's display with confidence and read the newer two-value display syntax.

You already met block and inline boxes in CSS Display Property, and the HTML side in Block, Inline, div & span. This lesson goes under the bonnet.

Two ways to lay out boxes

The browser places boxes using formatting contexts. A formatting context is a set of layout rules for the boxes inside one area. Normal flow, the layout you get before Flexbox or Grid, has two of them.

Block formatting stacks boxes one below the other, like the floors of a building. Each block box:

  • starts on a new line and takes the full width of its container (when width is auto);
  • accepts width, height, padding, borders and margins on all four sides;
  • is separated from its neighbours by vertical margins, which can collapse (see Margin Collapse).

Inline formatting places boxes side by side, like words written along a ruled line in a notebook. Each inline box:

  • sits in the current line and is only as wide as its content;
  • wraps onto the next line when the line is full, and can split across lines;
  • follows the text rules: line-height, vertical-align and the font.

A paragraph is a block box on the outside, but its text and <span>, <a> and <strong> children are laid out with inline formatting inside it.

Line boxes

When the browser lays out inline content, it cuts it into line boxes. A line box is one horizontal row of text, like one ruled line in your notebook. Words and inline boxes fill a line box from left to right. When the next word does not fit, a new line box starts below.

The height of a line box comes from the content on that line:

  • every piece of text contributes its line-height;
  • a taller inline-level box, such as an image, makes that one line taller;
  • boxes are lined up on the baseline, the invisible line that letters such as "a" and "x" sit on.

The baseline explains a famous surprise. An image is inline by default, so it sits on the baseline. Below the baseline the browser keeps room for letter tails such as "g" and "y". That room shows as a small gap under the image. In Chrome, a 50-pixel image inside a <div> makes the <div> 56 pixels tall with default fonts. The fix is display: block on the image, or vertical-align: middle if it must stay inline.

What inline boxes ignore

This is the part that confuses beginners. On a normal inline box such as <span>, <a> or <em>:

PropertyEffect on an inline box
width, heightIgnored. The content decides the size
margin-left, margin-rightWorks, at the start and end of the element
margin-top, margin-bottomIgnored. No space is added
Left and right padding and borderWorks and pushes nearby words away
Top and bottom padding and borderPainted, but the line box does not grow, so it overlaps nearby lines

Why? An inline box is a piece of a sentence. If one word could demand 100 pixels of height, every line of text would become uneven. So the line height stays in charge, and vertical padding is drawn over the neighbouring lines instead.

When an inline box splits across two lines, its left padding and margin appear only at the very start, and its right ones only at the very end.

Step 1: see it happen

Run this school notice. The highlighted <span> has a width, a height, vertical margins and big padding.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Inline boxes and line boxes</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1.5rem; max-width: 32rem; }
    p { line-height: 1.6; background: #f3f6fa; }
    .date {
      background: #ffe08a;
      border: 2px solid #b8860b;
      width: 300px;          /* ignored */
      height: 80px;          /* ignored */
      margin: 40px 12px;     /* only left and right work */
      padding: 12px 6px;     /* vertical padding overlaps lines */
    }
  </style>
</head>
<body>
  <p>
    Kendriya Vidyalaya, Jaipur: the annual sports day is on
    <span class="date">Friday 12 December</span>
    at the main ground. Parents are welcome from 8 am. Students must
    wear house colours and bring a water bottle.
  </p>
</body>
</html>

What you should see: the yellow box ignores the 300px width and is only as wide as its text. At most widths it even splits across two lines, with the 12-pixel margin only before "Friday" and after "December". There is no extra space above or below. Its top and bottom padding spill over the lines around it, and the line spacing stays even.

Step 2: fix it by choosing the right display

If the date needs real vertical space, change its display. Add display: inline-block; to .date and run again. The width, height, margins and padding now all work, and the line grows to make room. You will study that value in Inline-Block Display.

If the date should sit on its own line, use display: block; instead. Now it starts a new line and fills the width of the paragraph, unless the 300px width limits it.

Changing display on purpose

Each element's default display comes from the browser's built-in style sheet. You can change it whenever the design needs a different behaviour. Two changes are very common.

Block links in a sidebar. A link is inline, so only its text is clickable. As a block, the whole row becomes the click target, which is much easier to tap on a phone.

Inline list items. List items are block (strictly, list-item). Making them inline puts them on one line, for a breadcrumb trail for example.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Changing display</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }
    .side { width: 14rem; border: 1px solid #ccd5e0; }
    .side a { padding: 0.6rem 0.8rem; color: #13315c; text-decoration: none; }
    .side a:hover { background: #dbe7f6; }
    .side.block a { display: block; }
    .crumbs { list-style: none; padding: 0; }
    .crumbs li { display: inline; }
    .crumbs li + li::before { content: " / "; color: #777; }
  </style>
</head>
<body>
  <h3>Inline links (hover the gaps)</h3>
  <nav class="side"><a href="#">Results</a> <a href="#">Timetable</a> <a href="#">Fees</a></nav>
  <h3>Block links</h3>
  <nav class="side block"><a href="#">Results</a> <a href="#">Timetable</a> <a href="#">Fees</a></nav>
  <h3>Breadcrumb with inline list items</h3>
  <ul class="crumbs"><li>Home</li><li>Class 10</li><li>Science notes</li></ul>
</body>
</html>

In the first sidebar the links sit side by side like words, wrapping when the row is full, and only the small area around each word is clickable. In the second, each link is a full-width row. The breadcrumb shows three list items on a single line, joined by slashes.

One warning: CSS changes how an element looks, not what HTML allows. A <div> with display: inline still cannot go inside a <p>. The HTML parser closes the paragraph before the <div>, whatever your CSS says.

Outer and inner display

display actually answers two questions:

  • Outer display: how does this box sit among its siblings? As block or as inline.
  • Inner display: how does it lay out its own children? With flow (normal flow), flow-root, flex or grid.

The one-word values you already know are short forms of a pair:

One wordTwo-value form
blockblock flow
inlineinline flow
inline-blockinline flow-root
flexblock flex
inline-gridinline grid

So inline-block simply means "inline on the outside, a self-contained block inside". Thinking in pairs makes new layout values easy to read. The two-value syntax is Baseline 2023 (Chrome added it last, in version 115), and Chrome returns the short form, such as inline-block, when you read the computed value. Most real code still uses the one-word values, which are fine. You will use the inner values properly in CSS Flexbox Introduction and CSS Grid Introduction.

Common mistakes

  • Giving a <span> or <a> a width or height. Nothing happens. Use inline-block or block.
  • Adding margin-top to a link. Vertical margins are ignored on inline boxes. Change the display or add space on a parent.
  • Big vertical padding on inline text. It overlaps the lines around it. Raise line-height or use inline-block.
  • A gap under images. The image sits on the text baseline. Use display: block or vertical-align: middle.
  • Expecting CSS to fix invalid nesting. display never changes the HTML content rules.

Interview questions

1. What is the difference between block and inline elements in CSS?

Block boxes start on a new line, fill the width of their container and accept width, height and margins on all sides. Inline boxes flow inside a line of text, are as wide as their content and wrap across lines. The behaviour comes from the display value, so CSS can change it.

2. Which box properties do not work on inline elements?

On non-replaced inline boxes, width and height are ignored, and top and bottom margins add no space. Top and bottom padding and borders are painted but do not change the line height, so they overlap neighbouring lines. Horizontal margins, padding and borders do work.

3. What is a line box?

A line box is one horizontal row created when the browser lays out inline content. Text and inline boxes fill it, aligned on the baseline, and a new line box starts when the content no longer fits. Its height depends on the line-height of the content and on any taller inline-level box in that line.

4. Why is there a small gap below an image inside a div?

Images are inline, so they sit on the text baseline. The line keeps space below the baseline for descenders such as "g" and "y", and that space appears as a gap. Setting the image to display: block or vertical-align: middle removes it.

5. What does the two-value display syntax mean?

display sets an outer type (block or inline) and an inner type (flow, flow-root, flex or grid). For example, inline-block equals inline flow-root, and flex equals block flex. The two-value form is Baseline 2023 and makes the meaning explicit.

Key points

  • Normal flow uses block formatting (boxes stacked vertically) and inline formatting (boxes in lines).
  • Inline content is cut into line boxes, aligned on the baseline, with heights set by line-height and tall items.
  • Non-replaced inline boxes ignore width, height and vertical margins; vertical padding overlaps nearby lines.
  • Replaced elements such as <img> are inline but accept width and height.
  • Change display to change behaviour, but CSS never changes HTML nesting rules.
  • Every display value is an outer type plus an inner type: inline-block is inline flow-root.

Exercise

Ananya runs a stationery shop in Pune. Build a short paragraph that mentions a "Sale" label inside the sentence, styled with a background and 0.5rem vertical padding that must not overlap the lines around it. Below it, add a footer menu of four links (Home, Notebooks, Pens, Contact) where each link is a full-width row with padding, so the whole row can be clicked. Finally, place a small image ({{samples}}/icon.svg) inside a bordered box with no gap underneath it.

Show solution
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Stationery shop</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; max-width: 30rem; }
    p { line-height: 1.6; }
    .sale { display: inline-block; padding: 0.5rem; background: #ffd6d6; border-radius: 4px; }
    .menu a { display: block; padding: 0.6rem 1rem; border-bottom: 1px solid #ddd; text-decoration: none; }
    .logo-box { border: 2px solid #444; width: fit-content; }
    .logo-box img { display: block; }
  </style>
</head>
<body>
  <p>All A4 notebooks are now <span class="sale">Sale: ₹45 each</span> until Sunday, only at our Pune shop.</p>
  <nav class="menu">
    <a href="#">Home</a>
    <a href="#">Notebooks</a>
    <a href="#">Pens</a>
    <a href="#">Contact</a>
  </nav>
  <div class="logo-box"><img src="https://course.ethicscomputer.in/assets/samples/icon.svg" alt="Shop icon" width="64" height="64"></div>
</body>
</html>

inline-block lets the label keep its place in the sentence while its padding pushes the lines apart. Block links make each row clickable, and display: block on the image removes the baseline gap.

Quick Quiz

1. You give a <span> the rule width: 200px; height: 50px;. What happens?

2. What happens to padding-top: 20px on an inline link inside a paragraph?

3. Why can you set width on an <img> even though it is inline?

4. Which two-value form is the same as inline-block?

Learn CSS with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call