1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Google Fontsをダウンロードして自分のウェブサイトでホスティングする

Posted at

Google Fontsをダウンロードして自分のウェブサイトでホスティングする

1 動機

日本語と英語の2言語対応のWebサイトの制作時に、次のような課題がありました。

  • cssファイル中でfont-family: monospace;を利用している文字の見た目がlang=jalang=enで異なる

下図にその例を示します。Click me!のフォントはどちらもmonospaceですが、<html lang="ja"><html lang="en">で表示されるフォントが異なることがわかります。

image.png

上図の比較したときのHTMLとCSSファイルで掲載します。

/qiita/ja/index.html
<!-- 日本語版のサイト -->
<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="UTF-8">
  <title>日本語版のサイト</title>
  <link rel="stylesheet" href="../assets/css/style.css">
</head>

<body>
  <h1>ようこそ</h1>
  <a href="#">Click me!</p>
</body>

</html>
/qiita/en/index.html
<!-- 英語版のサイト -->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>英語版のサイト</title>
  <link rel="stylesheet" href="../assets/css/style.css">
</head>

<body>
  <h1>Welcome!</h1>
  <a href="#">Click me!</p>
</body>

</html>
/qiita/assets/css/style.css
* {
  line-height: 1;
  font-family: monospace;
  padding: 0;
  margin: 0;
  box-sizing: content-box;
}

h1 {
  font-size: 32px;
  line-height: 1.5;
}

セレクタ*は、全称セレクタまたはユニバーサルセレクタです。

monospace等幅フォントと呼ばれるもので、アルファベットによる文字幅の違いを吸収するために利用されます。プログラミング言語などを入力するテキストエディタ内でも等幅フォントが推奨されます。

<h1>要素のセレクタにline-height: 1.5を追加する理由は、フォントサイズ(32px)に対して、コンテンツ領域の高さを1.5倍(48px)に固定するためです。

2 Goole Fontsの利用

Google Fontsには、等幅フォントで、かつ英語/日本語ページの両方で同一の見た目を表示できるフォントファミリがあるそうです。そのフォントファミリをダウンロードし、自分でホスティングする手順をこれから説明します。

2.1 Google Fontsのダウンロード

Google Fonts にアクセスし、検索ボックスにRobot Monoと入力して、フィルタされた1件の候補を選択します。

image.png

右上のGet fontボタンをクリックするとダウンロードが始まります。

image.png

ダウンロードが完了したら、.zipファイルを展開します。フォント情報を含むファイルは拡張子.ttfのファイルに保存されています。

image.png

2.2 ファイル形式の変換

.ttf.woff2に変換するため、Transfonter にアクセスします。Add fontsから先ほど解凍したファイルのうち使用するフォント (RobotoMono-Bold.ttfRobotoMono-Regular.ttfとします)をアップロードし、下図の設定の状態でConvert (変換)を実行します。完了後、.zipファイルのダウンロードが始ます。

image.png

.zipファイルを解凍すると次のようなファイルが手元にできます。

image.png

2.3 プログラム変更

次のフォルダ構成になるように.woff2ファイルを配置します。

/assets/
  /fonts/
    /roboto-mono/
      RobotoMono-Regular.woff2
      RobotoMono-Bold.woff2
  /css/
    fonts-local.css
    style.css
/en/
  index.html
/ja/
  index.html

fonts-local.cssを新規追加し、index.htmlstyle.cssを書き換えます。

font-local.css
/* Roboto Mono */
@font-face {
  font-family: "Roboto Mono Local";
  src: url("../fonts/roboto-mono/RobotoMono-Regular.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Roboto Mono Local";
  src: url("../fonts/roboto-mono/RobotoMono-Bold.woff2") format("woff2");
  font-weight: 700;
  font-style: normal;
  font-display: swap;
}
/en/index.html
<!-- 英語版のサイト -->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>英語版のサイト</title>
  <link rel="stylesheet" href="../assets/css/fonts-local.css">
  <link rel="stylesheet" href="../assets/css/style.css">
</head>

<body>
  <h1>Welcome!</h1>
  <a href="#">Click me!</p>
</body>

</html>
/ja/index.html
<!-- 日本語版のサイト -->
<!DOCTYPE html>
<html lang="ja">

<head>
  <meta charset="UTF-8">
  <title>日本語版のサイト</title>
  <link rel="stylesheet" href="../assets/css/fonts-local.css">
  <link rel="stylesheet" href="../assets/css/style.css">
</head>

<body>
  <h1>ようこそ</h1>
  <a href="#">Click me!</p>
</body>

</html>
style.css
* {
  line-height: 1;
  font-family: 'Roboto Mono Local';
  padding: 0;
  margin: 0;
  box-sizing: content-box;
}

h1 {
  font-size: 32px;
  line-height: 1.5;
}

3 結果

編集後の/en/index.html/ja/index.htmlを再度見比べてみます。Click me!の見た目が同じになりました。

image.png

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?