Building Blocks of CSS – CSS Selectors

Faiza Khalid
29.10.2021
|
3410
Building Blocks of CSS – CSS Selectors

HTML, CSS, and JavaScript help to build a flexible and lightweight web page. Although the whole process takes some skill and time, the result is a highly customizable website.  Familiarity with the basics of CSS proves helpful for anyone interested in creating a personalized website. In this article, we will go through one such building block of CSS, which is Selectors.

What are CSS Selectors?

HTML defines the main structure of a webpage. The hierarchy of HTML elements dictates the place of each element. CSS adds styling to each of these elements. CSS selectors help with this styling. A CSS selector indicates which elements to select for a particular styling. A designer may want the text of all of the paragraphs of a webpage in black, but one in particular should appear highlighted; or all of the web links on a page should appear blue, and the ones visited should become green; or some text should appear highlighted if the user places a mouse cursor on it. Such styling requires that a designer has fine control over all the elements present on a webpage. CSS selectors provide such control. Some CSS selectors cover a broad selection of elements for styling, while others are more specific. There are some CSS selectors which are an absolute  must for any aspiring web designer or a front-end developer to know. Let’s go through them.

We will start with CSS selectors that broadly select elements and narrow them down to some specific ones. 

Universal Selector

As the name indicates, this selector has a universal effect. It applies the defined styling to all the HTML elements.

<!DOCTYPE html>
<html>
<head>
  <style type="text/css">
  * {
   color: blue;
   background-color: yellow;
   font-style: oblique;
  } 
  </style>
</head>

<body>
  <div>
    <p>We all have same CSS styling.</p>
  </div>
  <div class="div1">
    <p>We all have same CSS styling.</p>
  </div>
  <div id="diffdiv" class="div1">
    <p>We all have same CSS styling.</p>
  </div>
</body>
</html>

The above CSS has styled all of the elements, including divs and paragraphs. The whole body tag selects for universal styling. This is why we see the full page turn yellow!

The universal selector is also used to select all of the children of an element. The following styling applies to all of the child elements of a div.

 div *{
   color: blue;
   background-color: yellow;
   font-style: oblique;
  }

Type Selector

Another general CSS selector is a type selector. The defined CSS styles are applied to the elements of a specific type. For example, to all of the <a> tags or <p> tags.

 p {
 color: green;
 background-color: yellow;
 font-weight: bold;
} 

Class Selector

If there are multiple HTML elements and all of them should appear in a similar style on a webpage, we put them under a class name. The class selector then selects all these class elements and applies a similar styling to them.

Id Selector

The id selector narrows the element selection further. The id for all of the elements is unique. No two elements can have a similar id, which means even if two elements belong to the same class, and an id selector is defined for one of them, we can style it uniquely. This gives more control to a designer over the elements and their intricate styling. Below we have provided an elaborate example of styling different elements with the same class but a different Id.

<!DOCTYPE html>
<html>
<head>
  <style type="text/css">
  div {
   font-style: oblique;
   font-size: 20px;
   font-family: monospace, sans-serif;
   background-color: yellow;
  } 
  .div1{
       border: solid black;
  }
  #diffdiv{
    font-weight: bold;
    border: dotted blue;
    background-color: green;
    color: white;
  }
  </style>
</head>

<body>
  <div>
    <p>I am the first div.</p>
  </div>
  <div class="div1">
    <p>I am the second div with a class name.</p>
  </div>
  <div id="diffdiv" class="div1">
    <p>I am the third div with div1 class and diffdiv Id.</p>
  </div>
</body>
</html>

Attribute Selector

An attribute selector selects HTML elements with specific attributes or attribute values. For example, a website developer wants to style all of the input properties with type attributes uniformly, except for the type value of a button:

<!DOCTYPE html>
<html>
<head>
  <style type="text/css">
  div {
   font-style: oblique;
   font-size: 20px;
   font-family: monospace, sans-serif;
   background-color: gray;
   color: white;
  }
  input{
       margin-bottom: 10px;
       margin-top: 10px;
       border: solid black;
       display:block;
  }
  input[type=button]{
    font-weight: bold;
    background-color: lightblue;
    color: black;
  }
  </style>
