【问题标题】:CSS hover transition, width from the centerCSS悬停过渡,从中心开始的宽度
【发布时间】:2020-06-09 14:46:06
【问题描述】:

我想知道如何使对象从中心变宽,而不是从左侧变宽。 这是我的 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: translate(-50%, 0);到 a:hover::before 在 css 中,它有点工作,但它使它有点不稳定(我不知道如何解释它)。有人可以帮忙吗?

【问题讨论】:

  • 您是在寻找之前的元素来增长并跨越整个链接,还是从左侧扩大是什么意思?否则,您可以将其 left 属性从 50% 更改为 0

标签: html css width transition


【解决方案1】:

你可以只使用背景:

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%;
}
&lt;a href='#'&gt;Hover&lt;/a&gt;

【讨论】:

  • 谢谢你,这行得通! center/0% 100% 到底是做什么的?有没有更详细的解释?
  • @HoneyPoop 这是表示position/size 的简写,所以我将渐变放在中心,尺寸宽度=0%,高度=100%,然后在悬停时我只更新尺寸
  • @HoneyPoop 查看更多详情:stackoverflow.com/a/54055521/8620333
【解决方案2】:

我想我解决了。我在元素中添加了::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,请单击 here

【讨论】:

    【解决方案3】:

    将这些样式添加到正文元素以使链接居中。然后从链接中删除position: absolute; top: 0 and left:0;(它们对于 flex 来说是多余的)并添加此样式以使其成为父级。

    body{
        display: flex;
        justify-content: center;
        align-items: center;
    } 
    a{ 
        position: relative
        transition: all .1s;
    }
    

    然后根据需要在 a:hover::before{} 上添加 transform: translateX(-50%);。 我发现这种格式没有晃动。

    【讨论】:

      猜你喜欢
      • 2016-10-24
      • 1970-01-01
      • 2017-04-09
      • 2018-01-22
      • 2021-05-17
      • 2019-01-03
      • 1970-01-01
      • 2014-08-19
      相关资源
      最近更新 更多