目次
1. はじめに
Google が開発しているデザインシステム「Material」の中には、アイコンも含まれています。
このアイコンは、Icons と呼ばれているもので、誰でも利用することができます。
本ページでは、この Icons を Webページに導入する手順について説明します。
2. Icons の導入手順
2-1. Google Web Fonts を利用する場合の導入手順
Icons はアイコンフォントであるため、Google Fonts でもホスティングされています。
こちらを利用するには、Webページ内の <head>
要素内に以下を記述子ます。
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
rel="stylesheet">
これだけです。
あとは HTML 内で
<i class="material-icons">launch</i>
というように書けばアイコンを表示することができます(アイコンによって “launch” の部分が変わります)。
どのようなアイコンがあって、それぞれどう記述するかについては、Icons ページで確認できます。
Icons には 5つのテーマがあります
Icons は、以下の 5つのテーマ が用意されています。
- Filled(塗りつぶし)
- Outlined(輪郭線)
- Rounded(角丸)
- Two-Tone(2色)
- Sharp(角が四角)
1つのアイコンに対して、それぞれのテーマで見た目が変わりますが、アイコンによっては変わらないものもあります。
以下は、3つのアイコンをそれぞれのテーマで表示しています。
テーマ別に導入することもできますし、すべてのテーマを同時に導入することもできます。
テーマ別に導入する場合は、以下の <link>
タグを記述します。
Filled
<link href="https://fonts.googleapis.com/css?family=Material+Icons" rel="stylesheet">
- アイコンの記述例:
<i class="material-icons">account_box</i>
Outlined
<link href="https://fonts.googleapis.com/css?family=Material+Icons+Outlined" rel="stylesheet">
- アイコンの記述例:
<i class="material-icons-outlined">account_box</i>
Rounded
<link href="https://fonts.googleapis.com/css?family=Material+Rounded" rel="stylesheet">
- アイコンの記述例:
<i class="material-icons-round">account_box</i>
Two-Tone
<link href="https://fonts.googleapis.com/css?family=Material+Icons+Two+Tone" rel="stylesheet">
- アイコンの記述例:
<i class="material-icons-two-tone">account_box</i>
Sharp
<link href="https://fonts.googleapis.com/css?family=Material+Icons+Sharp" rel="stylesheet">
- アイコンの記述例:
<i class="material-icons-sharp">account_box</i>
すべて
<link href="https://fonts.googleapis.com/css?family=Material+Icons|Material+Icons+Outlined|Material+Icons+Two+Tone|Material+Icons+Round|Material+Icons+Sharp" rel="stylesheet">
- アイコンの記述は、上の5つの例を参照してください。
2-2. 自分のWebサイトに設置する場合の導入手順
Filled テーマのアイコンを導入する例です。
手順
(1) material-design-icons/iconfont | GitHub からフォントファイルをダウンロードします。
面倒な場合は、以下のコマンドでダウンロードすることもできます(5行あります)。
$ wget --max-redirect=2 https://github.com/google/material-design-icons/blob/master/font/MaterialIcons-Regular.ijmap?raw=true -O MaterialIcons-Regular.ijmap
$ wget --max-redirect=2 https://github.com/google/material-design-icons/blob/master/font/MaterialIcons-Regular.svg?raw=true -O MaterialIcons-Regular.svg
$ wget --max-redirect=2 https://github.com/google/material-design-icons/blob/master/font/MaterialIcons-Regular.ttf?raw=true -O MaterialIcons-Regular.ttf
$ wget --max-redirect=2 https://github.com/google/material-design-icons/blob/master/font/MaterialIcons-Regular.woff?raw=true -O MaterialIcons-Regular.woff
$ wget --max-redirect=2 https://github.com/google/material-design-icons/blob/master/font/MaterialIcons-Regular.woff2?raw=true -O MaterialIcons-Regular.woff2
(2) Webページから読み込むCSSファイルに、以下を記述します。
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url(../font/MaterialIcons-Regular.eot); /* For IE6-8 */
src: local('Material Icons'),
local('MaterialIcons-Regular'),
url(../font/MaterialIcons-Regular.woff2) format('woff2'),
url(../font/MaterialIcons-Regular.woff) format('woff'),
url(../font/MaterialIcons-Regular.ttf) format('truetype');
}
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px; /* Preferred icon size */
display: inline-block;
line-height: 1;
text-transform: none;
letter-spacing: normal;
word-wrap: normal;
white-space: nowrap;
direction: ltr;
/* Support for all WebKit browsers. */
-webkit-font-smoothing: antialiased;
/* Support for Safari and Chrome. */
text-rendering: optimizeLegibility;
/* Support for Firefox. */
-moz-osx-font-smoothing: grayscale;
/* Support for IE. */
font-feature-settings: 'liga';
}
- フォントファイルへのパスは適宜修正します。
3. 便利な CSSクラスを定義する
アイコンの大きさや色を CSSクラスで指定できるようにしておくと便利です。
例えば以下のスタイルを CSSファイルに追記します。
/* Rules for sizing the icon. */
.material-icons.md-18 { font-size: 18px; }
.material-icons.md-24 { font-size: 24px; }
.material-icons.md-36 { font-size: 36px; }
.material-icons.md-48 { font-size: 48px; }
/* Rules for using icons as black on a light background. */
.material-icons.md-dark { color: rgba(0, 0, 0, 0.54); }
.material-icons.md-dark.md-inactive { color: rgba(0, 0, 0, 0.26); }
/* Rules for using icons as white on a dark background. */
.material-icons.md-light { color: rgba(255, 255, 255, 1); }
.material-icons.md-light.md-inactive { color: rgba(255, 255, 255, 0.3); }
- 上記は .material-icons クラスに対してのみ定義していますが、必要に応じて .material-icons-outlined クラスなどに対しても定義します。
- 例えば、
<i class="material-icons md-18">stars</i>
と記述すれば font-size が 18px でアイコン表示できます。 - 詳細は Material Icons Guide – Google Design を参照してください。