HTML Blocks
HTML elements fall into two broad groups based on how they sit on the page: block-level elements, which start on a new line and stretch across the full width available, and inline elements, which sit within a line of text and take only as much width as their content. Knowing which is which explains why some elements stack and others flow side by side.
How block and inline elements behave
| Behaviour | Block-level | Inline |
|---|---|---|
| Starts on a new line | Yes | No |
| Default width | Full width of the parent | Width of its content |
| Width and height properties | Respected | Ignored |
| Vertical margin | Pushes neighbours away | Does not affect line spacing |
| Examples | div, p, h1–h6, ul, section, form | span, a, strong, em, img, code |
Strictly speaking, modern HTML describes content categories rather than "block" and "inline", and the visual behaviour comes from the CSS display value. In everyday work, though, the two labels are still the clearest way to think about it.
The div and span containers
Two elements exist purely to group other content and carry no meaning of their own. <div> is the generic block container, and <span> is the generic inline one. You reach for them when you need a hook for CSS or JavaScript and no meaningful element fits.
<div class="notice">
<h2>Library timings</h2>
<p>
Open from <span class="time">9 AM</span> to
<span class="time">6 PM</span>, Monday to Saturday.
</p>
<p>Sunday: <strong>closed</strong>.</p>
</div>
Here the div groups the whole notice so it can get a border and padding, while each span lets you colour the times without breaking the sentence onto new lines.
Prefer meaningful elements over plain divs
A page built entirely from divs works visually, but browsers, search engines and screen readers learn nothing about its structure. HTML5 added semantic block elements that describe their role: <header>, <nav>, <main>, <section>, <article>, <aside> and <footer>. Use these first, and fall back to div only for grouping that is purely for styling. These elements are the building blocks of the layouts in the next lesson.
Nesting rules that catch beginners
Not every element can go inside every other one:
- A
<p>cannot contain block elements like<div>,<ul>or another<p>. If you write one inside, the browser closes the paragraph early, and your CSS suddenly stops matching what you expected. - Inline elements such as
<span>should not wrap block elements. The exception is<a>, which is allowed to wrap a whole card in HTML5, as long as nothing interactive is inside it. - List items,
<li>, belong directly inside<ul>or<ol>.
A validator or the browser's element inspector quickly shows when the browser has silently rearranged your markup.
Changing the behaviour with CSS
Block or inline is just a default. CSS can change it: display: block makes a link fill its row, display: inline-block lets an element sit in a line while still accepting width, height and vertical padding, and display: flex turns a container into a flexible layout. The HTML element you choose should reflect meaning; CSS takes care of the look.
The habit to keep: pick elements for what the content is, use div and span only when nothing better exists, and adjust appearance with display.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.