Using CSS selectors to target specific elements

CSS selectors are a fundamental part of working with CSS and allow you to target specific elements on a webpage to style them in a specific way. There are several types of selectors that you can use, and each of them has their own unique syntax and capabilities. In this post we are going to explore some of them.

Element Selector

One of the most basic types of CSS selectors is the element selector. An element selector in CSS allows you to target all elements of a specific type on a webpage and apply styles to them. The syntax for an element selector is simply the name of the element you want to target. For example, to target all p elements (paragraphs) on a webpage, you would use the p element selector as shown below:

p {
  color: green;
  font-size: 14px;
  font-weight: bold;

This would apply the styles of color green, a font size 14px and a font-weight of bold to all paragraphs on the webpage.

Element selectors can also be used to target multiple element types at the same time. For example, to target both paragraphs and headings, you could use the following syntax:

p, h1, h2, h3, h4, h5, h6 {
  color: green;
  font-size: 14px;

This would apply the above styles to all paragraphs and headings on the webpage.

Class Selector

Another common type of CSS selectors is the class selector. Class selector in CSS allows you to create a custom class that you can apply to any element on a webpage. This allows you to reuse the same styles across multiple elements, rather than having to define the styles for each element individually.

The syntax for a class selector is a period (.) followed by the name of the class. For example, to create a class called success, you would use the following syntax:

<p class="success">It was a success!</p>

Then, you could apply CSS style to this class:

.success {
  color: green;
  font-weight: bold;

You can also apply multiple classes to a single element by separating the class names with a space. For example:

<p class="success congrat">Congrat, it was a success!</p>

This would apply both the success and congrat classes to the paragraph element. Class selectors are a useful tool for creating reusable styles

ID Selector

An ID selector in CSS allows you to target a specific element on a webpage by its ID attribute. The ID attribute is a unique identifier that is assigned to an element in the HTML code and is used to identify that element specifically.

The syntax for an ID selector is a hashtag (#) followed by the value of the ID attribute. For example, to target an element with an ID of content, you would use the following syntax:

#content {
  background-color: gray;
  border: 1px solid gray;

To assign an ID to an element in the HTML code, you would use the id attribute:

<div id="content">
  <!-- content goes here -->

This would apply the styles of a gray background color and a gray border to the div element with the ID of content.

It’s important to note that an ID should only be used once on a webpage. If you try to use the same ID multiple times, it will result in an error.


A pseudo-class in CSS is a keyword that is used to target a specific state of an element. These states are not represented in the HTML code and are instead generated by the user’s actions or the element’s context on the webpage.

One common example of a pseudo-class is the :hover pseudo-class, which allows you to apply styles to an element when the user hovers over it with their mouse. For example, to change the color of all links on a webpage when the user hovers over them, you could use the syntax below:

a:hover {
  color: purple;

There are several other pseudo-classes that you can use, such as :active, which targets an element when it is being activated (e.g., when a button is being clicked), and :focus, which targets an element when it has focus (e.g., when an input field is selected).


You can also use pseudo-classes to target elements based on their position in the document, such as the :first-child pseudo-class, which targets the first child element of its parent, and the :nth-child(n) pseudo-class, which targets the nth child element of its parent.

CSS selectors are an essential tool for any web developer and allow you to fine-tune the appearance of your website to create the perfect user experience. Whether you’re just starting out with CSS or are a seasoned pro, learning how to effectively use selectors is an important skill to have. Shallout!!!