悬停时按钮边框无法正常工作
Button border not working well when hovered
我正在练习 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: block;
替换为 display: inline-block;
。这修复了对齐方式,然后您可以通过将边框为黄色(.button:hover .hoverBtn:before, .button:hover .hoverBtn:after
)的样式拆分为两种不同的样式来删除中间边框,然后删除之前的 border-right 并删除 border-left之后。
查看我在下面所做的 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>
我正在练习 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: block;
替换为 display: inline-block;
。这修复了对齐方式,然后您可以通过将边框为黄色(.button:hover .hoverBtn:before, .button:hover .hoverBtn:after
)的样式拆分为两种不同的样式来删除中间边框,然后删除之前的 border-right 并删除 border-left之后。
查看我在下面所做的 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>