今回は上のアイキャッチのような虹色のボタンを作っていきます
ちなみにこの虹色は動きます。
簡単なCSSとアニメーションなので皆さん知っている方も多いかと思いますが、紹介していこうと思います。
すぐ終わりますm(_ _)m
HTMLの準備
ほとんど何も入ってないHTMLです
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>CSS Sample</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrap">
<a class="box" href="#">Hello World</a>
</div>
</body>
</html>
CSSの準備
/* 例なのでbodyにflexかけて上下左右中央揃えにしてます */
body {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
height: 100vh;
background: #000;
}
/* Hello Worldの後ろの黒い背景です。 */
.wrap {
position: relative;
background: #000;
-webkit-transition: .3s;
-o-transition: .3s;
transition: .3s;
}
/*
Hello Worldの直下の背景です
clipでテキストをマスクしています
animationで背景の虹色と同じアニメーションをつけています。
*/
.box {
display: block;
text-decoration: none;
color: transparent;
padding: 50px;
font-size: 32px;
font-weight: bold;
-webkit-transition: .3s;
-o-transition: .3s;
transition: .3s;
background: -webkit-linear-gradient(45deg,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783);
background: -o-linear-gradient(45deg,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783);
background: linear-gradient(45deg,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783);
background-size: 200%;
background-clip: text;
-webkit-background-clip: text;
-webkit-animation: bgAnimation 5s linear infinite;
animation: bgAnimation 5s linear infinite;
}
/* 一番後ろの背景に上下左右2pxずつ大きい虹色の背景を作っています */
.wrap::before,
.wrap::after {
-webkit-transition: .6s;
-o-transition: .6s;
transition: .6s;
content: '';
position: absolute;
top: -2px;
left: -2px;
background: -webkit-linear-gradient(45deg,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783);
background: -o-linear-gradient(45deg,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783);
background: linear-gradient(45deg,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783,#E60012,#F39800,#FFF100,#009944,#0068B7,#1D2088,#920783);
background-size: 200%;
width: calc(100% + 4px);
height: calc(100% + 4px);
z-index: -1;
-webkit-animation: bgAnimation 5s linear infinite;
animation: bgAnimation 5s linear infinite;
}
/* afterだけにぼかしをつけています */
.wrap::after {
-webkit-filter: blur(10px);
filter: blur(10px);
}
/* ホバーのエフェクトです */
.wrap:hover::before,
.wrap:hover::after {
top: -25px;
left: -25px;
width: calc(100% + 50px);
height: calc(100% + 50px);
border-radius: 25px;
}
.wrap:hover {
border-radius: 25px;
background: rgba(0, 0, 0,0.9);
}
.wrap:hover .box {
-webkit-transform: rotate(-35deg) scale(1.2);
-ms-transform: rotate(-35deg) scale(1.2);
transform: rotate(-35deg) scale(1.2);
}
.wrap:hover::after {
-webkit-filter: blur(50px);
filter: blur(50px);
}
/* animationで一番下の背景のポジションを動かしています */
@-webkit-keyframes bgAnimation {
0% {
background-position: 0 0;
}
50% {
background-position: 100% 0;
}
100% {
background-position: 0 0;
}
}
@keyframes bgAnimation {
0% {
background-position: 0 0;
}
50% {
background-position: 100% 0;
}
100% {
background-position: 0 0;
}
}
CSSはいっぱい書いていますが、コメントで説明書いていますので、カスタムしてみてください^ ^
CSSのfilter: blur();
のブラウザサポートはこんな感じです
background-clipはこんな感じ
使う際は環境に気をつけてくださいね
以上超簡単ですが、終わります^ ^
では〜〜〜