我在将a
标签的文本移到nav
栏的中间时遇到问题,如果我使用padding-top
,则鼠标悬停会导致标签溢出,因此我使用overflow: hidden;
栏上的nav
,什么是解决此问题的最佳方法?
请注意,我的问题不是关于如何在p
内将像div
标签之类的其他元素的子元素居中,而且我正在寻求这种当前情况下的最佳方法,我想很多新手都会觉得棘手。
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>
答案 0 :(得分:2)
nav a {
display: flex;
align-items: center;
}
...只要它们的顶部和底部填充值相等,就会将它们垂直居中:
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>
但是,更大的问题是您使用网格使所有按钮的宽度相等。顶部导航菜单普遍接受的范例是:每个元素采用所需的宽度+一些填充。如:
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>