> For the complete documentation index, see [llms.txt](https://chnn-anne.gitbook.io/html-css/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chnn-anne.gitbook.io/html-css/html-css-intro/css-intro.md).

# CSS

## CSS - Cascading Style Sheets

HTML is used to give structure to the page content. CSS was introduced to allow the browser to apply specific styling features such as colors, fonts, and positioning to the HTML elements on the page.&#x20;

![https://blog.templatetoaster.com/what-is-css/](https://836634774-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MeN44h3QMT7WRlkbx4e%2F-MfEJdRk08shIq_hWiuU%2F-MfENXCZ1yLiJFk78mgL%2Fimage.png?alt=media\&token=5e5de65d-9815-4a16-afb1-23ba951c0dcc)

With CSS, we can use a stylesheet language to specify which styling properties we want to change for specific HTML elements.

### CSS Rules

To change the style of an element you must target the element using a CSS Rule.

![](https://836634774-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MeN44h3QMT7WRlkbx4e%2F-MhZKJMC67VVZxq2gSyR%2F-MhZOL_ypLw4PHZkzAIN%2Fimage.png?alt=media\&token=5903068a-1aff-4619-9120-3c46525a8452)

A CSS rule is made up of **selectors**, **properties**, and **values**.

* A **selector** describes which HTML elements to style
  * `h1` is the **selector**
* A **property** is the specific style to apply
  * `font-size` is the **property**
* A **value** is the value for the property
  * `1.5rem` is the **value**
* A **declaration** is the combination of the property and value
  * `font-size: 1.5rem;` is the **declaration**
* A **rule** is the combination of the selector and its declarations
  * `h1 { font-size: 1.5rem; }` is the **rule**

{% hint style="success" %}
**Check for understanding**. In the CSS code below, which parts are the rules, selectors, properties, values, and declarations?
{% endhint %}

```css
h1, h2, h3, h4, h5, h6, p {
  color: #333;
  margin-bottom: 2rem;
}

.box {
  border: 1px solid #333;
}
```

###

### Three Methods for Applying Styles

**1. External Stylesheets** - styles are loaded from an external stylesheet referenced in a `<link>` element. This is the **preferred method**, as all of the styles are maintained in one place and can be shared across web pages.

```markup
<head>
    <title>Jellies From the Deep</title>
    <link rel="stylesheet" href="styles.css">
</head>
```

```css
// styles.css

a {
  text-decoration: none;
  color:red;
  font-size:36px;
}

#pink-jelly {
  color: deeppink;
}

#yellow-jelly {
  color: #ff9900;
}

#orange-jelly {
  color: #ff3300;
}

li:before {
  content: '🎐'
}
```

**2. Embedded Stylesheets** - styles embedded directly in the html `<style>` element within the `<head>` section of the page. This method is discouraged because it is harder to maintain and re-use styles.

```markup
<head>
    <title>Jellies From the Deep</title>
    <style>
      a {
        text-decoration: none;
        color:red;
        font-size:36px;
      }
      #pink-jelly {
        color: deeppink;
      }

      #yellow-jelly {
        color: #ff9900;
      }

      #orange-jelly {
        color: #ff3300;
      }
      li:before {
        content: '🎐'
      }
    </style>
</head>
```

**3. Inline** - styles are specified as attributes directly on the HTML element to which you want to apply the style.  This method is also discourages for the reasons explain for embedded styles.

```markup
<h2 style="color:#ff9900">Orange Jelly</h2>
```
