<div class="layout">
<!-- ここからボタンのHTML -->
<!-- 背景が左から右に流れる -->
<div class="button-01">
<a href="#"><span>ボタン</span></a>
</div>
<!-- 背景が右から左に流れる -->
<div class="button-02">
<a href="#"><span>ボタン</span></a>
</div>
<!-- ここまでボタンのHTML -->
</div>
/* ここはレイアウトなのでボタンとは関係ない */
* {
margin: 0;
}
.layout {
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
gap: 50px;
width: 100%;
height: 100vh;
}
/* ここからボタンのCSS */
//背景が左から右に流れるボタン
.button-01 {
a {
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 240px;
height: 50px;
font-size: 16px;
text-decoration: none;
transition: 0.3s;
//ボタンのテキストの色
color: #5cc0ef;
//ボタンの背景の色(transparentを推奨)
background-color: transparent;
//ボタンの線の色
border: 1px solid #5cc0ef;
span {
position: relative;
}
&::before {
content: "";
position: absolute;
top: 0;
left: 0;
display: block;
width: 100%;
height: 100%;
transform: scaleX(0);
transform-origin: right;
transition: all 0.3s ease;
transition-property: transform;
//左から右に流れる背景の色
background: #5cc0ef;
}
&:hover {
color: #fff;
&::before {
transform: scaleX(1);
transform-origin: left;
}
}
}
}
//背景が右から左に流れるボタン
.button-02 {
a {
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 240px;
height: 50px;
font-size: 16px;
text-decoration: none;
transition: 0.3s;
//ボタンのテキストの色
color: #5cc0ef;
//ボタンの背景の色(transparentを推奨)
background-color: transparent;
//ボタンの線の色
border: 1px solid #5cc0ef;
span {
position: relative;
}
&::before {
content: "";
position: absolute;
top: 0;
left: 0;
display: block;
width: 100%;
height: 100%;
transform: scaleX(0);
transform-origin: left;
transition: all 0.3s ease;
transition-property: transform;
//右から左に流れる背景の色
background: #5cc0ef;
}
&:hover {
color: #fff;
&::before {
transform: scaleX(1);
transform-origin: right;
}
}
}
}
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.