Dular SharmaDular SharmaDular Sharma
(Monday - Saturday)
Dular SharmaDular SharmaDular Sharma

Chapter 6. DIV and SPAN Selectors in CSS


Understanding CSS Selectors: The Versatile Div and Span Elements

In web development, div and span elements are fundamental building blocks used for structuring content. These elements, combined with CSS, allow developers to create complex, well-styled web pages. In this blog, we’ll dive deep into the div and span elements, explore their uses, and demonstrate how to apply CSS to style them effectively.

The div Element

The div (short for “division”) element is a block-level container used to group together other elements. It is a versatile and widely used element in HTML, allowing for the organization of content and application of styles.

Basic Usage

A div element is typically used to create sections or containers within a webpage. Here’s a simple example:

    <div class="container">
        <h1>Welcome to My Website</h1>
        <div class="content">
            <p>This is a paragraph inside a div element.</p>

And the corresponding CSS:

.container {
    width: 80%;
    margin: 0 auto;
    padding: 20px;
    background-color: #f0f0f0;

.content {
    padding: 10px;
    background-color: #e0e0e0;

The span Element

The span element is an inline container used to wrap text or other inline elements. Unlike div, which is a block-level element, span does not cause line breaks before and after it.

Basic Usage

A span element is useful for applying styles to part of the text or other inline elements:

<p>This is a <span class="highlight">highlighted</span> word in a sentence.</p>

And the corresponding CSS:

.highlight {
    color: red;
    font-weight: bold;

Combining div and span

div and span can be used together to create well-structured and styled content. Here’s an example:

<div class="article">
    <h2>Article Title</h2>
    <p>This is an article with a <span class="keyword">keyword</span> highlighted.</p>
    <div class="author">
        <span>By: </span><span class="name">John Doe</span>

And the corresponding CSS:

.article {
    border: 1px solid #ccc;
    padding: 15px;
    margin-bottom: 20px;
    background-color: #fafafa;

.keyword {
    background-color: yellow;

.author {
    margin-top: 10px;
    font-style: italic;

.name {
    font-weight: bold;

Understanding and utilizing div and span elements effectively is crucial for web development. They provide the flexibility needed to structure content and apply styles precisely. By following best practices and leveraging the power of CSS, you can create clean, maintainable, and visually appealing web pages.

Previous Post
Newer Post

Leave A Comment

Need Help?