1

I having problems with moving the text of the a tag to the middle of the nav bar, if I use padding-top the hover for the a tag overflow so I hide it using overflow: hidden; on the nav bar, what is the best solution for this problem ?. note that my question is not about how to center an element who is a child of an other element like a p tag inside a div, and also I'm asking for the best approach in this current situation, which I guess a lot of beginner find it tricky .

html, body {
    margin: 0px;
}
header {
    display: grid;
    grid-template-columns: 1fr 1fr;
    color: white;
    background: black;
}

header .title{
    padding-left: 10px;
    font-size: 2rem;
}
header h1 {
    margin: 0;
}

nav {
    overflow: hidden;
}

nav ul {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    margin: 0;
    height: 100%;
}

nav ul li {
    list-style-type: none;
    text-align: center;
}

nav a {
    color: white;
    font-weight: bold;
    text-decoration: none;
    font-size: 1.4rem;
    display: block;
    height: 100%;
    padding-top: 20px;
}
nav a:hover {
    background-color: red;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="./css/style.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div class="wrapper">
        <header>
            <div class="title">
                <h1>Biblio</h1>
            </div>
            <nav>
                <ul>
                    <li><a href="#">home</a></li>
                    <li><a href="#">languages</a></li>
                    <li><a href="#">books</a></li>
                </ul>
            </nav>
        </header>
    </div>
</body>

</html>

1 Answers1

2
nav a {
  display: flex;
  align-items: center;
}

...will center them vertically, as long as the top and bottom padding values are equal:

html, body {
    margin: 0px;
}
header {
    display: grid;
    grid-template-columns: 1fr 1fr;
    color: white;
    background: black;
}

header .title{
    padding-left: 10px;
    font-size: 2rem;
}
header h1 {
    margin: 0;
}

nav {
    overflow: hidden;
}

nav ul {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    margin: 0;
    height: 100%;
}

nav ul li {
    list-style-type: none;
    text-align: center;
}

nav a {
    color: white;
    font-weight: bold;
    text-decoration: none;
    font-size: 1.4rem;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}
nav a:hover {
    background-color: red;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="./css/style.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div class="wrapper">
        <header>
            <div class="title">
                <h1>Biblio</h1>
            </div>
            <nav>
                <ul>
                    <li><a href="#">home</a></li>
                    <li><a href="#">languages</a></li>
                    <li><a href="#">books</a></li>
                </ul>
            </nav>
        </header>
    </div>
</body>

</html>

However, a bigger problem is the fact you made all buttons equal in width, using grid. The general accepted paradigm with top nav menus is: each element takes the required amount of width + some padding. As in:

nav ul {
  display: flex;
  justify-content: flex-end;
}

nav a {
  display: flex;
  align-items: center;
  padding: 0 .75em;
}

html, body {
    margin: 0px;
}
header {
    display: grid;
    grid-template-columns: 1fr 1fr;
    color: white;
    background: black;
}

header .title{
    padding-left: 10px;
    font-size: 2rem;
}
header h1 {
    margin: 0;
}

nav {
    overflow: hidden;
}

nav ul {
    display: flex;
    justify-content: flex-end;
    margin: 0;
    height: 100%;
}

nav ul li {
    list-style-type: none;
    text-align: center;
}

nav a {
    color: white;
    font-weight: bold;
    text-decoration: none;
    font-size: 1.4rem;
    height: 100%;
    display: flex;
    align-items: center;
    padding: 0 .75em;
}
nav a:hover {
    background-color: red;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <link rel="stylesheet" href="./css/style.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div class="wrapper">
        <header>
            <div class="title">
                <h1>Biblio</h1>
            </div>
            <nav>
                <ul>
                    <li><a href="#">home</a></li>
                    <li><a href="#">languages</a></li>
                    <li><a href="#">books</a></li>
                </ul>
            </nav>
        </header>
    </div>
</body>

</html>
tao
  • 82,996
  • 16
  • 114
  • 150