On this page10 sections
Lecture 016: Block & Inline: div and span
| Category | 4: Page Structure |
| Lecture | 016 of 048 |
| Estimated video length | 10 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
Some elements start on a new line and take the full width; others sit inside a line of text. Knowing which is which explains most "why is this on its own line?" questions. Two plain elements,
<div>and<span>, let you group anything for styling.
Learning Objectives
- Tell block elements from inline elements
- Group content with
<div>(block) and<span>(inline) - Use
classon these elements so CSS can style them
1. Block Elements
A block element starts on a new line and stretches across the full width available.
Common block elements: <h1>–<h6>, <p>, <ul>, <ol>, <li>, <table>, <div>,
<form>.
Example: the background colour shows how wide each block is:
<p style="background: #ffe8cc;">I am a paragraph, so I fill the whole width.</p>
<h3 style="background: #d3f9d8;">So does this heading.</h3>2. Inline Elements
An inline element sits inside a line of text and is only as wide as its content.
Common inline elements: <a>, <strong>, <em>, <img>, <code>, <span>.
Example
<p>
This has a <a href="#">link</a>, some <strong>bold text</strong>
and <span style="background: #ffe066;">a highlighted span</span>, all on one line.
</p>Rule of thumb: inline elements go inside block elements, not the other way round.
3. <div>: a Box for Grouping
<div> has no meaning of its own. It's a plain block box that groups other elements, so you
can style or move them together.
Example
<div style="border: 2px solid #4c6ef5; padding: 12px;">
<h3>Ali Raza</h3>
<p>Flutter developer, Lahore</p>
<a href="mailto:ali@example.com">Email Ali</a>
</div>4. <span>: a Wrapper Inside Text
<span> is the inline version: it wraps a few words so you can style them.
Example
<p>Your order is <span style="color: green; font-weight: bold;">confirmed</span>.</p>5. Classes Make Groups Reusable
Inline style gets messy fast. Give elements a class, and in the CSS course one rule will
style them all.
Example: the same idea using a <style> block (a preview of CSS):
<style>
.card { border: 1px solid #ccc; border-radius: 8px; padding: 12px; margin-bottom: 10px; }
.price { color: crimson; font-weight: bold; }
</style>
<div class="card">
<h3>Wireless mouse</h3>
<p>Only <span class="price">Rs 1,800</span></p>
</div>
<div class="card">
<h3>USB keyboard</h3>
<p>Only <span class="price">Rs 2,500</span></p>
</div>Common Beginner Mistakes
1. "Div soup". Wrapping everything in <div>s when a meaningful element exists. A list
should be <ul>, a heading <h2>. Lecture 023 adds more meaningful boxes.
2. Block inside inline. <span><p>…</p></span> is invalid. Put spans inside paragraphs.
3. Expecting <span> to start a new line. It's inline. Use a <div> or <p> for a new line.
Practice Questions
- Does a
<p>start on a new line? - Name three inline elements.
- Which element groups several elements into one block?
- Which element would you use to colour just one word in a sentence?
- Is
<strong><div>Hi</div></strong>a good idea? Why? - Wrap a heading and a paragraph in a
divwith the classnotice.
Practice Answers
- Yes. It's a block element.
- Any three of
<a>,<strong>,<em>,<img>,<code>,<span>. <div><span>- No. A block element (
<div>) shouldn't go inside an inline element (<strong>). -
<div class="notice"> <h3>Holiday</h3> <p>The office is closed on Friday.</p> </div>
Key Takeaways
- Block elements start a new line and fill the width; inline elements flow inside text.
<div>= a plain block box;<span>= a plain inline wrapper.- Neither has meaning; use them for grouping and styling, with a
class. - Prefer meaningful elements whenever one fits.