问题
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>
回答1:
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.
回答2:
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%;
}
来源:https://stackoverflow.com/questions/34342801/increasing-font-size-without-moving-other-text