【问题标题】:Hover button and fill from left to right with round shape悬停按钮并从左到右填充圆形
【发布时间】:2023-01-07 01:35:52
【问题描述】:
我有一个带有悬停效果的按钮,它从左到右填充背景:
当前的 HTML
<a href="#" class="button">Mehr erfahren</a>
当前 CSS
body {
background-color: #f6f6f6;
}
a {
text-decoration: none;
color: black;
}
a:hover {
text-decoration: none;
}
.button {
font-size: 20px;
color: black;
background-color: white;
padding: 20px;
border-radius: 50px;
display: inline-block;
background-image: radial-gradient(circle at left, black 50%, black 50%);
background-size: 0 100%;
background-repeat: no-repeat;
transition: 0.4s;
}
.button:hover {
color: white;
background-size: 100% 100% !important;
}
.button::before {
content: "";
display: inline-block;
width: 15px;
height: 15px;
-moz-border-radius: 7.5px;
-webkit-border-radius: 7.5px;
border-radius: 7.5px;
background-color: white;
border: 1px solid black;
margin-right: 15px;
}
Find it also here on codepen
目标是填充颜色的右侧有圆角,如下所示:
有任何想法吗?提前致谢
【问题讨论】:
标签:
html
css
button
hover
【解决方案1】:
正如other SO question中提到的,没有办法直接将border-radius添加到background-image。如果您对使用伪背景元素并在文本周围添加 <span> 以进行 z-index 调整来获得所需结果的另一个选项持开放态度,那么您可以参考我下面的示例。
body {
background-color: #f6f6f6;
}
a {
text-decoration: none;
color: black;
}
a:hover {
text-decoration: none;
}
.button {
font-size: 20px;
color: black;
background-color: white;
padding: 20px;
border-radius: 50px;
display: inline-block;
transition: 0.4s;
}
.button:hover {
color: white;
}
/* New CSS */
.button {
position: relative;
overflow: hidden;
}
.button:hover::after {
transform: translateX(0);
}
.button::after {
content: '';
position: absolute;
top: 0;
left: 0;
background: #000;
width: 100%;
height: 100%;
border-radius: 50px;
transform: translateX(-100%);
z-index: 0;
transition: 0.4s;
}
.button span {
position: relative;
z-index: 1;
}
.button span::before {
content: "";
display: inline-block;
width: 15px;
height: 15px;
-moz-border-radius: 7.5px;
-webkit-border-radius: 7.5px;
border-radius: 7.5px;
background-color: white;
border: 1px solid black;
margin-right: 15px;
}
<a href="#" class="button"><span>Mehr erfahren</span></a>