2

I'm about to program a navigation bar. The problem is that the text in the navigation bar is squeezed. I think that's because of relative and absolute positioning.

This is the code that causes this error:

div.menu {
       float: right;
       position: relative;
    }

    div.menu a {
       text-decoration: none;
       position: absolute;
       top: 50%;
       transform: translateY (-50%);
    }

But I need this code to center the a elements vertically. How do I make it that the a elements are vertically centered and the text is not squeezed together?

/*****************************************************************************************************************************************/
/************************************************************* styles.css ***************************************************************/
/*****************************************************************************************************************************************/


/*****************************************************************************************************************************************/
/* *** START:Colors *** */
/*****************************************************************************************************************************************/

/*
Blau: #1f4ebc;
Dunkel Grau: #3d3f45;
Hell Grau: #e5e5e5;
Rot: #a62c21
*/

/*****************************************************************************************************************************************/
/* *** END:Colors *** */
/*****************************************************************************************************************************************/


/*****************************************************************************************************************************************/
/* *** START:General Settings *** */
/*****************************************************************************************************************************************/

html{
  font-size: 62.5%;
}

body {
  margin: 0;
  font-family: 'Raleway', 'Lato', 'Helvetica Neue', 'Arial', sans-serif;
  font-size: 1.6rem;
}

* {
  box-sizing: border-box;
}

/*****************************************************************************************************************************************/
/* *** END:General Settings  *** */
/*****************************************************************************************************************************************/


/*****************************************************************************************************************************************/
/* *** START:Navigation *** */
/*****************************************************************************************************************************************/

.header-nav {
  width: 100%;
  height: 5rem;
  background-color: #3d3f45;
  font-weight: 400;
}

/* *** START: Logo *** */
.header-nav div {
  height:100%;
  display: inline-block;
  position: relative;
}

.header-nav div img {
  height: 3rem;
  width: auto;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  left:2rem;
}
/* *** END: Logo *** */

/* *** START: Menu *** */
div.menu {
  float:right;
  position:relative;
}

div.menu ul {
  height: 100%;
  margin: 0;
}

div.menu ul li {
  display: inline-block;
  height:100%;
  padding: 0 1rem;
}

div.menu a {
  text-decoration: none;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

div.menu a:link {
  color:#fff;
}

div.menu a:visited {
  color:#fff;
}

div.menu a:hover,
div.menu a:active {
  color:#e5e5e5;
}

.active {
  background-color: #a62c21;
}
/* *** END: Menu *** */


/*****************************************************************************************************************************************/
/* *** END:Navigation *** */
/*****************************************************************************************************************************************/
<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="utf-8">
    <title>Home</title>
    <link rel="stylesheet" type="text/css" href="src/css/styles.css">
  </head>
  <body>

    <!-- **************************************************************************************************************************************************** -->
    <!-- ***   START: Navigation *** -->
    <!-- **************************************************************************************************************************************************** -->

    <nav class="header-nav">

      <!-- *** START: Logo *** -->
      <div class="logo">
        <a href="index.html">
          <img src="src/img/logo.png" alt="logo">
        </a>
      </div>
      <!-- *** END: Logo *** -->

      <!-- *** START: Menu *** -->
      <div class="menu">
        <ul>
          <li class="active"><a href="index.html">Home</a></li>
          <li><a href="#">Lorem</a></li>
          <li><a href="#">Ipsum</a></li>
          <li><a href="#">Dolor</a></li>
        </ul>
      </div>
      <!-- *** END: Menu *** -->

    </nav>

    <!-- **************************************************************************************************************************************************** -->
    <!-- *** END: Navigation *** -->
    <!-- **************************************************************************************************************************************************** -->






  </body>
</html>
  • Try to increase padding applied to menu linklist
  • div.menu ul li { display: inline-block; height: 100%; padding: 0 7rem; } So that you can see the menu items
  • – user6297534 Apr 11 '18 at 15:46
  • I would like to use a clean solution. –  Apr 11 '18 at 15:51
  • 1
    Possible duplicate of [Vertical Align Unordered List Nav Links?](https://stackoverflow.com/questions/9726209/vertical-align-unordered-list-nav-links) – Adam Apr 11 '18 at 15:54