The
<div>and<span>elements are two of the most commonly used tags in HTML.
They do not add any visual change by themselves but are extremely useful for grouping content, styling, and layout control using CSS.
Introduction
Both <div> and <span> are container elements:
<div>is a block-level container.<span>is an inline container.
They are often used with CSS or JavaScript to structure and style specific parts of a web page.
The <div> Element
The <div> tag defines a division or section in a webpage.
It is a block-level element, meaning it starts on a new line and takes up the full width.
<div>Content goes here...</div><div style="background-color: #f0f0f0; padding: 10px;">
<h2>Welcome to HTML</h2>
<p>This section is wrapped inside a div element.</p>
</div>Explanation:
- The
<div>element groups the heading and paragraph. - Styling (like background color and padding) is applied to the entire section.
Common Uses of <div>
- To create page sections like header, footer, sidebar, or content area.
- To apply CSS styling to grouped elements.
- To structure layouts for responsive design.
- To apply JavaScript functions to a specific section.
Example: Page Layout Using <div>
<div id="header">Header Section</div>
<div id="menu">Menu Section</div>
<div id="content">Main Content Section</div>
<div id="footer">Footer Section</div>Each <div> can be styled separately using CSS.
The <span> Element
The <span> tag is used to group inline elements for styling or scripting purposes.
Unlike <div>, it does not start a new line and only takes up the width of its content.
<span>Inline text here...</span><p>Welcome to <span style="color: red;">HTML5</span> Tutorial.</p>Explanation:
- The
<span>tag highlights a specific part of text within a paragraph. - CSS is used to color only that section red.
Difference Between <div> and <span>
| Feature | <div> | <span> |
|---|---|---|
| Type | Block-level | Inline |
| Starts a new line | Yes | No |
| Used for | Large sections or containers | Small text or inline content |
| Typical use | Layouts, grouping sections | Highlighting or styling inline text |
| Default width | 100% | As per content |
Combining <div> and <span>
You can use both tags together for flexible styling.
<div style="background: #eef; padding: 10px;">
<h3>User Details</h3>
<p>Name: <span style="color: blue;">Amit Sharma</span></p>
<p>Role: <span style="font-weight: bold;">Developer</span></p>
</div>Explanation:
<div>groups the section.<span>styles specific text portions within paragraphs.
Styling <div> and <span> with CSS Classes
Instead of inline styles, it’s best practice to use CSS classes.
<style>
.box { background: lightgray; padding: 10px; margin: 5px; }
.highlight { color: green; font-weight: bold; }
</style>
<div class="box">
<p>This is a <span class="highlight">highlighted</span> word inside a div.</p>
</div>Explanation:
- The
.boxclass applies layout and spacing to the<div>. - The
.highlightclass changes color and font weight for text inside<span>.
Practical Example: Layout with Div & Span
<div style="border: 1px solid #ccc; padding: 10px;">
<h2>Product Information</h2>
<p>Product Name: <span style="color: brown;">Wireless Mouse</span></p>
<p>Price: <span style="color: green;">$25</span></p>
<p>Description: <span>A smooth and precise wireless mouse for everyday use.</span></p>
</div>Explanation:
This structure allows easy styling and layout control. Each <span> can be customized independently.
Conclusion
- The
<div>element is used for structuring and organizing large sections of content. - The
<span>element is used for styling and targeting specific parts of inline text. - Together, they form the building blocks of modern web layouts, especially when combined with CSS and JavaScript.