How to apply font like a boss

@font-face work around

CSSsassreact

2017-10-20


1. Create @font-face mixin

๋จผ์ €, ์ผ๋ฐ˜์ ์ธ css์˜ ํฐํŠธ ์ฝ”๋“œ๋ฅผ ๋ณด์ž.

@font-face {
    font-family: "Noto Sans CJK KR";
    font-style: normal;
    font-weight: 900;
    src: url("./fonts/NotoSans-Black.eot?") format("eot"),
        url("./fonts/NotoSans-Black.woff2") format("woff2"),
        url("./fonts/NotoSans-Black.woff") format("woff");
    }

@font-face๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด, ์‚ฌ์šฉ์ž ์ปดํ“จํ„ฐ์— ํ•ด๋‹น ํฐํŠธ๊ฐ€ ์„ค์น˜๋˜์–ด ์žˆ์ง€ ์•Š์•„๋„ ํฐํŠธ๋ฅผ ๋””์Šคํ”Œ๋ ˆ์ด ํ•  ์ˆ˜ ์žˆ๋‹ค. eot, woff, woff2 ๋Š” ํฐํŠธ ํฌ๋งท์œผ๋กœ, ์„œ๋กœ ๋‹ค๋ฅธ ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ง€์›ํ•œ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์—ฌ๊ธฐ ์ฐธ๊ณ .

css ํŒŒ์ผ์„ ์“ด๋‹ค๋ฉด font-family ์ด๋ฆ„๊ณผ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํฌ๋งท์„ ์ง€์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค. ํฐํŠธ๊ฐ€ 1-2๊ฐœ์ด๋ฉด ๊ดœ์ฐฎ์ง€๋งŒ ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ๋  ๊ฒฝ์šฐ, ๊ท€์ฐฎ์„ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์‹ค์ˆ˜ํ•˜๊ธฐ๋„ ์‰ฝ๋‹ค. ๋Œ€๊ฐœ๋Š” ํฐํŠธ๋ช…๊ณผ font-weight ๋งŒ ๋‹ฌ๋ผ์ง€๊ณ  ๊ฐ™์€ ๋‚ด์šฉ์ธ๋ฐ, ์ด ์ž‘์—…์„ ์•„๋ž˜์˜ ์ฝ”๋“œ๋กœ ๋ฐ˜๋ณตํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค. ์ถœ์ฒ˜

/* _mixins.scss */
@mixin font-face($name, $path, $weight: null, $style: null, $exts: eot woff2 woff ttf svg) {
	$src: null;
	$extmods: (
		eot: "?",
		svg: "#" + str-replace($name, " ", "_")
	);
	$formats: (
		otf: "opentype",
		ttf: "truetype"
	);
	@each $ext in $exts {
		$extmod: if(map-has-key($extmods, $ext), $ext + map-get($extmods, $ext), $ext);
		$format: if(map-has-key($formats, $ext), map-get($formats, $ext), $ext);
		$src: append($src, url(quote($path + "." + $extmod)) format(quote($format)), comma);
	}
	@font-face {
		font-family: quote($name);
		font-style: $style;
		font-weight: $weight;
		src: $src;
	}
}

2. Import mixin!

@importํ‚ค์›Œ๋“œ๋กœ mixin์„ ์ž„ํฌํŠธํ•˜๊ณ , @include ํ‚ค์›Œ๋“œ๋กœ ๋งŒ๋“ค์–ด์ค€ @font-face mixin ์„ ์‚ฌ์šฉํ•œ๋‹ค.

/* App.scss */
@import "../stylesheets/_mixins.scss";

@include font-face("Noto Sans CJK KR", "./fonts/NotoSans-Black", 900, normal, eot woff2 woff);

.App {
    font-family: "Noto Sans CJK KR";
    -webkit-font-smoothing: antialiased;
}

์—ฌ๊ธฐ๊นŒ์ง€ ํ•˜๋ฉด ๋!

...์ด๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ํฐํŠธ๊ฐ€ ์ ์šฉ์ด ๋˜์ง€ ์•Š์•˜๋‹ค. ๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ๊ผผ์ˆ˜๋ฅผ ์จ๋ณด์•˜๋‹ค.


3. Create Font.scss

ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์˜ ๋ฌธ์„œ ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์•˜๋‹ค. (* create-react-app)

.
โ”œโ”€โ”€ App.css
โ”œโ”€โ”€ App.js
โ”œโ”€โ”€ App.scss
โ”œโ”€โ”€ index.css
โ”œโ”€โ”€ index.js
โ”œโ”€โ”€ components
โ”‚ย ย  โ”œโ”€โ”€ About
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ About.css
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ About.js
โ”‚ย ย  โ”‚ย ย  โ”œโ”€โ”€ About.scss
โ”‚ย ย  โ”œโ”€โ”€ Event
โ”‚ย ย  โ”œโ”€โ”€ Header
โ”‚ย ย  โ””โ”€โ”€ index.js
โ”œโ”€โ”€ fonts
โ”‚ย ย  โ”œโ”€โ”€ NotoSans-Black.eot
โ”‚ย ย  โ”œโ”€โ”€ NotoSans-Black.otf
โ”‚ย ย  โ”œโ”€โ”€ NotoSans-Black.woff2
โ”‚ย ย  โ””โ”€โ”€ NotoSans-Black.woff
โ””โ”€โ”€ stylesheets
    โ”œโ”€โ”€ _mixins.scss
    โ””โ”€โ”€ _variables.scss

์ƒ๋Œ€๊ฒฝ๋กœ ./fonts/NotoSans-Black.woff๋กœ ์ง€์ •ํ•ด์ค€ ํฐํŠธ๊ฐ€ ๋ฐ›์•„์ง€์ง€ ์•Š๋Š” ๊ฒƒ์ด์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํฐํŠธ๊ฐ€ ๋“ค์–ด์žˆ๋Š” fonts/ ํด๋”์— font.scssํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  npm run watch-css ๋ช…๋ น์–ด๋กœ sass ํŒŒ์ผ์„ ์ปดํŒŒ์ผํ•ด์ค€๋‹ค. (node-sass-chokidar๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.) ๊ทธ๋Ÿฌ๋ฉด css ํŒŒ์ผ์ด ์ƒ๊ธด๋‹ค.

โ”œโ”€โ”€ fonts
โ”‚ย ย  โ”œโ”€โ”€ font.css
โ”‚ย ย  โ”œโ”€โ”€ font.scss
โ”‚ย ย  โ”œโ”€โ”€ NotoSans-Black.eot
โ”‚ย ย  โ”œโ”€โ”€ NotoSans-Black.otf
โ”‚ย ย  โ”œโ”€โ”€ NotoSans-Black.woff2
โ”‚ย ย  โ””โ”€โ”€ NotoSans-Black.woff
โ””โ”€โ”€ stylesheets
    โ”œโ”€โ”€ _mixins.scss
    โ””โ”€โ”€ _variables.scss

์ด font.scss ํŒŒ์ผ ์•ˆ์—์„œ _mixins.scss๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ณ  ์ด font.css ํŒŒ์ผ์„ index.css ์— ์ž„ํฌํŠธ ํ•ด์ฃผ์—ˆ๋‹ค.

/* font.scss */
@import "../stylesheets/_mixins.scss";

@include font-face("Noto Sans CJK KR", "./NotoSans-Black", 900, normal, eot woff2 woff);
/* index.css */
@import url('./fonts/font.css');

์ •๋ฆฌ!

  1. @font-face ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ฃผ๋Š” _mixins.scss๋ฅผ ๋งŒ๋“ ๋‹ค.
  2. font ํŒŒ์ผ์ด ์žˆ๋Š” ํด๋”์— font.scss๋ฅผ ๋งŒ๋“ ๋‹ค.
  3. @include font-face๋ฅผ ์‚ฌ์šฉํ•ด ์‰ฝ๊ฒŒ font-face๋ฅผ ๋งŒ๋“ ๋‹ค.
  4. ์ด๋ ‡๊ฒŒ ๋งŒ๋“  font.scss ๊ฐ€ ์ƒ์„ฑํ•œ font.css ํŒŒ์ผ์„ index.css ํŒŒ์ผ์— importํ•œ๋‹ค.

๋!