Skip to main content

How to Put an Element on Its Own Line with display: block in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Some elements, such as label and span, sit in the line of text with their neighbors. display: block puts an element on a line of its own, so a label lands above its field instead of beside it.

Syntax​

Syntax
<selector> {
display: block;
}
PartWhat it does
<selector>The element to move onto its own line.
blockMakes the element a block: it starts on a new line, and what follows it starts on the next one.

MDN's display page says a block generates line breaks both before and after the element, while an inline element doesn't.

The Example​

The example is a feedback form with a Comments label and a text area. First you see the label beside the text area, then one rule puts it above.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>display: block</title>
</head>
<body>
<form>
<label for="comments">Comments</label>
<textarea id="comments" rows="3"></textarea>
</form>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: the word Comments to the left of an empty text area, lined up with its bottom edge.

The label sits to the left of the text area, at the level of its bottom edge: a label is inline, so it shares the line with the text area. The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

Step 3: Make the Label a Block​

Add a style element with a rule for label to head, so the file looks like this:

index.html, the same page with a style element in its head holding a rule for label that sets display to block.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>display: block</title>
<style>
label {
display: block;
}
</style>
</head>
<body>
<form>
<label for="comments">Comments</label>
<textarea id="comments" rows="3"></textarea>
</form>
</body>
</html>

Step 4: Reload the Page​

Save the file and reload the page in your browser:

The page in Chromium: the word Comments on its own line, with the text area right below it.

What Happened​

  • The label moved onto its own line, and the text area starts right under it.
  • The label is now as wide as the page. In a window 800 pixels wide, it measured 784 pixels: a block takes the whole width of its container.

Common Mistakes​

MistakeWhat happensDo this instead
A typo in the value: display: blocksThe value is invalid and ignored: the label stayed inline, beside the text areaWrite block
Expecting a block to stay as wide as its textA block takes the full width: a span with display: block and a yellow background was 784 pixels wide, where it had been 37Set a width if it should be narrower

Troubleshooting​

SymptomLikely causeFix
The element stays on the same lineThe rule doesn't match it, or the value is misspelledCheck the selector and block
A background suddenly fills the whole lineBlocks are as wide as their containerThat's expected, or set a width

FAQ​

What's the difference between inline and block?​

An inline element, like label or span, sits in the line with the text around it and is only as wide as its content. A block, like p or div, starts on a new line and takes the full width.

Do I need br to break the line instead?​

No. display: block moves the element onto its own line without changing the HTML.

Conclusion​

Set display: block on an inline element to give it a line of its own. It also takes the full width of its container, so set a width when it should be narrower.

Additional Resources​