|
| 1 | +--- |
| 2 | +title: SASS and CSS |
| 3 | +--- |
| 4 | + |
| 5 | +::: {.pale-blue} |
| 6 | + |
| 7 | +**On this page we will:** |
| 8 | + |
| 9 | +- Understand, in simple terms, what Sass and CSS are. |
| 10 | +- See when you might use each in a Quarto site. |
| 11 | +- Make changes using a `.scss` file and a `.css` file. |
| 12 | + |
| 13 | +::: |
| 14 | + |
| 15 | +When you customise the look of a Quarto site, you are changing how the **web page is styled**. |
| 16 | + |
| 17 | +Web pages are styled with **CSS**, which stands for "Cascading Style Sheets". CSS is a simple styling language that tells the browser things like: |
| 18 | + |
| 19 | +* "Make all main headings this colour". |
| 20 | +* "Add extra space around this box". |
| 21 | +* "Use this font for the body text". |
| 22 | + |
| 23 | +There is also **Sass**, which you can think of as a **helper language for writing CSS**. Sass lets you define variables (for example, `$navbar-bg` for the navbar background colour). Quarto themes come with a set of Sass variables you can override, so you can change key theme‑level settings without writing lots of detailed rules. |
| 24 | + |
| 25 | +In practice in a Quarto site: |
| 26 | + |
| 27 | +* Use **Sass** when want to change **theme-level settings** (and there is a Sass variable for it), such as overall background, text, or navbar colours. |
| 28 | +* Use **CSS** when want to **add or adjust specific styles**, such as a custom class for a callout, extra spacing, or the look of a particular box or heading. |
| 29 | + |
| 30 | +## Sass |
| 31 | + |
| 32 | +You use Sass in Quarto by creating an `.scss` file and listing it under `theme:`. |
| 33 | + |
| 34 | +Quarto HTML themes expose many Sass variables you can override (for example, `$body-bg`, `$body-color`, `$navbar-bg`). You can see a fuller list in the [Quarto documentation](https://quarto.org/docs/output-formats/html-themes.html#sass-variables). |
| 35 | + |
| 36 | +Example SCSS theme file: |
| 37 | + |
| 38 | +```scss |
| 39 | +$body-bg: #fdf6e3; |
| 40 | +$body-color: #002b36; |
| 41 | +$navbar-bg: #d33682; |
| 42 | +``` |
| 43 | + |
| 44 | +> Here the values like `#d33682` are **hex colour codes**. A hex code is a way of writing a colour using six characters after a `#`. You can find these by searching online, or can pick them from an image using a [hex colour picker](https://imagecolorpicker.com/). |
| 45 | +
|
| 46 | +Then in `_quarto.yml`: |
| 47 | + |
| 48 | +```{.yaml} |
| 49 | +format: |
| 50 | + html: |
| 51 | + theme: [default, styles.scss] |
| 52 | +``` |
| 53 | + |
| 54 | +This says, start with `default` theme, then apply my overrides from `styles.scss`. |
| 55 | + |
| 56 | +::: {.pale-blue} |
| 57 | + |
| 58 | +**Task**: |
| 59 | + |
| 60 | +* [ ] Create a `styles.scss` file with at least one Sass variable (for example, change `$navbar-bg` or `$body-bg`). |
| 61 | +* [ ] Add the SCSS file to your theme configuration in `_quarto.yml`. |
| 62 | +* [ ] Render the site and check the colour changes. |
| 63 | +::: |
| 64 | + |
| 65 | +## CSS |
| 66 | + |
| 67 | +You use CSS in Quarto by creating a `.css` file and listing it under `format:` → `html:` → `css:` in `_quarto.yml` (or in an individual page's YAML): |
| 68 | + |
| 69 | +```{.yaml} |
| 70 | +format: |
| 71 | + html: |
| 72 | + css: custom.css |
| 73 | +``` |
| 74 | + |
| 75 | +Inside `custom.css`, you write rules like: |
| 76 | + |
| 77 | +```{.css} |
| 78 | +h1, h2 { |
| 79 | + letter-spacing: 0.03em; |
| 80 | +} |
| 81 | +
|
| 82 | +.pale-blue { |
| 83 | + background-color: #e6f2fa; |
| 84 | + color: black; |
| 85 | + border: 2px solid #9ecae1; |
| 86 | + border-radius: 8px; |
| 87 | + padding: 1.2em 1em 0.5em 1em; |
| 88 | + margin: 1.5em 0; |
| 89 | + position: relative; |
| 90 | + box-shadow: 0 2px 8px rgba(25, 118, 210, 0.15); |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +- The `h1, h2` rule changes the spacing between letters for level‑1 and level‑2 headings. |
| 95 | +- The `.pale-blue` rule controls how any element with class `pale-blue` is styled - this example is the blue boxes we used for tasks on this site. |
| 96 | + |
| 97 | +::: {.pale-blue} |
| 98 | + |
| 99 | +**Task:** |
| 100 | + |
| 101 | +- [ ] Create a `custom.css` file in your project. |
| 102 | +- [ ] Add at least one simple rule (for example, change heading letter‑spacing, or adjust the `.pale-blue` box styling). |
| 103 | +- [ ] Add `css: custom.css` under `format: html:` in `_quarto.yml`. |
| 104 | +- [ ] Render the site and confirm that your CSS change appears. |
| 105 | + |
| 106 | +::: |
0 commit comments