DEV Community

Israel Rotimi
Israel Rotimi

Posted on

1

CSS Selectors for Beginners

When styling a webpage, you need to know CSS and understand how to use the right selectors to achieve your desired result. Selectors help you target specific HTML elements to apply styles efficiently.


1. Universal Selector (*)

The universal selector applies styles to all elements on the page.

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
Enter fullscreen mode Exit fullscreen mode

Use it sparingly, mainly for resetting default styles.


2. Element Selector (element)

The element selector targets all instances of a specific HTML tag.

p {
    color: blue;
    font-size: 16px;
}
Enter fullscreen mode Exit fullscreen mode

You can use this for general styling of common elements (e.g., body, h1, p).


3. Class Selector (.class)

The class selector targets elements with a specific class.

.highlight {
    background-color: yellow;
    font-weight: bold;
}
Enter fullscreen mode Exit fullscreen mode
<p class="highlight">This text is highlighted.</p>
Enter fullscreen mode Exit fullscreen mode

Best for reusable styles across multiple elements.


4. ID Selector (#id)

The ID selector targets an element with a specific id.

#header {
    background-color: navy;
    color: white;
    padding: 10px;
}
Enter fullscreen mode Exit fullscreen mode
<div id="header">Welcome to My Website</div>
Enter fullscreen mode Exit fullscreen mode

Use for 'one-off' styles only.


5. Grouping Selector (A, B, C)

This allows styling multiple elements at once.

h1, h2, h3 {
    font-family: Arial, sans-serif;
    color: darkslategray;
}
Enter fullscreen mode Exit fullscreen mode

It helps to avoid redundant styles and keep CSS clean.


6. Descendant Selector (A B)

Targets elements inside a specific parent.

nav ul {
    list-style-type: none;
    padding: 0;
}
Enter fullscreen mode Exit fullscreen mode
<nav>
    <ul>
        <li>Home</li>
        <li>About</li>
    </ul>
</nav>
Enter fullscreen mode Exit fullscreen mode

They Keep selectors short and readable.


7. Child Selector (A > B)

Targets direct children only.

div > p {
    color: red;
}
Enter fullscreen mode Exit fullscreen mode
<div>
    <p>Styled paragraph</p>  <!-- Affected -->
    <section>
        <p>Not affected</p>  <!-- Not affected -->
    </section>
</div>
Enter fullscreen mode Exit fullscreen mode

Use when only direct children need styling.


8. Adjacent Sibling Selector (A + B)

Targets an element immediately after another.

h1 + p {
    color: green;
}
Enter fullscreen mode Exit fullscreen mode
<h1>Heading</h1>
<p>This paragraph is green.</p>
<p>This one is not.</p>
Enter fullscreen mode Exit fullscreen mode

Use for styling elements right after another.


9. General Sibling Selector (A ~ B)

Targets all matching siblings after a specific element.

h1 ~ p {
    color: orange;
}
Enter fullscreen mode Exit fullscreen mode
<h1>Heading</h1>
<p>This is orange.</p>
<p>This is also orange.</p>
Enter fullscreen mode Exit fullscreen mode

Use when multiple elements after a specific one need styling.


Conclusion

Mastering selectors makes styling easier and more structured! 🚀
What do you normally use when styling?


I'm a full stack JS/TS developer open to collaboration and job offers. Feel free to reach out to me

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed
  • 2:34 --only-changed
  • 4:27 --repeat-each
  • 5:15 --forbid-only
  • 5:51 --ui --headed --workers 1

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay