LoginSignup
0
2

More than 3 years have passed since last update.

Railsにフォントを導入するまで

Last updated at Posted at 2020-09-26

はじめに

ネット上には素晴らしいフリーフォントがたくさんあります。
デザインの表現の幅を広げるために是非、活用していきたいところ。
そこで、フリーフォントをRailsに導入していきます。

フォントファイルの導入

ここでは導入したいフォントファイルをsmog.otfとします。
publicフォルダにfontsフォルダを作り、そこにフォントファイルを配置。

- public
    - fonts
        - smog.otf

使用するcssファイル(ここではsample.css)に以下のコードを追加します。
font-familyの名前は自由に設定できます、今回は'Smog'としましょう。
またformatはファイルのフォーマットに合わせます。

フォーマット 拡張子
format('woff') .woff
format('truetype') .ttf
format('opentype') .otf または .ttf
format('embedded-opentype') .eot
format('svg') .svg または .svgz
sample.css
@font-face {
  font-family: "Smog";
  src: asset-url('/fonts/smog.otf') format('opentype');
  font-weight: normal;
  font-style: normal;
}

あとはいつも通りcssでフォントを指定すれば使えます。
以下は例です。

sample.css
.foo h1 {
  font-family: 'Smog';
}
index.html.erb
<div class="foo">
  <h1>hello!</h1>
</div>

以上です、お疲れ様でした!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2