What are CSS Rules and Selectors?
Tags: css, webdev, beginners, tutorial
Published: 2022-07-20 10:19:00

Firstly, it’s a good idea to get familiar with what CSS is and how it is used. I have created another article explaining this topic. Here is a link to learn more about [CSS and how to use it](https://maxlockwood.dev/cascading-style-sheets-css-are-used-in-what-ways/)

## CSS Syntax

The style rules in CSS are interpreted by the browser and then applied to the matching elements in your document.

A style rule has three parts: selector, property, and value.

For example, the headline color can be defined as:

```css
h1 { color: blue; }
```

![CSS Syntax](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gu75ymex4efhlof7bmwf.png)

The selector points to the [HTML element](https://maxlockwood.dev/what-are-html-elements/) you want to style.

One or more declarations are contained in the declaration block and are separated by semicolons.
A colon separates the property name from the value in each declaration.

## Type Selectors

Type selectors are the most widely used and simple to comprehend selectors. This selector focuses on certain page element types.

For example, to target all the paragraphs on the page:

```css
p {
  text-align: center;
  color: red;
  font-size: 18px;
}
```

Here, all `<p>` elements on the page will be center-aligned, with a text color that is `red` and a `font-size` of `18px` applied.

## Id and Class Selectors

### Id Selectors

Regardless of where they are in the document tree, id selectors let you style an HTML element that has an id attribute. The id of an element is unique within a page, so the id selector is used to select one unique element!

A sample id selector is shown here:

```html
<div id=“unique”>
    <p> This paragraph is in the intro section.</p>
</div>
<p> This paragraph is not in the intro section.</p>
```

```css
#unique {
  color: white;
  background-color: coral;
}
```

Use a hash character (`#`) followed by the element’s id to choose an element with a specified id.

## Class Selectors

Similar principles apply to class selectors. The primary distinction between the two is that classes may be used as many times as necessary on a page but IDs may only be applied once per page.

```html
<div>
    <p class=“first”>This is a paragraph</p>
    <p> This is the second paragraph. </p>
</div>
```

```css
.first {font-size: 30px;}
```
Use a period character (`.`) followed by the name of the class to select elements that belong to that class.
A class or ID name should NOT begin with a number!

## The CSS Grouping Selector

All of our selectors up to this point have only focused on a single class, ID, or element. But in CSS multiple elements can be chosen and styled collectively using the CSS grouping selector. Declaring common styles for each element saves time and minimises the amount of code needed. Each selector is separated from the others by a comma.

In this example we have grouped the selectors:

```css
h1, h2, p {
  text-align: center;
  color: orange;
}
```

For a complete list of all the different types of selectors, see MDN Web Docs [CSS selectors reference](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors).