This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
oliverdavies.uk-old-sculpin/source/_posts/include-css-fonts-using-sass-each-loop.md

68 lines
2 KiB
Markdown
Raw Permalink Normal View History

2015-03-16 16:16:06 +00:00
---
title: Include CSS Fonts by Using a SASS each Loop
2020-03-08 14:32:13 +00:00
date: 2014-11-18
2018-12-31 12:13:05 +00:00
excerpt: How to use an SASS each loop to easily add multiple fonts to your CSS.
2015-03-16 16:16:06 +00:00
tags:
2015-06-14 02:27:41 +00:00
- compass
- drupal-planet
- fonts
- sass
2015-03-16 16:16:06 +00:00
---
2015-06-15 09:47:06 +00:00
How to use an @each loop in SASS to quickly include multiple font files within
your stylesheet.
Using a file structure similar to this, organise your font files into
directories, using the the font name for both the directory name and for the
file names.
2015-03-16 16:16:06 +00:00
2017-03-16 08:09:52 +00:00
```language-bash
2015-03-16 16:16:06 +00:00
.
├── FuturaBold
2015-06-14 02:27:41 +00:00
│ ├── FuturaBold.eot
│ ├── FuturaBold.svg
│ ├── FuturaBold.ttf
│ └── FuturaBold.woff
2015-03-16 16:16:06 +00:00
├── FuturaBoldItalic
2015-06-14 02:27:41 +00:00
│ ├── FuturaBoldItalic.eot
│ ├── FuturaBoldItalic.svg
│ ├── FuturaBoldItalic.ttf
│ └── FuturaBoldItalic.woff
2015-03-16 16:16:06 +00:00
├── FuturaBook
2015-06-14 02:27:41 +00:00
│ ├── FuturaBook.eot
│ ├── FuturaBook.svg
│ ├── FuturaBook.ttf
│ └── FuturaBook.woff
2015-03-16 16:16:06 +00:00
├── FuturaItalic
2015-06-14 02:27:41 +00:00
│ ├── FuturaItalic.eot
│ ├── FuturaItalic.svg
│ ├── FuturaItalic.ttf
│ └── FuturaItalic.woff
2017-03-16 08:09:52 +00:00
```
2015-03-16 16:16:06 +00:00
Within your SASS file, start an `@each` loop, listing the names of the fonts. In
the same way as PHP's `foreach` loop, each font name will get looped through
using the `$family` variable and then compiled into CSS.
2015-03-16 16:16:06 +00:00
2017-03-16 08:09:52 +00:00
```language-scss
2015-03-16 16:16:06 +00:00
@each $family in FuturaBook, FuturaBold, FuturaBoldItalic, FuturaItalic {
2015-06-14 02:27:41 +00:00
@font-face {
font-family: #{$family};
src: url('../fonts/#{$family}/#{$family}.eot');
src: url('../fonts/#{$family}/#{$family}.eot?#iefix') format('embedded-opentype'),
url('../fonts/#{$family}/#{$family}.woff') format('woff'),
url('../fonts/#{$family}/#{$family}.ttf') format('truetype'),
url('../fonts/#{$family}/#{$family}.svg##{$family}') format('svg');
font-weight: normal;
font-style: normal;
}
2015-03-16 16:16:06 +00:00
}
2017-03-16 08:09:52 +00:00
```
2015-03-16 16:16:06 +00:00
When the CSS has been compiled, you can then use in your CSS in the standard
way.
2015-03-16 16:16:06 +00:00
2017-03-16 08:09:52 +00:00
```language-scss
font-family: "FuturaBook";
```