Increasing font size without moving other text
Asked Answered
S

2

7

I'm learning HTML and CSS, I have searched this question but I didn't find a solution which worked for me.

So in my navigation bar, when the user hovers their mouse over a piece of text the size increases but also moves the other pieces of text. I want the size to increase but not move the other pieces of text.

MY WEBSITE

.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>
Shumway answered 17/12, 2015 at 19:34 Comment(2)
move this to a fiddle and it'll be easier to see your problemPervade
Try this: jsfiddle.net/3rm2sut4/embedded/resultShumway
K
9

Is this what you're looking for?

.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>

As a matter of fact, changing font size is NOT recommended for text animations, as most of the times animation is jerky. Here's an example using 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>

Here's the fiddle. Feel free to tweak it to your liking.

Kissiah answered 17/12, 2015 at 20:15 Comment(3)
Is it possible to decrease the size between each li or word?Shumway
Of course. You either decrease ul (because each li is 1/5 of ul width) or you decrease the 20%. See second snippet.Kissiah
This is so cool, I learned a new thing thanks @tao, here is a very simplified version of your gist jsfiddle.net/14gq5ceuOrian
P
0

This happens because the width of the elements change when the size gets bigger. One solution is to give li an explicit width:

.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%;
}
Pervade answered 17/12, 2015 at 20:16 Comment(1)
np, if this is the answer you're looking for would appreciate getting it accepted as the answer. tryna get mah points up!Pervade

© 2022 - 2024 — McMap. All rights reserved.