我想知道如何使物体从中心而不是左边变宽。以下是我的HTML:
<html>
<body>
<a href='#'>Hover</a>
</body>
</html>这是我的CSS:
body{
margin:0;
padding:0;
background-color:#262626;
}
a{
position:absolute;
top:50%;
left:50%;
transform:translate(-50%,-50%);
padding:10px 25px;
border:2px solid white;
text-decoration:none;
color:white;
font-family:verdana;
font-size:27px;
text-transform:uppercase;
letter-spacing:4px;
transform:1s;
}
a::before{
content:'';
position:absolute;
height:100%;
width:0%;
top:0;
left:50%;
z-index:-1;
background-image:linear-gradient(45deg, #eea949,#ff3984);
transition:.5s;
}
a:hover::before{
width:100%;
}它不是在悬停时从中间变宽,而是从左边变宽。我尝试过添加transform: transform(-50%,0);在css中之前,它有点工作,但它使它有点不稳定(我不知道该如何解释)。有人能帮忙吗?
发布于 2020-06-09 15:04:32
你只需要背景就可以做到:
body {
margin: 0;
padding: 0;
background-color: #262626;
}
a {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 10px 25px;
border: 2px solid white;
text-decoration: none;
color: white;
font-family: verdana;
font-size: 27px;
text-transform: uppercase;
letter-spacing: 4px;
transition: 0.5s;
background: linear-gradient(45deg, #eea949, #ff3984) center/0% 100% no-repeat;
}
a:hover {
background-size: 100% 100%;
}<a href='#'>Hover</a>
发布于 2020-06-09 15:01:59
我想我解决了。我向元素中添加了一个::after,旋转了梯度,使::after向左转了50%,而::before向右转了50%。如下所示:
body {
margin: 0;
padding: 0;
background-color: #262626;
}
a {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
padding: 10px 25px;
border: 2px solid white;
text-decoration: none;
color: white;
font-family: verdana;
font-size: 27px;
text-transform: uppercase;
letter-spacing: 4px;
transform: 1s;
}
a::before {
content: '';
position: absolute;
height: 100%;
width: 0%;
top: 0;
left: 50%;
z-index: -1;
border: none;
outline: none;
background-image: linear-gradient(45deg, #eea949, #ff3984);
transition: .5s;
}
a::after {
content: '';
position: absolute;
height: 100%;
width: 0%;
top: 0;
right: 50%;
z-index: -1;
background-image: linear-gradient(135deg, #ff3984, #eea949);
border: none;
outline: none;
transition: .5s;
}
a:hover::before {
width: 50%;
}
a:hover::after {
width: 50%;
}<html>
<body>
<a href='#'>Hover</a>
</body>
</html>
我希望这能帮到你。如果要查看JSFiddle,请单击“这里”。
发布于 2020-06-09 15:16:42
将这些样式添加到主体元素以使链接居中。然后从链接中删除position: absolute; top: 0 and left:0; (它们与flex一起是多余的),并添加这种样式以使其成为父级。
body{
display: flex;
justify-content: center;
align-items: center;
}
a{
position: relative
transition: all .1s;
}然后按您的要求在a:transform: translateX(-50%);::前面添加{}。我发现这种格式没有摆动。
https://stackoverflow.com/questions/62285306
复制相似问题