グラデーションカラーのボタンを作成する - CSS

CSSでボタンの表面(背景色)がグラデーションカラーのボタンを作成するコードを紹介します。

グラデーションボタン

ボタンのキャプションが文字列のグラデーションボタンを作成します。

コード

以下のコードを記述します。
button-gradient.css (cssファイル)
.gradientButton{
  border:1px solid #ff6a00;
  width:100px;
  height:28px;
  background:linear-gradient(to bottom, #fcffa2,#fbb700);
}
button-gradient.html (htmlファイル)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
  <link rel="stylesheet" type="text/css" href="button-gradient.css" />
</head>
<body>
  <div><button type="submit" class="gradientButton">Button</button></div>
  <hr />
</body>
</html>

解説

スタイルシートの下記コードにより、ボタンの背景色をグラデーションに設定します。以前はブラウザごとにグラデーションを指定するCSSが異なりましたが、上記のコードを用いると、IE10,11,Firefox,Chromeでグラデーション表示できます。
background:linear-gradient(to bottom, #fcffa2,#fbb700);

実行結果

上記のHTMLファイルを表示します。下図の画面が表示されます。グラデーションカラーのボタンが表示できました。

アイコンつきグラデーションボタン

グラデーションボタンにアイコンを表示するコードです。

画像の準備

こちらの記事で用いている画像と同じ画像を用います。(16x16ピクセル、背景色透過、PNGファイル。画像はimgディレクトリ内に"search.png"という名称で配置。)

コード

以下のコードを記述します。
image-button-gradient.css (cssファイル)
#buttonImage {
  background-image:url('/img/search.png');
  display:inline-block;
  margin-top:2px;
  width:16px;
  height:16px;
}

.flatbutton{
  border:1px solid #3079ed;
  width:100px;
  height:28px;
  background:linear-gradient(to bottom, #9bcfff,#4683ea); 
}
image-button-gradient.html (HTMLファイル)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title></title>
  <link rel="stylesheet" type="text/css" href="image-button-gradient.css" />
</head>
<body>
  <button class="flatbutton"><span id="buttonImage"></span></button>

</body>
</html>

解説

先ほどの文字列キャプションのグラデーションボタンとほぼ同じです。画像を表示するためにbuttonタグ内にspanタグを記述しています。ボタン内に画像を表示する方法はこちらの記事を参照してください。

実行結果

上記のHTMLファイルを実行すると下図の画面が表示されます。グラデーションボタンにアイコンが表示できています。

著者
iPentecのメインデザイナー
Webページ、Webクリエイティブのデザインを担当。PhotoshopやIllustratorの作業もする。
掲載日: 2014-03-11
iPentec all rights reserverd.