- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML Div & Span
HTML Layout
HTML Div & Span
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:
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 Syntax
html
<div>Content goes here...</div>Div Example
html
<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 Example
html
<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 Syntax
html
<span>Inline text here...</span>Span Example
html
<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.
Combined Example
html
<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:
Styling <div> and <span> with CSS Classes
Instead of inline styles, it’s best practice to use CSS classes.
Div & Span Style Example
html
<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
Practical Example
html
<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.
Browser Support
Chrome | Edge | Firefox | Safari | Opera | IE9+ |
|---|---|---|---|---|---|
| Yes | Yes | Yes | Yes | Yes | Yes |
