Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Selectors in CSS?

In this blog post, we will be exploring the world of web design and the role that selectors play in creating beautiful and functional websites. As someone who is just learning programming, you may find it challenging to grasp some of the concepts and terminologies, but don't worry; we'll break everything down in simple terms with clear examples.

A Brief Introduction to CSS

Before we dive into selectors, let's take a step back and understand what CSS is. CSS stands for Cascading Style Sheets, and it is a language used to define the look and formatting of a document, usually written in HTML (HyperText Markup Language) or XHTML (Extensible HyperText Markup Language). In simple terms, HTML is responsible for the structure and content of a webpage, while CSS controls the visual appearance and layout.

What are Selectors?

Selectors are the foundation of CSS. They help us target specific elements or groups of elements on a webpage, so that we can apply styles to them. Think of selectors as a way of "selecting" elements on a page, like picking apples from a tree. Once you've picked the apples (selected the elements), you can then apply various actions to them, such as changing their color or size.

Now that we have a basic understanding of what selectors are, let's dive deeper into the different types of selectors available in CSS and how to use them effectively.

Basic Selectors

1. Element Selector

An element selector targets all instances of a specific HTML element. For example, if you want to change the styling of all paragraphs on your webpage, you would use the p selector.

Here's an example:

p {
  font-size: 16px;
  color: blue;
}

In this example, we are targeting all p (paragraph) elements and applying a font size of 16 pixels and a blue color to them.

2. Class Selector

A class selector targets elements that have a specific class attribute. Classes are a way to group elements that share common styles. To define a class selector in CSS, we use a period . followed by the class name.

Here's an example:

<p class="highlight">This is a highlighted paragraph.</p>
.highlight {
  background-color: yellow;
}

In this example, the paragraph with the class highlight will have a yellow background color.

3. ID Selector

An ID selector targets a specific element with a unique ID attribute. IDs, unlike classes, should be unique within an HTML document. To define an ID selector in CSS, we use a hash # followed by the ID name.

Here's an example:

<p id="main-content">This is the main content of the page.</p>
#main-content {
  font-size: 24px;
  font-weight: bold;
}

In this example, the paragraph with the ID main-content will have a font size of 24 pixels and the text will be bold.

Combinators

Combinators allow us to create more complex selectors by combining two or more basic selectors. We'll discuss four types of combinators: descendant, child, adjacent sibling, and general sibling.

1. Descendant Combinator

A descendant combinator targets elements that are descendants of a specific element. In CSS, we denote a descendant combinator by a space between two selectors.

Here's an example:

<div>
  <p>This is a paragraph inside the div element.</p>
</div>
<p>This is a paragraph outside the div element.</p>
div p {
  color: red;
}

In this example, only the paragraph inside the div element will have a red color. The paragraph outside the div element will remain unaffected.

2. Child Combinator

A child combinator targets elements that are direct children of a specific element. In CSS, we denote a child combinator by a greater-than sign > between two selectors.

Here's an example:

<div>
  <p>This is a direct child of the div element.</p>
  <ul>
    <li><p>This is a paragraph inside a list item.</p></li>
  </ul>
</div>
div > p {
  font-style: italic;
}

In this example, only the paragraph that is a direct child of the div element will have an italic font style. The paragraph inside the list item will remain unaffected.

3. Adjacent Sibling Combinator

An adjacent sibling combinator targets elements that are immediate siblings of a specific element. In CSS, we denote an adjacent sibling combinator by a plus sign + between two selectors.

Here's an example:

<h1>Title</h1>
<p>This is a paragraph immediately following the title.</p>
<p>This is another paragraph.</p>
h1 + p {
  font-size: 18px;
}

In this example, only the paragraph immediately following the h1 element will have a font size of 18 pixels. The other paragraph will remain unaffected.

4. General Sibling Combinator

A general sibling combinator targets elements that are siblings of a specific element. In CSS, we denote a general sibling combinator by a tilde ~ between two selectors.

Here's an example:

<h1>Title</h1>
<p>This is a paragraph following the title.</p>
<p>This is another paragraph.</p>
h1 ~ p {
  text-decoration: underline;
}

In this example, both paragraphs following the h1 element will have an underline text decoration.

Pseudo-classes and Pseudo-elements

Pseudo-classes and pseudo-elements are special selectors that allow you to target elements based on their state or position in the document.

Pseudo-classes

Pseudo-classes target elements based on their state, such as when a button is being hovered over or an input field has focus. Some common pseudo-classes include :hover, :active, :focus, and :nth-child.

Here's an example using the :hover pseudo-class:

<button>Hover over me!</button>
button:hover {
  background-color: green;
}

In this example, the button's background color will change to green when the user hovers over it.

Pseudo-elements

Pseudo-elements target specific parts of an element, such as the first letter or before/after the content. Some common pseudo-elements include ::first-letter, ::before, and ::after.

Here's an example using the ::first-letter pseudo-element:

<p>This is a paragraph with a large first letter.</p>
p::first-letter {
  font-size: 24px;
  font-weight: bold;
}

In this example, the first letter of the paragraph will have a font size of 24 pixels and the text will be bold.

Conclusion

Selectors are a powerful tool in CSS, allowing you to target specific elements or groups of elements on a webpage to apply styles. By understanding and utilizing the different types of selectors, combinators, and pseudo-classes/elements, you can create visually appealing and functional websites. As you continue learning programming and working with CSS, you'll become more comfortable with these concepts and be able to create more complex and engaging designs.