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

求助:为何无法将navbar(导航栏)居中显示?

解决导航栏居中问题

先看你当前的导航栏效果:
导航栏效果截图

你的代码如下:

CSS 代码

@import url('https://fonts.googleapis.com/css2?family=Kumbh+Sans&display=swap');
:root {
  /* primary */
  --Orange: hsl(26, 100%, 55%);
  --Pale-orange: hsl(25, 100%, 94%);
  /* neutral */
  --Very-dark-blue: hsl(220, 13%, 13%);
  --Dark-grayish-blue: hsl(219, 9%, 45%);
  --Grayish-blue: hsl(220, 14%, 75%);
  --Light-grayish-blue: hsl(223, 64%, 98%);
  --White: hsl(0, 0%, 100%);
  --Black: hsl(0, 0%, 0%);
  /* (with 75% opacity for lightbox background) */
}

html {
  font-family: 'Kumbh Sans', sans-serif;
  font-size: 16px;
  box-sizing: border-box;
}

*,
*:after,
*:before {
  box-sizing: inherit;
}

div {
  border: 1px dotted grey;
}

body {
  margin: 0;
  padding: 0;
}

body {
  min-height: 100vh;
}


/* nav bar */

nav {
  display: flex;
  place-items: center;
}

.navbar {
  width: 80%;
  height: 7rem;
  background: rgb(201, 159, 159);
}


/* /////////////////////////// */

.attribution {
  font-size: 11px;
  text-align: center;
}

.attribution a {
  color: hsl(229, 22%, 53%);
}

HTML 代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="icon" type="image/png" sizes="32x32" href="./images/favicon-32x32.png">
  <title>Frontend Mentor | E-commerce product page</title>
  <link rel="stylesheet" href="main.css">
</head>

<body>

  <nav>
    <div class="navbar">
      <div class="navbar-head">sneakers</div>
      <div class="navbar-links"></div>
      <div class="navbar-user"></div>
    </div>
  </nav>

  <main>
    <div class="product">
      <div class="product-gallery"></div>

      <div class="product-info">
        <h5 class="product-origin">
          </h4>
          <h1 class="product-name">Fall Limited Sneakers</h1>
          <p class="product-description">These low-profile sneakers are your perfect casual wear companion. Featuring a durable rubber outer sole, they'll withstand everything the weather can offer.</p>

          <div class="product-pricetag">
            <div class="price-tag">
              <h5 class="price"></h5>
              <div class="discount"></div>
              <p class="price-original"></p>
            </div>
          </div>
      </div>
    </div>
  </main>

  <div class="attribution">
    Challenge by <a href="https://www.frontendmentor.io?ref=challenge" target="_blank">Frontend Mentor</a>. Coded by <a href="#">Your Name Here</a>.
  </div>
</body>

</html>

问题原因与解决方法

你之前的操作没生效,核心原因是:place-items: center 只控制flex容器内元素的**垂直方向(交叉轴)**对齐,水平方向(主轴)的居中需要额外设置;另外直接给.navbar加margin时,父元素nav默认没有占满视口宽度,导致margin无法自动分配空间。

方法1:给nav添加水平居中属性

修改nav的CSS,添加justify-content: center控制水平对齐,同时设置宽度和高度让垂直居中效果更明显:

nav {
  display: flex;
  place-items: center; /* 垂直居中 */
  justify-content: center; /* 水平居中 */
  min-height: 7rem; /* 和navbar高度匹配,确保垂直居中生效 */
  width: 100%; /* 让nav占满视口宽度 */
}

方法2:直接给.navbar设置自动margin

因为.navbar已经有明确的width:80%,直接给它加margin: 0 auto就能让浏览器自动分配左右空间,实现水平居中:

.navbar {
  width: 80%;
  height: 7rem;
  background: rgb(201, 159, 159);
  margin: 0 auto; /* 左右自动留白,实现水平居中 */
}

两种方法都能解决问题,根据你的整体布局需求选择即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 08:47:41