You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

HTML/CSS响应式导航栏:宽屏模式下文字无法居中求助

问题修复:大屏下section文字偏左的问题

问题原因

当屏幕宽度大于952px时,导航栏内的<ul>使用了float: right,该浮动元素未被清除,导致后续<section>的布局被干扰,使得内部<h1>视觉上偏左。同时你的viewport meta标签存在拼写错误,会影响响应式布局的准确性。

修复步骤

  1. 修正viewport标签拼写
    将HTML中的错误标签:

    <meta name="viewport" content="width=device-widht,initial-sclae=1.0">
    

    改为:

    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    
  2. 清除浮动并优化section布局
    修改section的CSS样式,添加清除浮动的属性,同时可以通过flex布局让文字在section内水平+垂直居中:

    section {
      background: url(bg1.jpeg) no-repeat;
      background-size: cover;
      height: calc(100vh - 80px);
      opacity: 20%;
      clear: both; /* 清除导航栏浮动带来的布局干扰 */
      display: flex;
      justify-content: center; /* 水平居中内容 */
      align-items: center; /* 垂直居中内容 */
    }
    
    section h1 {
      text-align: center; /* 确保文字自身居中(flex布局下其实可省略,但保留更稳妥) */
    }
    
  3. 移除内联样式(可选但推荐)
    把HTML中<h1>的内联样式style="text-align:center;"删除,统一通过CSS控制样式,便于维护。

修复后完整代码

CSS

* {
  padding: 0;
  margin: 0;
  text-decoration: none;
  list-style: none;
  box-sizing: border-box;
}

body {
  font-family: montserrat;
}

nav {
  background: #0082e6;
  height: 80px;
  width: 100%;
}

label.logo {
  color: white;
  font-size: 35px;
  line-height: 80px;
  padding: 100px;
  font-weight: bold;
}

nav ul {
  float: right;
  margin-right: 20px;
}

nav ul li {
  display: inline-block;
  line-height: 80px;
  margin: 0 5px;
}

nav ul li a {
  color: white;
  font-size: 17px;
  padding: 7px 13px;
  border-radius: 3px;
  text-transform: uppercase;
}

a.active,
a:hover {
  background: #1b9bff;
  transition: .5s;
}

.checkbtn {
  font-size: 30px;
  color: white;
  float: right;
  line-height: 80px;
  margin-right: 40px;
  cursor: pointer;
  display: none;
}

#check {
  display: none;
}

@media (max-width: 952px) {
  label.logo {
    font-size: 30px;
    padding-left: 50px;
  }
  nav ul li a {
    font-size: 16px;
  }
}

@media (max-width: 858px) {
  .checkbtn {
    display: block;
  }
  ul {
    position: fixed;
    width: 100%;
    height: 100vh;
    background: #2c3e50;
    top: 80px;
    left: -100%;
    text-align: center;
    transition: all .5s;
  }
  nav ul li {
    display: block;
  }
  nav ul li a {
    font-size: 20px;
  }
  a:hover,
  a.active {
    background: none;
    color: #0082e6;
  }
  #check:checked~ul {
    left: 0;
  }
}

section {
  background: url(bg1.jpeg) no-repeat;
  background-size: cover;
  height: calc(100vh - 80px);
  opacity: 20%;
  clear: both;
  display: flex;
  justify-content: center;
  align-items: center;
}

section h1 {
  text-align: center;
}

HTML

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="https://kit.fontawesome.com/.js" crossorigin="anonymous"></script>
<nav>
  <input type="checkbox" id="check">
  <label for="check" class="checkbtn">
    <i class="fas fa-bars"></i>
  </label>
  <label class="logo">Air</label>
  <ul>
    <li><a href='home.html'>Home</a></li>
    <li><a href='About.html'>About</a></li>
    <li><a href='Flights.html'>Flights</a></li>
    <li><a href='Contact.html'>Contact</a></li>
  </ul>
</nav>
<section>
  <h1>test</h1>
</section>

内容的提问来源于stack exchange,提问作者Foros

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 12:20:54