Home > database >  How to vertically center text inside <a> tag without using vertical padding
How to vertically center text inside <a> tag without using vertical padding

Time:03-09

I have a nav bar with height: 5vh and the rest of the page inside a container with height: 95vh. I use display: flex for my nav bar <ul> element and align-items: stretch so that the <li> elements have 100% height of the nav bar. Also I set the height of the <a> elements to 100% of the parent <li> elements.

However, the problem is that I want the text of the <a> elements to be vertically aligned to center (not top). I can't set a vertical padding for the <a> elements because this will affect the height of the element and will not make its height responsive.

Here is the full code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

        * {
            box-sizing: border-box;
        }

        body {
            margin: 0;
        }

        nav ul {
            background-color: aquamarine;
            list-style-type: none;
            margin: 0;
            padding: 0;
            height: 5vh;
            display: flex;
            align-items: stretch;
        }

        nav a {
            display: inline-block;
            padding: 0 10px;
            height: 100%;
            text-decoration: none;
            font-weight: bold;
            color: navy;
        }

        nav a:hover {
            background-color: rgb(95, 206, 169);
        }

        nav a.active {
            background-color: azure;
        }

        .content {
            height: 95vh;
            overflow: hidden;
            background-color: silver;
        }
    </style>
</head>

<body>

    <!-- Nav bar -->
    <nav>
        <ul>
            <li><a  href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Contact Us</a></li>
        </ul>
    </nav>

    <!-- Rest of the page -->
    <div ></div>

</body>

</html>

CodePudding user response:

You can set <a> to flex and use align-items: center to align it vertically

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      box-sizing: border-box;
    }
    
    body {
      margin: 0;
    }
    
    nav ul {
      background-color: aquamarine;
      list-style-type: none;
      margin: 0;
      padding: 0;
      height: 5vh;
      display: flex;
      align-items: stretch;
    }
    
    nav a {
      display: flex;
      align-items: center;
      padding: 0 10px;
      height: 100%;
      text-decoration: none;
      font-weight: bold;
      color: navy;
    }
    
    nav a:hover {
      background-color: rgb(95, 206, 169);
    }
    
    nav a.active {
      background-color: azure;
    }
    
    .content {
      height: 95vh;
      overflow: hidden;
      background-color: silver;
    }
  </style>
</head>

<body>

  <!-- Nav bar -->
  <nav>
    <ul>
      <li><a  href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact Us</a></li>
    </ul>
  </nav>

  <!-- Rest of the page -->
  <div ></div>

</body>

</html>

CodePudding user response:

Update

Fixed the navbar being too short in smaller windows:

nav ul {
  min-height: 5vh;
...

Didn't see a style for <li> so I added it and looks vertically centered on fullpage, but the navbar shrinks drastically when window scales down (this behavior is before any modification).

li {
  min-height: 100%;
  display: inline-block;
  align-self: space-around;
  vertical-align: bottom;
}

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      box-sizing: border-box;
    }
    
    body {
      margin: 0;
    }
    
    nav ul {
      background-color: aquamarine;
      list-style-type: none;
      margin: 0;
      padding: 0;
      min-height: 5vh;
      display: flex;
      align-items: stretch;
    }
    
    li {
      min-height: 100%;
      display: inline-block;
      align-self: space-around;
      vertical-align: bottom;
    }
    
    nav a {
      display: inline-block;
      padding: 0 10px;
      min-height: 100%;
      text-decoration: none;
      font-weight: bold;
      color: navy;
    }
    
    nav a:hover {
      background-color: rgb(95, 206, 169);
    }
    
    nav a.active {
      background-color: azure;
    }
    
    .content {
      height: 95vh;
      overflow: hidden;
      background-color: silver;
    }
  </style>
</head>

<body>

  <!-- Nav bar -->
  <nav>
    <ul>
      <li><a  href="#">Home</a></li>
      <li><a href="#">About</a></li>
      <li><a href="#">Contact Us</a></li>
    </ul>
  </nav>

  <!-- Rest of the page -->
  <div ></div>

</body>

</html>

  • Related