30 Days of CSS

David
2 min readJun 11, 2021

--

Photo by Mike Lewis HeadSmart Media on Unsplash

Introduction

I’ve been a backend developer for a long time and decided that I want to learn more frontend skills. So, I’ve started doing 30 days of CSS. You can follow my progress on Twitter at @cloudblogaas

For my first couple of days, I’ve learned about selectors and selector specificity.

Selectors

For my reference, some of the more common selectors are:

There are many more selectors, all detailed at MDN WebDocs. The ones above though, seem to be the most common.

I’ve followed this video on YouTube which I found invaluable for learning.

Selector Specificity

Selector Specificity allows us to know what orders selectors will be used. This is useful when trying to work out what a style hasn’t been applied to an element. Normally, CSS is read from the top of the file downwards, so something at the bottom of the css file will overwrite something defined at the top.

Selector specificity states how certain selectors are “higher priority” than others. The specificity of selectors ranks from high to low as:

  • style
  • id
  • class
  • element

So, for example if an element has the following css, the style associated with the id would be used instead of that of the class, so the element would be styled red.

#myId {
color: red;
}

.myClass {
color: blue;
}
<h1 id="myId" class="myClass>...</h1>

A style can be defined as important by appending the !important tag, for example:

.myClass {
color: blue !important;
}

From what I’ve seen though this is considered bad practice as it breaks specificity and can override what a system is meant to do.

Summary

CSS wouldn’t be CSS without selectors. The MDN WebDocs on Selectors and MDN WebDocs on Specificity provide good indepth details.

--

--

David
David

Written by David

Full Stack Web Developer, Lover of the Cloud

No responses yet