Itamar S
Itamar S

Reputation: 1565

CSS transitions affecting the entire navigation menu on page load

I'm currently attempting to create a navigation menu for a website of mine, where I had the thought in mind to make each item on the menu pop out a bit once hovered upon so the user would have something to focus on when needed.

Currently, once I load the page itself, this transition takes place on all of the navigation menu items, making it seem a bit unsettling and kind of 'ruining the mood' of the website.

Relevant CSS Code


html,
body {
  margin: 0;
  padding: 0;
  background-image: linear-gradient(to right, #2cac8c, #84a98c);
}

nav {
  position: fixed;
  top: 0;
  width: 100%;
}
nav, nav li {
  display: inline-flex;
  background-color: rgb(48, 48, 48); 
  flex-direction: row;
  font-family: 'Open Sans', sans-serif;
  font-weight: 800;
  font-size: 24px;
  text-align: right;
  color: white;
}

nav li {
  margin-left: 20px;
  margin-right: 20px;
  transition: font-size 0.5s;
}

nav li:hover {
  font-size: 30px;
}

nav ul a {
  text-decoration: none;
  color: white;
}

#name {
  display: inline-flex;
  font-size: 50px;
  color: white;
  font-family: Arial, Helvetica, sans-serif;
  font-weight: bold;
  margin-left: 20px;
  margin-right: 1100px;
  text-align: center
}
<nav>
    <ul>
        <li id="name">Bqre</li>
        <li><a href="#about">About Me</a></li>
        <li>Web Design</li>
        <li>Resume</li>
        <li>Contact</li>
    </ul>
</nav>

Note: When I attempt to move the transition under the nav li:hover element, this does fix the page loading issue, but it also makes it so once unhovered from the item it goes back immediately to its regular font-size without taking the 0.5 seconds as intended.

Is there a way I could fix this?
Reference video for how it currently loads: https://www.youtube.com/watch?v=pEEyP16DXoU

Upvotes: 0

Views: 218

Answers (1)

mqbaka mqbaka
mqbaka mqbaka

Reputation: 282

If you change font-size the container will automatically adapt it's size and that's why your entire nav is changing. Why not use transform : scale() instead ? https://codepen.io/mqbaka/pen/vYgeXGJ

html,
body {
  margin: 0;
  padding: 0;
  background-image: linear-gradient(to right, #2cac8c, #84a98c);
}

nav {
  position: fixed;
  top: 0;
  width: 100%;
}
nav, nav li {
  display: inline-flex;
  background-color: rgb(48, 48, 48); 
  flex-direction: row;
  font-family: 'Open Sans', sans-serif;
  font-weight: 800;
  font-size: 24px;
  text-align: right;
  color: white;
}

nav li {
  margin-left: 20px;
  margin-right: 20px;
  transition: transform 0.5s;
}

nav li:hover {
  transform: scale(1.2);
}

nav ul a {
  text-decoration: none;
  color: white;
}

#name {
  display: inline-flex;
  font-size: 50px;
  color: white;
  font-family: Arial, Helvetica, sans-serif;
  font-weight: bold;
  margin-left: 20px;
  margin-right: 1100px;
  text-align: center
}
<nav style='width:100%'>
    <ul>
        <li id="name">Bqre</li>
        <li><a href="#about">About Me</a></li>
        <li>Web Design</li>
        <li>Resume</li>
        <li>Contact</li>
    </ul>
</nav

Let me know if I misenderstood your problem.

Upvotes: 2

Related Questions