增加字体大小而不移动其他文本

时间:2015-12-17 19:34:14

标签: html css navigation html-lists

我正在学习HTML和CSS,我已经搜索了这个问题,但我找不到适合我的解决方案。

因此,在我的导航栏中,当用户将鼠标悬停在一段文本上时,大小会增加,但也会移动其他文本。我希望大小增加但不移动其他文本。

我的网站

.nav {
    margin-top: 0px;
    height: 40px;
    width: 600px;
    background: white;
}
.nav ul {
    display: inline-block;
    margin: 0;
    padding: 0;
}
.nav ul li {
    list-style: none;
    display: inline;
}
.nav ul li a {
    text-decoration: none;
    float: left;
    display: block;
    padding: 10px 20px; 
    color: black;
    font-family: ProximaNova;
    font-size: 18px;
}
.nav ul li a:hover {
    color: #D46300;
    font-size: 22px;
}
 <div class="nav">
            <ul>
                <li><a href="news.html">News</a></li>
                <li><a href="servers.html">Servers</a></li>
                <li><a href="donate.html">Donate</a></li>
                <li><a href="bans.html">Bans</a></li>
                <li><a href="support.html">Support</a></li>
            </ul>
        </div>

2 个答案:

答案 0 :(得分:1)

这是你要找的吗?

.nav {
  margin-top: 0px;
  height: 40px;
  width: 600px;
  background: white;
}

.nav ul {
  display: block;
  margin: 0;
  padding: 0;
  position: relative;
}

.nav ul li {
  list-style-type: none;
  display: inline-block;
  width: 20%;
  padding: 0;
  margin: 0;
  float: left;
  text-align: center;
}

.nav ul li a {
  text-decoration: none;
  display: block;
  width: 100%;
  height: 100%;
  line-height: 40px;
  color: black;
  font-family: ProximaNova;
  font-size: 18px;
  transition: color .3s cubic-bezier(0.55, 0, 0.55, 0.2) , font-size .3s cubic-bezier(0.55, 0, 0.55, 0.2) ;
}

.nav ul li a:hover {
  color: #D46300;
  font-size: 22px;
}
<center>
  <div class="nav">
    <ul>
      <li><a href="news.html">News</a></li>
      <li><a href="servers.html">Servers</a></li>
      <li><a href="donate.html">Donate</a></li>
      <li><a href="bans.html">Bans</a></li>
      <li><a href="support.html">Support</a></li>
    </ul>
  </div>
</center>

事实上,不建议在文本动画中更改字体大小,因为大多数时候动画都是生涩的。以下是使用scale的示例:

.nav {
      margin-top: 0px;
      height: 40px;
      width: 600px;
      background: white;
    }
    .nav ul {
      display: block;
      margin: 0;
      padding: 0;
      position: relative;
    }
    .nav ul li {
      list-style-type: none;
      display: inline-block;
      width: 12%;
      padding: 0;
      margin: 0;
      text-align: center;
    }
    .nav ul li a {
      text-decoration: none;
      text-rendering: geometricPrecision;
      position: relative;
      display: block;
      width: 100%;
      height: 100%;
      line-height: 40px;
      color: black;
      font-family: ProximaNova;
      font-size: 18px;
      transition: transform .3s cubic-bezier(0.55, 0, 0.55, 0.2) , color .3s cubic-bezier(0.55, 0, 0.55, 0.2);
      transform-origin: 50% 30%;
    }
    .nav ul li a:hover {
      color: #D46300;
      transform: scale(1.3);
    }
<center>
  <div class="nav">
    <ul>
      <li><a href="news.html">News</a>
      </li>
      <li><a href="servers.html">Servers</a>
      </li>
      <li><a href="donate.html">Donate</a>
      </li>
      <li><a href="bans.html">Bans</a>
      </li>
      <li><a href="support.html">Support</a>
      </li>
    </ul>
  </div>
</center>

这是the fiddle。随意根据自己的喜好调整它。

答案 1 :(得分:0)

这是因为当尺寸变大时,元素的宽度会发生变化。一种解决方案是给li一个明确的宽度:

.nav ul li {
    list-style: none;
  float: left;
  width: 40px;
    padding: 10px 20px; 
}
.nav ul li a {
    text-decoration: none;
    display: block;
    color: black;
    font-family: ProximaNova;
    font-size: 18px;
  height: 100%;
  width: 100%;
}