如何将 a 标签的文本移动到导航栏的中间
how I can move the text of the a tag to the middle of the nav bar
我在将 a
标签的文本移动到 nav
栏的中间时遇到问题,如果我使用 padding-top
标签溢出的悬停,所以我隐藏了它在 nav
栏上使用 overflow: hidden;
,这个问题的最佳解决方案是什么?。
请注意,我的问题不是关于如何将作为其他元素的子元素的元素居中,例如 div
中的 p
标记,而且我要求在当前情况下采用最佳方法,我猜很多初学者觉得这很棘手。
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>
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>
我在将 a
标签的文本移动到 nav
栏的中间时遇到问题,如果我使用 padding-top
标签溢出的悬停,所以我隐藏了它在 nav
栏上使用 overflow: hidden;
,这个问题的最佳解决方案是什么?。
请注意,我的问题不是关于如何将作为其他元素的子元素的元素居中,例如 div
中的 p
标记,而且我要求在当前情况下采用最佳方法,我猜很多初学者觉得这很棘手。
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>
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>