How to Include CSS
There are three ways to include CSS. The first is internal stylesheets, typically used for learning only.
| |
The second is inline styles, usually paired with JavaScript.
| |
The last method is external stylesheets. You write CSS in a separate file and link it using a link tag. This is common practice in development.
| |
CSS Selectors
Selectors are used to find elements and apply styles. There are four basic selector types.
Element Selectors
Use the element (tag) name directly as the selector. This applies the same style to all elements with that name.
| |
Common elements you can use include p, h1, div, a, img, etc.
Class Selectors
Used to find elements and apply different styles.
| |
If a class name has multiple words, use a hyphen (-) to connect them. A single class selector can be used by multiple elements, and one element can have multiple class names.
| |
ID Selectors
Used to find elements and apply different styles, usually in conjunction with JavaScript.
| |
An ID selector should only be used once per page.
Universal Selectors
Selects all elements on the page and applies the same style. Commonly used to clear default browser styles.
| |
Combinator Selectors
Definition: Formed by combining two or more basic selectors in different ways.
Purpose: To select target elements (tags) more precisely and efficiently.
Descendant Selectors
Can select all descendant elements of a specific element.
Syntax: parent-selector descendant-selector { CSS properties }
Parent and child selectors are separated by a space. They can be class or ID selectors.
| |
Child Selectors
Only selects direct child elements of a specific element.
Syntax: parent-selector > child-selector { CSS properties }
| |
Grouping Selectors
Selects multiple sets of elements to apply the same styles.
Syntax: selector1, selector2, ..., selectorN { CSS properties }
| |
Chained Selectors
Selects elements that meet multiple conditions simultaneously.
Syntax: selector1selector2 { CSS properties } Selectors are written consecutively, with no symbols in between.
If a chained selector includes an element selector, the element selector must be written first.
| |
Pseudo-class Selectors
Pseudo-classes represent an element’s state, letting you style an element based on its current state.
Hover State
Syntax: selector:hover { CSS properties }
| |
Any element can have a hover state applied.
Anchor Link States
| Selector | Purpose |
|---|---|
| :link | Before visit |
| :visited | After visit |
| :hover | Mouse hover |
| :active | While clicking (active) |
If you’re styling all four states for an anchor link, they should be written in this order.
| |
Structural Pseudo-class Selectors
Purpose: Select elements based on their structural relationship.
| Selector | Description |
|---|---|
| E:first-child | Selects the first E element. |
| E:last-child | Selects the last E element. |
| E:nth-child(N) | Selects the Nth E element (starting from 1). |
| |
For the third selector, you can use formulas to select multiple elements.
| Function | Formula |
|---|---|
| Even-numbered elements | 2n |
| Odd-numbered elements | 2n+1 or 2n-1 |
| Find elements that are multiples of 5 | 5n |
| Find elements from the 5th onwards (inclusive) | n+5 |
| Find elements up to the 5th (inclusive) | -n+5 |
Pseudo-element Selectors
Purpose: Create virtual elements (pseudo-elements) to display decorative content.
| Selector | Description |
|---|---|
| E::before | Adds a pseudo-element before the content of the E element. |
| E::after | Adds a pseudo-element after the content of the E element. |
Note:
- You must set the
contentproperty to define the pseudo-element’s content. If there’s no content, leave the quotes empty. - Pseudo-elements are inline by default.
- Their priority is the same as element selectors.
| |