HTML Block & Inline Elements
Every HTML element has a default display behavior. The two most important are block and inline. Understanding the difference explains a lot of "why is this on a new line?" and "why won't my width work?" moments.
Block-level elements
A block-level element:
- always starts on a new line;
- stretches to take up the full width available;
- respects
width,height,marginandpaddingin every direction.
Common block elements: <div>, <p>, <h1>–<h6>, <ul>, <ol>, <li>, <table>, <form>, <section>, <article>, <header>, <footer>, <nav>, <blockquote>, <pre>, <hr>.
Inline elements
An inline element:
- does not start on a new line — it flows along with the text;
- is only as wide as its content;
- ignores
widthandheight, and vertical margins don't push other lines away.
Common inline elements: <span>, <a>, <strong>, <em>, <b>, <i>, <code>, <img>, <label>, <abbr>, <small>, <sub>, <sup>, <br>, <input>, <button>.
The outlines in this example make each element's box visible:
<style>
p, div, h3 { outline: 2px solid #4a90d9; }
span, a, strong, em { outline: 2px solid #e34c26; }
body { font-family: sans-serif; }
</style>
<h3>A heading is a block</h3>
<p>This paragraph is a block, but these words — <strong>bold</strong>,
<em>italic</em>, <a href="#">a link</a> and <span>a span</span> — are inline
and sit inside the line of text.</p>
<div>A div is a block too.</div>
<div>So this div starts on a new line.</div>Blue outlines (blocks) span the whole width. Red outlines (inline) hug their words.
Side by side comparison
| Block | Inline | |
|---|---|---|
| Starts on new line | Yes | No |
| Default width | Full width of parent | Width of content |
width / height |
Applied | Ignored |
| Vertical margin/padding | Affects layout | Padding paints, but doesn't push lines apart |
| Generic container | <div> |
<span> |
| Can contain | Block and inline elements | Usually only inline content and text |
Width and height on inline elements
Try it: the same width and height are set on a <span> and a <div>. Only the block element listens.
<style>
.box { width: 160px; height: 60px; background: #ffd9cc; border: 2px solid #e34c26; }
</style>
<p>Here is a <span class="box">span with a size</span> in the middle of a sentence.</p>
<div class="box">div with a size</div>Changing the display type with CSS
The display property lets you switch an element's behavior. This changes how it looks, not what it means — a <li> styled as inline is still a list item.
| Value | Behavior |
|---|---|
block |
Like a <div> |
inline |
Like a <span> |
inline-block |
Flows in a line like inline, but respects width, height and all margins |
none |
Removes the element from the page entirely |
flex / grid |
Turns the element into a layout container for its children |
<style>
body { font-family: sans-serif; }
.tags span {
display: inline-block;
width: 90px;
padding: 6px;
margin: 4px;
text-align: center;
background: #e8f0fe;
border-radius: 14px;
}
.stacked a { display: block; padding: 6px; border-bottom: 1px solid #ccc; }
.hidden { display: none; }
</style>
<p class="tags">
<span>html</span><span>css</span><span>js</span><span class="hidden">secret</span>
</p>
<p>The fourth tag has <code>display: none</code>, so it isn't shown at all.</p>
<div class="stacked">
<a href="#">Links made into blocks</a>
<a href="#">each fill a whole row</a>
<a href="#">and are easier to tap</a>
</div>Tip:
inline-blockis a handy middle ground for buttons, badges and tags: they sit in a row like words but can still have a fixed size and padding.
Note: You can't put a block element like
<div>or<p>inside a<p>. The browser automatically closes the paragraph before the block, which can produce surprising results. Links (<a>) are an exception: in modern HTML an<a>may wrap block content such as a whole card.
Test yourself
Which of these is an inline element by default?
<p><div><span><h2>
What does a block-level element do by default?
- Takes only as much width as its content
- Starts on a new line and takes the full available width
- Ignores padding
- Floats to the left
Which display value lets an element sit in a line of text while still respecting width and height?
inlineblockinline-blocknone