Skip to Content
👆 We offer 1-on-1 classes as well check now

CSS Selectors

CSS Selectors are the fundamental building blocks of styling websites. They tell your browser which HTML elements you want to style. Think of them as the address you give to tell the browser “Hey, style this part of the page!”

What is a CSS Selector?

A CSS selector is a pattern used to select and apply styles to specific HTML elements. Selectors can target elements based on their HTML tag (like <p> or <h1>), their class (e.g., <div class="my-class">), or their ID (e.g., <div id="unique-id">). Understanding selectors is crucial for controlling the appearance of your web pages. They are the “targets” for your styling rules.

Basic Example: Tag Selectors

Let’s say we want to change the color of all paragraphs (<p>) on our page to blue. We would use a tag selector.

<!DOCTYPE html> <html> <head> <title>CSS Selectors Example</title> <style> p { color: blue; } </style> </head> <body> <p>This is a paragraph.</p> <p>Another paragraph here.</p> </body> </html>

Explanation: In this example, p is the selector. The code color: blue; is a style rule that sets the text color to blue for every <p> element. Everything inside the curly braces {} applies to the selected element(s).

Practical Usage: Class Selectors

Now, let’s say we have some special paragraphs that we want to highlight with a different background color. We can use a class selector.

<!DOCTYPE html> <html> <head> <title>CSS Selectors Example</title> <style> .highlight { background-color: yellow; } </style> </head> <body> <p>This is a regular paragraph.</p> <p class="highlight">This is a highlighted paragraph.</p> <p class="highlight">Another highlighted paragraph.</p> </body> </html>

Explanation: In this example, .highlight is the class selector. Notice the dot (.) before the class name. We assign the class “highlight” to specific <p> elements in the HTML. Only those paragraphs with the class “highlight” will get the yellow background. You can apply the same class to multiple elements.

Practical Usage: ID Selectors

ID selectors are used to select a single, unique element on a page. Think of it like a unique identifier.

<!DOCTYPE html> <html> <head> <title>CSS Selectors Example</title> <style> #unique-heading { font-size: 2em; /* Twice the default font size */ color: red; } </style> </head> <body> <h1 id="unique-heading">This is a unique heading!</h1> <p>Some other content.</p> </body> </html>

Explanation: Here, #unique-heading is the ID selector. Notice the hash symbol (#). We’ve assigned the ID “unique-heading” to a single <h1> element. Only that heading will be styled with a larger font size and red color. IDs should be unique; use them sparingly.

Key Takeaways

  • Selectors target HTML elements: They tell the browser what to style.
  • Tag selectors target HTML tags (e.g., p, h1).
  • Class selectors target elements with a specific class (e.g., .my-class). Use them to style multiple elements.
  • ID selectors target a single, unique element with a specific ID (e.g., #unique-id). Use them for unique styling.
  • Mastering selectors is crucial for controlling the look and feel of your website!
Last updated on