ボタンのホバーエフェクト
CSS Tips
目次
基本のボタンのホバーエフェクト
背景色を反転させる
<button class="button">BUTTON</button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
display: flex;
justify-content: center;
align-items: center;
transition: all 0.3s;
}
.button:hover {
cursor: pointer;
color: #fff;
background-color: #000;
}
背景色を半透明にする
<button class="button">BUTTON</button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
transition: all 0.3s;
}
.button:hover {
cursor: pointer;
opacity: 0.75;
}
矢印が右に動かす
<button class="button">BUTTON</button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
position: relative;
transition: all 0.3s;
}
.button:before {
width: 7px;
height: 7px;
border-top: 2px solid #fff;
border-right: 2px solid #fff;
content: "";
margin: auto;
position: absolute;
right: 20px;
top: 0;
bottom: 0;
transition: right 0.3s;
transform: rotate(45deg);
}
.button:hover {
cursor: pointer;
opacity: 1;
}
.button:hover:before {
right: 15px;
}
サイズを変える
<button class="button">BUTTON</button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
transition: all 0.3s;
}
.button:hover {
cursor: pointer;
transform: scale(1.1, 1.1);
}
<button class="button">BUTTON</button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
transition: all 0.3s;
}
.button:hover {
cursor: pointer;
transform: scale(0.9, 0.9);
}
立体的なボタン
押し込む
ホバー時に、少し立体的なボタンを凹ませて、クリックできることをわかりやすくします。
<button class="button">BUTTON</button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
box-shadow: 5px 5px 0 #ccc;
transition: all 0.3s;
}
.button:hover {
cursor: pointer;
box-shadow: unset;
transform: translate(4px, 4px);
}
背景が動くボタン
背景をスライドさせる
<button class="button"><supan>BUTTON</span></button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
display: flex;
justify-content: center;
align-items: center;
position: relative;
transition: color 0.5s ease;
}
.button:before {
width: 100%;
height: 100%;
content: "";
display: block;
background: #000;
position: absolute;
top: 0;
left: 0;
transform: scaleX(0);
transform-origin: right;
transition: all 0.5s ease;
transition-property: transform;
}
.button:hover {
color: #fff;
}
.button:hover:before {
transform: scaleX(1);
transform-origin: left;
}
.button span {
position: relative;
}
<button class="button"><supan>BUTTON</span></button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
display: flex;
justify-content: center;
align-items: center;
position: relative;
transition: color 0.5s ease;
}
.button:before {
width: 100%;
height: 100%;
content: "";
display: block;
background: #000;
position: absolute;
top: 0;
left: 0;
transform: scaleX(0);
transform-origin: left;
transition: all 0.5s ease;
transition-property: transform;
}
.button:hover {
color: #fff;
}
.button:before {
transform: scaleX(1);
transform-origin: right;
}
.button span {
position: relative;
}
<button class="button"><supan>BUTTON</span></button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
color: #000;
display: flex;
justify-content: center;
align-items: center;
position: relative;
transition: color 0.5s ease;
}
.button:before {
width: 100%;
height: 100%;
content: "";
display: block;
background: #000;
position: absolute;
top: 0;
left: 0;
transform: scaleX(0);
transition: all 0.5s ease;
transition-property: transform;
}
.button:hover {
color: #fff;
}
.button:hover:before {
transform: scaleY(1);
}
.button span {
position: relative;
}
<button class="button"><supan>BUTTON</span></button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
color: #000;
display: flex;
justify-content: center;
align-items: center;
position: relative;
transition: color 0.5s ease;
}
.button:before {
width: 100%;
height: 100%;
content: "";
display: block;
background: #000;
position: absolute;
top: 0;
left: 0;
transform: scaleY(0);
transition: all 0.5s ease;
transition-property: transform;
}
.button:hover {
color: #fff;
}
.button:hover:before {
transform: scaleY(1);
}
.button span {
position: relative;
}
テキストリンクに下線を表示する
下線を表示する
<a href="#" class="link">LINK</a>
.link {
color: #000;
text-decoration: none;
position: relative;
}
.link:after {
width: 100%;
height: 2px;
content: '';
visibility: hidden;
opacity: 0;
background: #000;
position: absolute;
left: 0;
bottom: -5px;
transition: 0.5s;
}
.link:hover:after {
visibility: visible;
opacity: 1;
transition: 0.5s;
}
<a href="#" class="link">LINK</a>
.link {
border-bottom: 2px;
color: #000;
text-decoration: none;
position: relative;
}
.link:after {
width: 100%;
height: 2px;
content: '';
background: #000;
position: absolute;
left: 0;
bottom: -5px;
transform: scale(0, 1);
transform-origin: right top;
transition: transform 0.5s;
}
.link:hover:after {
transform: scale(1, 1);
transform-origin: left top;
}
<a href="#" class="link">LINK</a>
.link {
border-bottom: 2px;
text-decoration: none;
color: #000;
position: relative;
}
.link:hover{
color: #000;
}
.link:after {
width: 100%;
height: 2px;
content: '';
background: #000;
position: absolute;
left: 0;
bottom: -5px;
transform: scale(0, 1);
transform-origin: left top;
transition: transform 0.5s;
}
.link:hover:after {
transform: scale(1, 1);
transform-origin: right top;
}
<a href="#" class="link">LINK</a>
.link {
border-bottom: 2px;
text-decoration: none;
color: #000;
position: relative;
}
.link:after {
width: 100%;
height: 2px;
content: '';
background: #000;
position: absolute;
left: 0;
bottom: -5px;
transform: scale(0, 1);
transition: transform 0.5s;
transform-origin: center top;
}
.link:hover:after {
transform: scale(1, 1)
}
マーカーを表示する
<a href="#" class="link">LINK</a>
.link {
color: #000;
text-decoration: none;
position: relative;
z-index: 1;
}
.link:before {
width: 100%;
height: 100%;
content: "";
display: block;
background: #000;
position: absolute;
left: 0;
bottom: 0;
transform: scale(0, 1);
transform-origin: right top;
transition: transform 0.3s;
z-index: -1;
}
.link:hover {
color: #fff;
}
.link:hover:before {
transform-origin: left top;
transform: scale(1, 1);
}
画像のホバーアニメーション
zoom
<a href="#" class="link"><img src="PATH TO IMAGE" /></a>
.link {
width: 200px;
height: 200px;
overflow: hidden;
}
.link img {
width: 200px;
height: 200px;
object-fit: cover;
transition: 0.3s;
}
.link img:hover {
transform: scale(1.2);
}
テキストを表示
<a href="#" class="link" data-text="HOVER TEXT"><img src="PATH TO IMAGE" /></a>
.link {
width: 200px;
height: 200px;
display: block;
object-fit: cover;
position: relative;
}
.link:before {
width: 100%;
height: 100%;
color: #fff;
font-size: 2.0rem;
font-weight: bold;
content: attr(data-text);
opacity: 0;
visibility: hidden;
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: 0;
left: 0;
transition: 0.3s;
}
.link:hover {
cursor: pointer;
}
.link:hover:before {
opacity: 1;
visibility: visible;
}
.link img {
width: 200px;
height: 200px;
object-fit: cover;
}
その他いろいろなホバーエフェクト
文字間を広げる
<button class="button">BUTTON</button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
position: relative;
transition: all 0.3s;
}
.button:hover {
letter-spacing: 3px;
text-indent: 3px;
}
ボタンが回転する
<button class="button">BUTTON</button>
.button {
width: 200px;
height: 50px;
border: 1px solid #000;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
background-color: #000;
}
.button:hover {
transition: 1s;
transform: rotateX(360deg);
}
グラデーションの色が流れる
<button class="button">BUTTON</button>
.button {
width: 200px;
height: 50px;
border: none;
color: #fff;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(to right, #000, #999);
background-position: 0% 0;
background-size: 200% 100%;
transition: all 0.3s ease-out;
}
.button:hover {
color: #fff;
background-position: 100% 0;
}