HTML & CSS Interview Question #11

What is the difference between <div> and <span>?

HTML Fundamentals, Structure, Semantics, Links, Images, Tables & Forms Junior Beginner

Quick Interview Answer

div and span are generic elements with no specific semantic meaning. A div is commonly used as a generic block container, while span is commonly used to wrap a small inline portion of content.

Detailed Explanation

div and span are generic elements with no specific semantic meaning. A div is commonly used as a generic block container, while span is commonly used to wrap a small inline portion of content.

They are useful when no more meaningful HTML element is appropriate, especially for grouping content for CSS or JavaScript. They should not replace semantic elements such as button, nav, article, or heading elements when those elements correctly describe the content.

Code Example

<div class="product-card">
    <h2>Projector</h2>
    <p>Price: <span class="price">$199</span></p>
</div>