In this video, we will learn about the most important CSS selectors and pseudo-elements. We will also cover attribute selectors, which are less common but can be very powerful. ## CSS Selectors The most basic CSS selector is the `*` selector, which selects all elements on the page. We can use the `type` selector to select elements by their type, such as `div`, `span`, or `a`. We can also use the `class` selector to select elements by their class name. To do this, we add a period (`.`) before the class name. For example, `.red` would select all elements with the class `red`. The `id` selector is similar to the class selector, but it is more specific. To use the id selector, we add a pound sign (`#`) before the id name. For example, `#myid` would select the element with the id `myid`. We can combine selectors together using the `,` operator to select multiple elements or the `>` operator to select child elements. For example, `div > ul > li` would select all `li` elements that are children of `ul` elements that are children of `div` elements. ## Pseudo-elements Pseudo-elements are used to select elements based on their position or state. For example, the `:hover` pseudo-element selects elements when the user hovers over them. The `:focus` pseudo-element selects elements when the user focuses on them. We can also use pseudo-elements to select elements based on their position in the document. For example, the `:first-child` pseudo-element selects the first child of an element. The `:last-child` pseudo-element selects the last child of an element. ## Attribute Selectors Attribute selectors are used to select elements based on their attributes. To use an attribute selector, we add brackets (`[]`) around the attribute name. We can also use the `==` operator to check for a specific value of the attribute. For example, `[data-red=true]` would select all elements with the `data-red` attribute that has the value `true`. ## Cheat Sheet Here is a cheat sheet of all the CSS selectors that we covered in this video: | Selector | Description | |---|---| | `*` | Selects all elements on the page | | `type` | Selects elements by their type | | `class` | Selects elements by their class name | | `id` | Selects elements by their id | | `,` | Combines selectors to select multiple elements | | `>` | Selects child elements | | `:hover` | Selects elements when the user hovers over them | | `:focus` | Selects elements when the user focuses on them | | `:first-child` | Selects the first child of an element | | `:last-child` | Selects the last child of an element | | `[attr]` | Selects elements by their attribute | | `[attr=value]` | Selects elements by their attribute value | | `[attr^=value]` | Selects elements by their attribute value that starts with `value` | | `[attr$=value]` | Selects elements by their attribute value that ends with `value` | | `[attr*=value]` | Selects elements by their attribute value that contains `value` |
What is the most common pseudo class used in CSS?
Which pseudo element is used to add content before an element?
What is the difference between a class selector and an ID selector?