</head>

<body>
  <div>
    <form id="form">
        <input type="text" placeholder="First Name">
        <input type="text" placeholder="Last Name">
        <input type="pswd" placeholder="Password">
        <input type="button" value="OK">
    </form>
  </div>
</body>
</html>

The above code styles the text and password type inputs similarly, but makes the button input unique. It has a light blue color and bold font.

Combinators

Elements can be selected in a combination as well. For example, select an element and its first child only or select only direct children of a specific element, etc.  

X + Y:

Selects y preceding x. For example, the link tag preceding a <p> tag.

X Y and X > Y:

X > Y selects the direct descendants Y of X, unlike in X Y, where all the elements under the X are selected.

The following code explains the difference.

<!DOCTYPE html>
<html>
<head>
  <style type="text/css">
  div {
   font-style: oblique;
   font-size: 20px;
   font-weight: bold;
   font-family: monospace, sans-serif;
  } 
  p > a{
    font-style: italic;
    color: yellow;
  }
  </style>
</head>

<body>
  <div>
    <p>These are some Wikipedia Pages.
       <a href="https://en.wikipedia.org/wiki/CSS"> CSS Wiki</a>
       <a href="https://en.wikipedia.org/wiki/HTML"> HTML Wiki</a>
    </p>     
  </div>
</body>
</html>

To see the difference, we can take the HTML wiki link out of the p tag. This time only <a> inside p is selected as it is the direct child.

<div>
  <p>These are some Wikipedia Pages.
     <a href="https://en.wikipedia.org/wiki/CSS"> CSS Wiki</a>
     <div>
       <a href="https://en.wikipedia.org/wiki/HTML"> HTML Wiki</a>
     </div>
  </p>     
</div>

Pseudo Selectors

A pseudo selector selects elements in a specific ‘state’. For example, select the visited links, select the focused input field, or select the element on a mouse ‘hover’. In these and other states, different styling applies to highlight the elements. It gives a visually appealing and interactive experience to the user.

a:visited{
  color: gray;
}
p:hover{
  background-color: lightgreen;
}

Adding these styles to the previous code will turn the visited links gray. Also, when we hover our mouse over a paragraph, it turns light green. It is pseudo selection.

Some other pseudo selectors are after, checked, first-line (in a paragraph), first-letter, etc.

CSS selectors can also be grouped if they have the same styling. They are separated by a comma such as:

a:visited, p:hover{
  background: gray;
}

Summary

In this article, we discussed one CSS building block - CSS Selectors. CSS selectors help with the precise styling of a webpage. With a large webpage, rules may get a little overwhelming to remember, but with practice, CSS selectors offer a fast way to customize a webpage with more control over each of the web elements.

More stories

how to create a market-ready application
Considerations While Crafting the Optimal Web Application

Find out how to create a market-ready application based on changing consumer trends,

Read more
Kubernetes to expand cloud possibilities
Kubernetes - The Ideal Cloud Platform Choice

Google’s Kubernetes cloud-based app deployment platform as an optimal solution for developers and businesses due to the framework’s versatile architecture and features like scalability, immutability and separation of concern.

Read more
Data wrangling in Python
Data Wrangling in Python

A real-life implementation example, using the Monte Carlo simulation method, to demonstrate MATLAB to C/C++ conversion and MEX-functions

Read more
How to use Regular Expressions in Python

Learn how to work with regular expressions in Python to solve pattern-matching problems for which operators may not be enough.

Read more
some basics of adding interactivity to a webpage using JavaScript.
Introduction to JS Browser Events for Web Designers

Learn some basics of adding interactivity to a webpage using JavaScript. A new web designer or anyone wanting to revise the basics of JS browser events will find this article helpful.

Read more
Dijkstra Algorithm  In Python
Shortest Paths Dijkstra Algorithm In Python

Find insight about methods that would reduce costs, save time and energy in many aspects of lives.

Read more

Algo uses cookies for personalization and other purposes. Learn more about cookies use. Algo supports the Digital Advertising Alliance principles. By interacting with this site, you agree to our use of cookies.