新たに始まった連載
ボタンのもとになる静的スタイル
まず、<body>
要素に書くコードだ。プッシュボタンにする<a>
要素は、class
属性"button"を定めた<p>
要素に含める。それをさらに、class
属性"container"の<div>
要素で包んだ。
<body>
<div class="container">
<p class="button"><a href="#">Push Button</a></p>
</div>
</body>
プッシュボタンのもととなるスタイル<style>
要素で定める。まだ、

<style>
body {
background: white;
font: 30px sans-serif;
}
.button a {
display: block;
width: 200px;
margin: 0 auto;
padding: 10px;
color: white;
text-align: center;
text-decoration: none;
background-color: lightseagreen;
}
</style>
ボタンの角に丸みを加える
ボタンらしく、border-radius
だ。プロパティの値には、
border-radius: 角丸の半径
プッシュボタンclass
属性"button")<a>
要素に、
.button a {
border-radius: 5px;
}

影でボタンを立体的に見せる
さらに、box-shadow
プロパティでボタンに影を加えたい。オプションinset
は後で用いる。水平・
box-shadow: [inset] 水平のずれ 垂直のずれ ぼけ幅 カラー
プッシュボタンclass
属性"button")<a>
要素には、rgba()
関数で半透明にしている。
.button a {
box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.6);
}

box-shadow
プロパティには、inset
を与えると、
.button a {
box-shadow: inset 4px 4px 4px rgba(255, 255, 255, 0.6),
inset -4px -4px 4px rgba(0, 0, 0, 0.4);
}

プッシュボタンの外側右下の影も加えた<style>
要素の記述はつぎのとおりだ。これで、
body {
background: white;
font: 30px sans-serif;
}
.button a {
display: block;
width: 200px;
margin: 0 auto;
padding: 10px;
color: white;
text-align: center;
text-decoration: none;
background-color: lightseagreen;
border-radius: 5px;
box-shadow: inset 4px 4px 4px rgba(255, 255, 255, 0.6),
inset -4px -4px 4px rgba(0, 0, 0, 0.4),
4px 4px 8px rgba(0, 0, 0, 0.6);
}

マウスポインタを重ねたときのボタンの表現
プッシュボタンにマウスポインタを重ねたとき、<a>
要素の:hover
擬似クラスで、transform
プロパティで位置を少し下げる。translateY()
関数が垂直位置を動かす。これで、
.button a:hover {
background-color: mediumaquamarine;
box-shadow: inset 2px 2px 4px rgba(255, 255, 255, 0.6),
inset -2px -2px 4px rgba(0, 0, 0, 0.4),
2px 2px 4px rgba(0, 0, 0, 0.8);
transform: translateY(2px);
}

プッシュボタンのボックスの位置を下げるには、transform
プロパティでなく、top
プロパティで変えることもできる。だが、transform
は位置を動かすだけでなく、
.button a {
position:relative;
}
.button a:hover {
top: 2px;
}
プッシュボタン上でマウスボタンを押したときの表現は、<a>
要素の:active
擬似クラスにつぎのように定める。内側inset
オプション)
.button a:active {
color: gainsboro;
background-color: darkcyan;
box-shadow: inset -2px -2px 4px rgba(255, 255, 255, 0.4),
inset 2px 2px 4px rgba(0, 0, 0, 0.8),
1px 1px 1px rgba(0, 0, 0, 0.6);
transform: translateY(4px);
}

ベンダープレフィックスと-prefix-free
ひとつ注意しておかなければならないのは、transform
プロパティの仕様だ。本稿執筆時現在では-webkit
を加えることになる。
.button a:active {
-webkit-transform: translateY(4px);
transform: translateY(4px);
}
この連載はCSS3のアニメーションでどのような表現ができるのか、<script>
要素に読み込めばよく
<script src="lib/prefixfree.min.js"></script>

ボタンに滑らかなアニメーションを定める
擬似クラス:hover
と:active
に定めたプッシュボタンの見た目は、transition
プロパティ<a>
要素にプロパティを定めれば、
.button a {
transition: 0.5s;
}
プッシュボタンを押したときの動き:active
擬似クラス)transition
プロパティを定め直してもよい。だが、transition-duration
プロパティを使うことにしよう。
.button a:active {
transition-duration: 0.2s;
}
これで今回のお題のプッシュボタンのアニメーションはでき上がった。ここまで書いてきた<script>
と<style>
の定めはつぎのコード1のとおりだ。併せて、
<script src="lib/prefixfree.min.js"></script>
<style>
body {
background: white;
font: 30px sans-serif;
}
.button a {
display: block;
width: 200px;
margin: 0 auto;
padding: 10px;
color: white;
text-align: center;
text-decoration: none;
background-color: lightseagreen;
border-radius: 5px;
box-shadow: inset 4px 4px 4px rgba(255, 255, 255, 0.6),
inset -4px -4px 4px rgba(0, 0, 0, 0.4),
4px 4px 8px rgba(0, 0, 0, 0.6);
transition: 0.5s;
}
.button a:hover {
background-color: mediumaquamarine;
box-shadow: inset 2px 2px 4px rgba(255, 255, 255, 0.6),
inset -2px -2px 4px rgba(0, 0, 0, 0.4),
2px 2px 4px rgba(0, 0, 0, 0.8);
transform: translateY(2px);
}
.button a:active {
color: gainsboro;
background-color: darkcyan;
box-shadow: inset -2px -2px 4px rgba(255, 255, 255, 0.4),
inset 2px 2px 4px rgba(0, 0, 0, 0.8),
1px 1px 1px rgba(0, 0, 0, 0.6);
transform: translateY(4px);
transition-duration: 0.2s;
}
</style>