The <div>
and <span>
tags are used to group and style content in HTML, but they differ in how they affect the layout of the page.
Differences:
- Block vs. Inline:
<div>
is a block-level element, meaning it takes up the full width available and always starts on a new line.<span>
is an inline element, meaning it only takes up as much width as necessary and does not start a new line. - Usage:
<div>
is used to group larger sections of content, such as paragraphs, images, and other block-level elements.<span>
is used to style small portions of text or other inline elements within larger elements.
Example of using <div>
:
<div> <h1>Section Title</h1> <p>This is a paragraph inside a div element.</p> </div>
Example of using <span>
:
<p>This is <span style="color: red;">red text</span> inside a paragraph.</p>