DEV Community

Cover image for CSS Selectors: Part 1, The Basics.
Mohamed Adel
Mohamed Adel

Posted on

1

CSS Selectors: Part 1, The Basics.

Intro:

  • So you have written some Html and now you want to style it? then the best way to do that is by using CSS Selectors.
  • CSS Selectors are among the first things you're going to learn while learning CSS, and in this post we are going over CSS Selectors basics.

-Selector types:

  • There are 4 basic CSS Selector types which are mostly used in CSS: selector types image

However these aren't the only ones, and we will be going through most of them in the other part/s.


-Universal selector (*):

  • This selector selects all the elements on an html page.
<p> paragraph </p>
<h2> header </h2>  
<div class="card"> --some content-- </div>
Enter fullscreen mode Exit fullscreen mode
* {
  margin: 0; 
  padding: 0;
  box-sizing: border-box;
}
Enter fullscreen mode Exit fullscreen mode
  • mostly used on top of your style sheet to add universal styles to all elements on the page, like the CSS reset for example.

-Tag selector:

  • This selector selects elements based on their HTML tag name.
<p> selected </p>
<h2> selected </h2>  
<a> not selected  </a>
Enter fullscreen mode Exit fullscreen mode
p {
  property: value; 
}

h2 {
  property: value;
}
Enter fullscreen mode Exit fullscreen mode

-Class selector:

  • This selector selects elements based on their HTML class name using ".class-name".
<p class="text"> selected </p>
<h2 class="heading"> not selected </h2>  
Enter fullscreen mode Exit fullscreen mode
.text {
  property: value;
}
Enter fullscreen mode Exit fullscreen mode

-ID selector:

  • This selector selects elements based on their HTML ID using "#id-name".
<p id="text"> not selected </p>
<h2 id="heading"> selected </h2>  
Enter fullscreen mode Exit fullscreen mode
#header {
  property: value;
}
Enter fullscreen mode Exit fullscreen mode

-Grouping CSS Selectors:

  • The previous CSS selectors can be grouped to apply common styles to more than one element using commas (,).
<p> not selected </p>
<h2> selected </h2>  
<div> selected </div>
<h1> selected </h1>
Enter fullscreen mode Exit fullscreen mode
h1, h2, div {
  /* usually a common style */
  property: value;
}
Enter fullscreen mode Exit fullscreen mode

-CSS Combinators:

  • CSS Combinators use the same basic CSS Selectors to target specific HTML elements based on their relation with other elements.

  • Types of CSS Combinators:
    Combinators image


-Descendant Combinator:

  • This combinator selects the children of the specified parent element.
<div>
  <h1> not selected </h1>
  <p> selected </p>
  <a> not selected </a>
  <p> selected </p>
</div>
<p> not selected </p>
Enter fullscreen mode Exit fullscreen mode
div p { /* this selects all p elements inside the div */
  property: value;
}
Enter fullscreen mode Exit fullscreen mode

-Direct Child Combinator (>):

  • This combinator selects the direct children of the specified parent element.
<div>
  <p> selected </p>
  <a> not selected </a>
  <p> selected </p>
</div>
<div>
  <span>
    <p> not selected </p>
  </span>
</div>
<p> not selected </p>
Enter fullscreen mode Exit fullscreen mode
div > p { /* this selects direct children p elements inside the div */
  property: value;
}
Enter fullscreen mode Exit fullscreen mode

-General Sibling Combinator (~):

  • This combinator selects the siblings of the specified element.
<p> not selected </p>
<div>
  <p> not selected </p>
</div>
<p> selected </p>
<p> selected </p>
Enter fullscreen mode Exit fullscreen mode
div ~ p { /* this selects the siblings that come after specified element */
  property: value;
}
Enter fullscreen mode Exit fullscreen mode

-Direct Sibling Combinator (+):

  • This combinator selects the direct sibling of the specified element not all siblings.
<p> not selected </p>
<div>
  <p> not selected </p>
</div>
<p> selected </p>
<p> not selected </p>
Enter fullscreen mode Exit fullscreen mode
div + p { /* this selects the direct p element that comes after the specified element */
  property: value;
}
Enter fullscreen mode Exit fullscreen mode

Conclusion:

Congratulations! You've just taken your first steps into the exciting world of CSS Selectors.

In the upcoming part/s of this series, we will discuss even more selector techniques in CSS.

If you have any questions or want to share your thoughts, please don't hesitate to leave a comment below. I'd love to hear from you!

Connect with me on LinkedIn, reach out via Email (contact.mo.adel@gmail.com), and explore my code on GitHub for more web development insights and collaboration opportunities.

Thank you for joining me on this CSS Selectors journey. Until next time, happy coding!

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more