如何在 html 中将 header 文本置于导航菜单中间
How do I center header text in the middle of the navigation menu in html
我想知道如何将我写在我创建的导航菜单中心的 header 文本居中,该文本已经居中但它在导航菜单的顶部居中,而不是在中间的,这就是我需要的。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<style>
body {margin:0;}
.Header {
z-index: 100;
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #000000;
height: 70px;
}
@media screen and (max-width:680px) {
.Header.responsive {position: relative;}
.Header.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
}
@media (max-width: 960px){
.Header .headerLogo{
display: inline-block;
width: 86px;
height: 15px;
margin-top: 17px;
margin-left: 6px;
}
}
</style>
</head>
<body>
<div class="Header" id="myHeader">
<a class = "headerLogo">
<header><center><i><font size = "6" face = "verdana" color = "white">Lunation Boards</font></i></center></header>
</a>
</div>
</body>
</html>
这应该可以解决问题。
要清理您的 HTML,请像这样设置它并删除 <center>
和 <font>
等标签:
<div class="Header" id="myHeader">
<a class = "headerLogo">
<h1>Lunation Boards</h1>
</a>
</div>
并且您可以使用 display: flex
将内容置于 header 的中心:
.Header {
z-index: 100;
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #000000;
height: 70px;
display: flex;
justify-content: center;
}
a {
width: 100%;
}
h1 {
margin: 0;
color: #fff;
}
@media (max-width: 960px){
.Header .headerLogo {
display: inline-block;
width: 86px;
height: 15px;
margin-left: 6px;
}
}
这里是完整的 fiddle 变化:
尝试(此处为您的代码)。将 : (此处为您的代码)替换为您在中心想要的任何内容。
你有三个选项,其中前两个选项更可靠。
第一个选项使用 flex-box 使项目水平和垂直居中。
div {
width: 100%;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
background: blue;
}
text {
background: orange;
}
<div>
<text>Centered horizontally and vertically</text>
</div>
第二个选项不是使用 flex-box,而是使用父元素的相对位置,子元素的绝对位置,子元素也使用 transform: translate(X, Y)
。
div {
width: 100%;
height: 200px;
position: relative;
background: blue;
}
text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: orange;
}
<div>
<text>Centered horizontally and vertically</text>
</div>
第三个选项,为了使元素垂直居中,使用 line-height
等于父元素的 height
。
div {
width: 100%;
height: 200px;
position: relative;
background: blue;
}
text {
position: absolute;
left: 50%;
transform: translateX(-50%);
line-height: 200px;
background: orange;
}
<div>
<text>Centered horizontally and vertically</text>
</div>
- 我认为您不需要在页眉中放置
<center>
标记,将其与 css 对齐。
- 而且当您使用自己的 class 时,您应该删除
<header>
标签。
- 您在页眉中使用了 100% 的宽度,因此您不应使用 left:0;
- 您需要在 headerlogo 中使用 position: relative class with margin: 0 auto;
到目前为止,我已经使用了 html 和 css,但我认为它会有所帮助
示例代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<style>
body {margin:0;}
.Header {
z-index: 100;
position: fixed;
top: 0;
width: 100%;
background-color: #000000;
height: 70px;
}
@media screen and (max-width:680px) {
.Header.responsive {position: relative;}
.Header.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
}
@media (max-width: 960px){
.Header .headerLogo{
position: relative;
display: inline-block;
width: 86px;
height: 15px;
margin: 0 auto;
}
}
</style>
</head>
<body>
<div class="Header" id="myHeader">
<a class = "headerLogo">
<i><font size = "6" face = "verdana" color = "white">Lunation Boards</font></i>
</a>
</div>
</body>
</html>
我想知道如何将我写在我创建的导航菜单中心的 header 文本居中,该文本已经居中但它在导航菜单的顶部居中,而不是在中间的,这就是我需要的。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<style>
body {margin:0;}
.Header {
z-index: 100;
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #000000;
height: 70px;
}
@media screen and (max-width:680px) {
.Header.responsive {position: relative;}
.Header.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
}
@media (max-width: 960px){
.Header .headerLogo{
display: inline-block;
width: 86px;
height: 15px;
margin-top: 17px;
margin-left: 6px;
}
}
</style>
</head>
<body>
<div class="Header" id="myHeader">
<a class = "headerLogo">
<header><center><i><font size = "6" face = "verdana" color = "white">Lunation Boards</font></i></center></header>
</a>
</div>
</body>
</html>
这应该可以解决问题。
要清理您的 HTML,请像这样设置它并删除 <center>
和 <font>
等标签:
<div class="Header" id="myHeader">
<a class = "headerLogo">
<h1>Lunation Boards</h1>
</a>
</div>
并且您可以使用 display: flex
将内容置于 header 的中心:
.Header {
z-index: 100;
position: fixed;
top: 0;
left: 0;
width: 100%;
background-color: #000000;
height: 70px;
display: flex;
justify-content: center;
}
a {
width: 100%;
}
h1 {
margin: 0;
color: #fff;
}
@media (max-width: 960px){
.Header .headerLogo {
display: inline-block;
width: 86px;
height: 15px;
margin-left: 6px;
}
}
这里是完整的 fiddle 变化:
尝试(此处为您的代码)。将 : (此处为您的代码)替换为您在中心想要的任何内容。
你有三个选项,其中前两个选项更可靠。
第一个选项使用 flex-box 使项目水平和垂直居中。
div {
width: 100%;
height: 200px;
display: flex;
justify-content: center;
align-items: center;
background: blue;
}
text {
background: orange;
}
<div>
<text>Centered horizontally and vertically</text>
</div>
第二个选项不是使用 flex-box,而是使用父元素的相对位置,子元素的绝对位置,子元素也使用 transform: translate(X, Y)
。
div {
width: 100%;
height: 200px;
position: relative;
background: blue;
}
text {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: orange;
}
<div>
<text>Centered horizontally and vertically</text>
</div>
第三个选项,为了使元素垂直居中,使用 line-height
等于父元素的 height
。
div {
width: 100%;
height: 200px;
position: relative;
background: blue;
}
text {
position: absolute;
left: 50%;
transform: translateX(-50%);
line-height: 200px;
background: orange;
}
<div>
<text>Centered horizontally and vertically</text>
</div>
- 我认为您不需要在页眉中放置
<center>
标记,将其与 css 对齐。 - 而且当您使用自己的 class 时,您应该删除
<header>
标签。 - 您在页眉中使用了 100% 的宽度,因此您不应使用 left:0;
- 您需要在 headerlogo 中使用 position: relative class with margin: 0 auto;
到目前为止,我已经使用了 html 和 css,但我认为它会有所帮助
示例代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<style>
body {margin:0;}
.Header {
z-index: 100;
position: fixed;
top: 0;
width: 100%;
background-color: #000000;
height: 70px;
}
@media screen and (max-width:680px) {
.Header.responsive {position: relative;}
.Header.responsive li.icon {
position: absolute;
right: 0;
top: 0;
}
}
@media (max-width: 960px){
.Header .headerLogo{
position: relative;
display: inline-block;
width: 86px;
height: 15px;
margin: 0 auto;
}
}
</style>
</head>
<body>
<div class="Header" id="myHeader">
<a class = "headerLogo">
<i><font size = "6" face = "verdana" color = "white">Lunation Boards</font></i>
</a>
</div>
</body>
</html>