我正在练习HTML和CSS,想创建一个按钮。当鼠标悬停时,我希望按钮周围出现一个彩色边框。然而,不知怎么的,我陷入了困境,无法把事情做好。按钮周围的边框没有完全显示,也没有完全显示。如果有人能帮我找出我做错了什么,并给出解决方案,我将不胜感激。
@import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro);
h1 {
font-family: "Montserrat", sans-serif;
text-align: center;
padding-top: 20px;
color: #000;
}
h2 {
text-align: center;
font-family: "Montserrat", sans-serif;
color: #999;
}
.button {
width: 200px;
margin: 100px auto;
position: relative;
border: solid 2px #cbd4d9;
height: 55px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
/*border: solid green;*/
}
.button:hover .hoverBtn:before, .button:hover .hoverBtn:after {
opacity: 1;
-webkit-animation: openB 0.4s;
/* Chrome, Safari, Opera */
animation: open 0.4s;
animation-fill-mode:forwards;
animation-timing-function:cubic-bezier(0.39, 0.575, 0.565, 1) ;
animation-direction:normal;
border: solid yellow;
}
.button:hover .hoverBtn-bottom:before, .button:hover .hoverBtn-bottom:after {
opacity: 1;
-webkit-animation: open 0.4s;
/* Chrome, Safari, Opera */
animation: openB 0.4s;
animation-delay: 0.4s;
animation-fill-mode:forwards;
animation-timing-function: cubic-bezier(0.39, 0.575, 0.565, 1);
animation-direction: normal;
/*border: solid pink;*/
}
.button h2 {
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 0 auto;
text-align: center;
color: #587785;
font-family: "Source Sans Pro", sans-serif;
font-size: 22px;
line-height: 55px;
}
.hoverBtn {
width: 100%;
height: 55px;
position: absolute;
top: -1px;
}
.hoverBtn:before {
position: absolute;
content: "";
height: 0;
width: 0;
display: block;
opacity: 0;
border-top: solid 2px #517180;
border-left: solid 2px #517180;
-webkit-border-top-left-radius: 5px;
-moz-border-top-left-radius: 5px;
border-top-left-radius: 5px;
-webkit-border-bottom-left-radius: 5px;
-moz-border-bottom-left-radius: 5px;
border-bottom-left-radius: 5px;
right: 100px;
}
.hoverBtn:after {
position: absolute;
content: "";
height: 0;
width: 0;
display: block;
opacity: 0;
border-top: solid 2px #517180;
border-right: solid 2px #517180;
-webkit-border-top-right-radius: 5px;
-moz-border-top-right-radius: 5px;
border-top-right-radius: 5px;
-webkit-border-bottom-right-radius: 5px;
-moz-border-bottom-right-radius: 5px;
border-bottom-right-radius: 5px;
left: 100px;
}
.hoverBtn-bottom {
width: 100%;
height: 55px;
position: absolute;
/*border: solid blue;*/
top: 0px;
}
.hoverBtn-bottom:before {
position: absolute;
content: "";
height: 0;
width: 0;
display:block;
opacity: 0;
height: 55px;
border-bottom: solid 2px #517180;
-webkit-border-top-right-radius: 5px;
-moz-border-top-right-radius: 5px;
border-top-right-radius: 5px;
-webkit-border-bottom-right-radius: 5px;
-moz-border-bottom-right-radius: 5px;
border-bottom-right-radius: 5px;
right: 0;
}
.hoverBtn-bottom:after {
position: absolute;
content: "";
height: 0;
width: 0;
display: block;
opacity: 0;
height: 55px;
border-bottom: solid 2px #517180;
-webkit-border-top-left-radius: 5px;
-moz-border-top-left-radius: 5px;
border-top-left-radius: 5px;
-webkit-border-bottom-left-radius: 5px;
-moz-border-bottom-left-radius: 5px;
border-bottom-left-radius: 5px;
left: 0;
}
h2.credits {
text-align: center;
font-family: "Source Sans Pro", sans-serif;
color: #888;
font-size: 16px;
}
/*
*/
@keyframes open {
0% {
width: 0;
height: 0;
}
50% {
width: 100px;
height: 0px;
}
100% {
width: 100px;
height: 55px;
}
}
@keyframes openB {
0% {
width: 0px;
}
100% {
width: 100px;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="border.css">
<title>Border</title>
</head>
<body>
<h1>Hover Border Animation</h1>
<div class="button">
<h2 class="hoverBtn">Hobbies</h2>
<br>
<div class="hoverBtn-bottom"></div>
<div class="box-img-container"></div>
</div>
</body>
</html>
提前谢谢。
最简单的解决方案是在.hoverBtn:before
和:after
上用display: inline-block;
替换display: block;
。这样可以修复对齐,然后可以通过将边框为黄色(.button:hover .hoverBtn:before, .button:hover .hoverBtn:after
(的样式拆分为两个不同的样式来删除中间边框,然后删除前面的边框和后面的边框。
请参阅我在下面所做的CSS更改:
@import url(https://fonts.googleapis.com/css?family=Source+Sans+Pro);
h1 {
font-family: "Montserrat", sans-serif;
text-align: center;
padding-top: 20px;
color: #000;
}
h2 {
text-align: center;
font-family: "Montserrat", sans-serif;
color: #999;
}
.button {
width: 200px;
margin: 100px auto;
position: relative;
border: solid 2px #cbd4d9;
height: 55px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
/*border: solid green;*/
}
.button:hover .hoverBtn:before {
opacity: 1;
-webkit-animation: openB 0.4s;
/* Chrome, Safari, Opera */
animation: open 0.4s;
animation-fill-mode: forwards;
animation-timing-function: cubic-bezier(0.39, 0.575, 0.565, 1);
animation-direction: normal;
border: solid yellow;
border-right: none;
width: 200px;
}
.button:hover .hoverBtn:after {
opacity: 1;
-webkit-animation: openB 0.4s;
/* Chrome, Safari, Opera */
animation: open 0.4s;
animation-fill-mode: forwards;
animation-timing-function: cubic-bezier(0.39, 0.575, 0.565, 1);
animation-direction: normal;
border: solid yellow;
border-left: none;
width: 200px;
}
.button:hover .hoverBtn-bottom:before,
.button:hover .hoverBtn-bottom:after {
opacity: 1;
-webkit-animation: open 0.4s;
/* Chrome, Safari, Opera */
animation: openB 0.4s;
animation-delay: 0.4s;
animation-fill-mode: forwards;
animation-timing-function: cubic-bezier(0.39, 0.575, 0.565, 1);
animation-direction: normal;
/*border: solid pink;*/
}
.button h2 {
position: absolute;
top: 0;
left: 0;
right: 0;
margin: 0 auto;
text-align: center;
color: #587785;
font-family: "Source Sans Pro", sans-serif;
font-size: 22px;
line-height: 55px;
}
.hoverBtn {
width: 100%;
height: 55px;
position: absolute;
top: -1px;
}
.hoverBtn:before {
position: absolute;
content: "";
height: 0;
width: 0;
display: inline-block;
opacity: 0;
border-top: solid 2px #517180;
border-left: solid 2px #517180;
-webkit-border-top-left-radius: 5px;
-moz-border-top-left-radius: 5px;
border-top-left-radius: 5px;
-webkit-border-bottom-left-radius: 5px;
-moz-border-bottom-left-radius: 5px;
border-bottom-left-radius: 5px;
right: 100px;
}
.hoverBtn:after {
position: absolute;
content: "";
height: 0;
width: 0;
display: inline-block;
opacity: 0;
border-top: solid 2px #517180;
border-right: solid 2px #517180;
-webkit-border-top-right-radius: 5px;
-moz-border-top-right-radius: 5px;
border-top-right-radius: 5px;
-webkit-border-bottom-right-radius: 5px;
-moz-border-bottom-right-radius: 5px;
border-bottom-right-radius: 5px;
left: 100px;
}
.hoverBtn-bottom {
width: 100%;
height: 55px;
position: absolute;
/*border: solid blue;*/
top: 0px;
}
.hoverBtn-bottom:before {
position: absolute;
content: "";
height: 0;
width: 0;
display: block;
opacity: 0;
height: 55px;
border-bottom: solid 2px #517180;
-webkit-border-top-right-radius: 5px;
-moz-border-top-right-radius: 5px;
border-top-right-radius: 5px;
-webkit-border-bottom-right-radius: 5px;
-moz-border-bottom-right-radius: 5px;
border-bottom-right-radius: 5px;
right: 0;
}
.hoverBtn-bottom:after {
position: absolute;
content: "";
height: 0;
width: 0;
display: block;
opacity: 0;
height: 55px;
border-bottom: solid 2px #517180;
-webkit-border-top-left-radius: 5px;
-moz-border-top-left-radius: 5px;
border-top-left-radius: 5px;
-webkit-border-bottom-left-radius: 5px;
-moz-border-bottom-left-radius: 5px;
border-bottom-left-radius: 5px;
left: 0;
}
h2.credits {
text-align: center;
font-family: "Source Sans Pro", sans-serif;
color: #888;
font-size: 16px;
}
/*
*/
@keyframes open {
0% {
width: 0;
height: 0;
}
50% {
width: 100px;
height: 0px;
}
100% {
width: 100px;
height: 55px;
}
}
@keyframes openB {
0% {
width: 0px;
}
100% {
width: 100px;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="border.css">
<title>Border</title>
</head>
<body>
<h1>Hover Border Animation</h1>
<div class="button">
<h2 class="hoverBtn">Hobbies</h2>
<br>
<div class="hoverBtn-bottom"></div>
<div class="box-img-container"></div>
</div>
</body>
</html>