DEV Community

Cover image for "Different Types of CSS Selectors”
Mahia  Momo
Mahia Momo

Posted on

"Different Types of CSS Selectors”

Types of Selectors in CSS:

Image description

Class Selector:

Code:

<body>
    <p class="heighlight">class of "heighlight".</p>
    <p>does not have any specific class.</p>
    <p class="heighlight">a class of "heighlight".</p>
</body>
Enter fullscreen mode Exit fullscreen mode
<style>
.heighlight {
    color: red;
    font-weight: bold;
}
</style>
Enter fullscreen mode Exit fullscreen mode

Id Selector:

Code:

<body>
    <h1 id="main-heading">This h1 has an id</h1>
    <p>This paragraph does not have an id</p>
    <p id="intro-paragraph">This paragraph has an id</p>
</body>
Enter fullscreen mode Exit fullscreen mode
<style>
#main-heading {
    color: blue;
    font-size: 24px;

}

#intro-paragraph {
    background-color: yellow;
    padding: 10px;

}
</style>

Enter fullscreen mode Exit fullscreen mode

Elementor Selector:

<body>
    <h1>Welcome to My Website</h1>
    <p>This is a paragraph</p>
    <p>This is another paragraph</p>
    <a href="https:///www.google.com">Visit Example Website</a>
</body>
Enter fullscreen mode Exit fullscreen mode
<style>
    h1 {
        color: blue;
    }
    p {
      font-size: 16px;

    }
    a {
        text-decoration: none;
        color: red;
    }
</style>
Enter fullscreen mode Exit fullscreen mode

Universal Selector

Code:

<body>
    <h1>Welcome to My Website</h1>
    <p>This is a paragraph</p>
    <div>
        <h2>About Us</h2>
        <p>This is another paragraph</p>
    </div>
</body>
Enter fullscreen mode Exit fullscreen mode
<style>
*{
    margin: 0;
    padding: 0;
    border: 1 px solid;
}
</style>
Enter fullscreen mode Exit fullscreen mode

Grouping Selector:

Code:

<body>
    <h1>Welcome to My Website</h1>
    <p>This is a paragraph.</p>
    <a href="#">Click me</a>
    <button>Submit</button>
</body>
Enter fullscreen mode Exit fullscreen mode
<style>
    h1,p {
        color: blue;
    }
    a,button {
        background-color: yellow;
        padding: 10px;
    }
</style>
Enter fullscreen mode Exit fullscreen mode

Attribute Selector:

Code:

<form>
    <label for="name">Name:</label>
    <input type="text" id="name" required />
    <input type="submit" value="Submit" />
</form>
Enter fullscreen mode Exit fullscreen mode
<style>
    input[type="submit"] {
        background-color: #4caf50;
        color: white;
    }
    input[required] {
        border: 1px solid red;
    }
</style>
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
raj_afre profile image
Raj

really helpful!

Collapse
 
mahiamomo profile image
Mahia Momo

It's pleasure of mine🥰