如何在 html 中的导航菜单中间居中标题文本

Noa*_*ltz 3 html css center

我想知道如何将我在创建的导航菜单中心编写的标题文本居中,文本已经居中,但它居中在导航菜单的顶部,而不是中间,这正是我所需要的。

<!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>
Run Code Online (Sandbox Code Playgroud)

小智 8

您有三个选项,其中前两个选项更可靠

第一个选项使用 flex-box 将项目水平和垂直居中。

div {
  width: 100%;
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: blue;
}

text {
  background: orange;
}
Run Code Online (Sandbox Code Playgroud)
<div>
   <text>Centered horizontally and vertically</text>
</div>
Run Code Online (Sandbox Code Playgroud)

第二个选项不是使用 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;
}
Run Code Online (Sandbox Code Playgroud)
<div>
   <text>Centered horizontally and vertically</text>
</div>
Run Code Online (Sandbox Code Playgroud)

第三个选项,为了使元素垂直居中,使用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;
}
Run Code Online (Sandbox Code Playgroud)
<div>
   <text>Centered horizontally and vertically</text>
</div>
Run Code Online (Sandbox Code Playgroud)