悬停时按钮边框效果不佳
我正在练习 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>
先感谢您。
I'm practicing HTML and CSS, and want to create a button. I want a colored border appear around the button when hovered. However, I'm somehow stuck and can't get something right. The border doesn't appear quite around the button, as well as not showing up completely. I'd appreciate it if somebody could kindly help me figure out what I'm doing wrong and give a solution to fix it.
@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>
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最简单的解决方案是在
.hoverBtn:before
和上将
display: block;
替换为display: inline-block;
:之后。这修复了对齐方式,然后您可以通过将带有黄色边框的样式(.button:hover .hoverBtn:before、.button:hover .hoverBtn:after
)拆分为两种不同的样式来删除中间边框,然后删除之前的右边框并删除之后的左边框。请参阅下面我所做的 CSS 更改:
The simplest solution would be to replace
display: block;
withdisplay: inline-block;
on both.hoverBtn:before
and:after
. This fixes the alignment, then you can remove the middle borders by splitting the styles with the border yellow (.button:hover .hoverBtn:before, .button:hover .hoverBtn:after
) into two different styles, then remove the border-right on before and remove the border-left on after.See the CSS changes I made below: