当文本在悬停时增长时,如何使元素宽度保持不变?

时间:2018-03-25 00:26:06

标签: html css

标题说明了这一切,我无法弄清楚如何让元素保持相同的宽度,同时它里面的文字增长,如果有人知道怎么做,那就太好了! p.s,我试图使用box-sizing,并定位:relative,以及所有这些。

.menu{
  width: 100%;
  position: fixed;
  background-color: #333;
}
.menu nav ul{
  list-style: none;
  list-style-type: none;
  padding: 0;
  margin: 0;
}
.menu nav ul li{
  display: inline-block;
}
.menu nav ul li button{
  margin-left: 20px;
  background-color: #d63031;
  font-size: 22px;
  color: #fffff7;
  border-style: none;
  border-bottom: 4px solid rgba(0,0,0,.3) !important;
  padding: 15px 22px;
  text-transform: uppercase;
  font-family: 'Open Sans', sans-serif;
  outline: none;
  border-radius: 6px;  
}
.menu nav ul li a{
  display: block;
  padding: 30px 20px;
  font-size: 22px;
  color: #fffff7;
  line-height: 12px;
  text-decoration: none;
  text-transform: uppercase;
}
.menu nav ul li a:hover{
  transition: all .3s ease;
  -o-transition: all .3s ease;
  -ms-transition: all .3s ease;
  -moz-transition: all .3s ease;
  -webkit-transition: all .3s ease;
  background-color: rgb(0,0,0,.8);
  font-size: 26px;
}

1 个答案:

答案 0 :(得分:0)

只需分享我的代码,使用transform缩放font-size上的文字而不是a:hover,希望它符合您的要求或为您提供灵感:



a {
    display: block;
    padding: 30px 20px;
    font-size: 22px;
}

a:hover {
    -webkit-transform: scale(1.18); /* 22px * 1.18 = 26px */
    -moz-transform: scale(1.18); /* 22px * 1.18 = 26px */
    transform: scale(1.18); /* 22px * 1.18 = 26px */
}

ul {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
}

li {
    border: 1px solid #ccc;
}

li a {
    padding: 30px 20px;
    transition: all .3s;
}

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <ul>
        <li><a href="#">link</a></li>
        <li><a href="#">another link</a></li>
    </ul>
</body>

</html>
&#13;
&#13;
&#13;