Search code examples
htmlcssfont-size

changing font size adds extra space at the top


I have a navbar where I am using an anchor tag as a button. When the font size is default, everything is fine but when I change it a bit it adds some space at the top as shown.

NOTE this space is not visible on pc but instead when the website is viewed on a phone or other devices with less width. If the same website is opened on landscape mode of the phone, the extra space on top disappears.

The problem:

The demo is below:

* {
  margin: 0;
  padding: 0;
}

body {
  background-color: cadetblue;
}

.nav {
  width: 100%;
  height: 5vw;
  display: inline-flex;
  align-items: center;
}

#heading-1 {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  position: absolute;
  color: white;
  background-color: rgb(53, 53, 53);
  height: 5vw;
  font-size: 3vw;
}

#a-1 {
  color: white;
  display: inline-flex;
  align-items: center;
  position: relative;
  text-decoration: none;
  background-color: rgb(40, 89, 194);
  margin: 0 0 0 1vw;
  border-radius: 3px;
  border: 0.15vw solid rgb(189, 189, 189);
  padding: 0px 1vw;
  font-size: 1vw;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  height: 2vw;
}
<div class="nav">
  <h1 class='nav-elements' id="heading-1">Code Craze Course</h1>
  <a class="nav-elements" id="a-1" href="google.com">Enroll Now</a>
</div>

I tried changing the line height but it didn't help. But changing the font size to default fixes the problem but I need the font size to be a bit bigger so can't keep t as default.

REQUEST-Please don't mark this as duplicate as I have already tried the solutions given in this post (Font-size increases the top margin) but it didn't help.


Solution

  • Just add top: 0; position: absolute; to .nav and it will be just fine, like so:

    * {
      margin: 0;
      padding: 0;
    }
    
    body {
      background-color: cadetblue;
    }
    
    .nav {
      width: 100%;
      height: 5vw;
      display: inline-flex;
      align-items: center;
      top: 0;
      position: absolute;
    }
    
    #heading-1 {
      display: inline-flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      position: absolute;
      color: white;
      background-color: rgb(53, 53, 53);
      height: 5vw;
      font-size: 3vw;
    }
    
    #a-1 {
      color: white;
      display: inline-flex;
      align-items: center;
      position: relative;
      text-decoration: none;
      background-color: rgb(40, 89, 194);
      margin: 0 0 0 1vw;
      border-radius: 3px;
      border: 0.15vw solid rgb(189, 189, 189);
      padding: 0px 1vw;
      font-size: 1vw;
      font-family: Verdana, Geneva, Tahoma, sans-serif;
      height: 2vw;
    }
    <div class="nav">
      <h1 class='nav-elements' id="heading-1">Code Craze Course</h1>
      <a class="nav-elements" id="a-1" href="google.com">Enroll Now</a>
    </div>

    TIP: the increased font-size overflows the text outside its container, so you should take the height out of it.