The Universal Selector

The css universal selector selects any element within an HTML page and can also select any element inside another.

For the universal selector we use the asterisk (*) symbol.

Basic usage:

* { style_proerties }

Example:

* {
   color: blue;
}

This will select all elements and sets their font color to blue.

Most of the time we only use this selector to reset css. What does it mean exactly? The browser applies some default formatting to HTML elements, and these formattings may not be the same in all browsers. It may result in some inconsistencies when people view your site in different browsers. With the universal selector we can remove these default formattings.

For example, let’s remove the default padding and margins from our document:

* {
   margin: 0;
   padding: 0
}

We can use the universal selector to select elements within another. For example,

#articles * {
   font-size: 18px;
}

Select all elements inside #articles and set their font size to 18px. However, we can firther narrow this down.

#articles * p {
   font-size: 18px;
}

Now we will only apply the font size to paragraph elements inside #articles

ChristianKovats
ChristianKovats

Christian Kovats is a London-based web designer who loves crafting innovative and user-friendly interfaces. His passion for design is only rivaled by his love for nature and hiking, often drawing inspiration from his treks through the diverse landscapes of the UK. Outside of work, Christian enjoys spending quality time with his wife and 4-year-old son. A seasoned lucid dreamer, he also spends time exploring the intricate world of dream interpretation, reflecting his ever-curious and creative mind.

ChristianKovats

Leave a reply

You must be logged in to post a comment.