頑張りすぎず頑張ろう

気になったことをとにかく書いていきます

【CSS】ボタンをアニメーションで作ろう【HTML】

index.html

 

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTMLとCSS3でつくるアニメーションボタン</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<button class="button type1">button1</button> 
<button class="button type2">button2</button> ホバー時に背景を変える
<button class="button type3">button3</button> ホバー時に左右に開くアニメーション
<button class="button type4">button4</button> ホバー時に上下に開くアニメーション
<button class="button type5">button5</button> 中心から外側に開くアニメーション
</body>
</html>

 

style.css

 

body {
padding: 0;
margin: 0;
}

.button {
color: #FFF;
background: #2c366d;
border: 2px solid #2c366d;
padding: 0 40px;
line-height: 40px;
font-size: 21px;
margin: 10px;
outline: 0;
box-sizing: border-box;
position: relative;
z-index: 1;
}

.button.type1:hover {
background: #46BED6;
border-color: #46BED6;
}

.button.type2:hover {
color: #46BED6;
background: #FFF;
border-color: #46BED6;
}
.button,
.button::before,
.button::after {
box-sizing: border-box;
transition: all 0.3s;
}

.button::before,
.button::after {
content: "";
display: block;
position: absolute;
z-index: -1;
}

.button.type3::before,
.button.type3::after {
top: 0;
width: 50%;
height: 100%;
background: #2c366d;
}

.button.type3::before {
left: 0;
}

.button.type3::after {
right: 0;
}

.button.type3:hover {
color: #46BED6;
background: #FFF;
border-color: #46BED6;
}

.button:hover.type3::before,
.button:hover.type3::after {
width: 0;
}

.button.type4::before,
.button.type4::after {
width: 100%;
height: 50%;
left: 0;
background: #2c366d;
}

.button.type4::before {
top: 0;
}

.button.type4::after {
bottom: 0;
}

.button.type4:hover {
color: #46BED6;
background: #FFF;
border-color: #46BED6;
}

.button:hover.type4::before,
.button:hover.type4::after {
height: 0;
}

.button.type5 {
color: #46BED6;
background: #FFF;
border-color: #46BED6;
}

.button.type5::after {
top: 0;
left: 0;
width: 100%;
height: 100%;
transform: scale(0.5);
}

.button.type5:hover {
color: #FFF;
border-color: #2c366d;
}

.button:hover.type5::after {
background: #2c366d;
transform: scale(1);
}