問題5:検索窓にアニメーションをつけよう
今回は検索窓の装飾をしてみましょう!
検索窓は多くの人が利用するパーツです。装飾してサイトを面白くしましょう!!
検索窓は多くの人が利用するパーツです。装飾してサイトを面白くしましょう!!
あらかじめエディタに書くコード
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.1/css/quot;>
</head>
<body>
<form method="get" action="#">
<input type="text" placeholder="キーワード">
<input type="submit" value="" class="fas">
</form>
</body>
</html>
¥HTML¥
form{
position: relative;
width: 200px;
height: 30px;
}
input:first-of-type{
box-sizing: border-box;
width: inherit;
height: inherit;
background-color:#dfd;
border: none;
outline: none;
}
input:last-of-type{
position: absolute;
top: 0;
right: 0;
box-sizing: border-box;
height: inherit;
border:none;
background-color: transparent;
cursor: pointer;
outline: none;
}
¥CSS¥
期待する画面
解答ソースコード
input:first-of-type:focus{
border: 2px solid;
background-color: #fff;
}
¥CSS¥
完了にする!
活動記録をTweetする
実は上記コードの通りclassがついてないなくてもcssを適用できます。first-of-typeは最初の〜にlast-of-typeは最後の〜にと指定しています。
では本題ですが、inputをクリックすると何かを打ち込めたりすると思うのですが、この時にcssを適用するには擬似要素focusを用います。focus時にアニメーションをつけると、選択できているかどうかを識別しやすくなります。