如何使用 flexbox 使列表项居中?

How to center list items with flexbox?

我正在尝试创建一个导航栏,标题在左侧,列表项居中。我使用 flexbox 来水平排列链接。然后我将 margin: auto 添加到 ul ,这将它们向左移动但没有将它们对称地居中。我猜 ul 已经没有 space 可以移动了。我检查了其他类似的帖子,但不太清楚问题出在哪里。谢谢你的时间。

HTML

* {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
}

body {
    font-family: 'Open Sans', sans-serif;
    background: #fff;
    color: #333;
    line-height: 1.6;
    background: url('/img/showcase.jpg.jpg');
}


#navbar  {
    display: flex;
    justify-content: center;
    align-items: center;
    flex: 1;
    top: 0;
    width: 100%;
    height: 60px;
    background-color: rgba(10, 10, 10, 0.5);
}

#navbar ul {
    display: flex;
    justify-content: space-around;
    text-align: center;
    margin: 0 auto;
    padding: 0;
}

#navbar li {
    list-style: none;
    padding: 20px;
}
<nav id="navbar">
        <div class="logo">
            <h1>Welcome</h1>
        </div>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
            <li><a href="#">Home</a></li>
        </ul>
    </nav>

您可以从 #navbar ul

中删除 margin: 0 auto;

父容器 (#navbar) 上的 flexbox 已经使 ul 居中。添加 flexbox 后添加 margin: 0 auto 会破坏布局。

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}
body {
  font-family: 'Open Sans', sans-serif;
  background: #fff;
  color: #333;
  line-height: 1.6;
  background: url('/img/showcase.jpg.jpg');
}
#navbar {
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  top: 0;
  width: 100%;
  height: 60px;
  background-color: rgba(10, 10, 10, 0.5);
}
#navbar ul {
  display: flex;
  justify-content: space-around;
  text-align: center;
  /* margin: 0 auto; REMOVE THIS LINE */
  padding: 0;
}
#navbar li {
  list-style: none;
  padding: 20px;
}
<nav id="navbar">
  <div class="logo">
    <h1>Welcome</h1>
  </div>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
  </ul>
</nav>

此外,将您的徽标作为列表项可能会产生更好的结果:

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}
body {
  font-family: 'Open Sans', sans-serif;
  background: #fff;
  color: #333;
  line-height: 1.6;
  background: url('/img/showcase.jpg.jpg');
}
#navbar, li { /* added flex to li's so content stays vertically centered */
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  top: 0;
  width: 100%;
  height: 60px;
  background-color: rgba(10, 10, 10, 0.5);
}
#navbar ul {
  display: flex;
  justify-content: space-around;
  text-align: center;
  /* margin: 0 auto; REMOVE THIS LINE */
  padding: 0;
}
#navbar li {
  list-style: none;
  padding: 0 20px; /* fixed vertical padding to 0 from 20px */
  background-color: transparent; /* Added for continuity with your design */
}
<nav id="navbar">
  <ul>
    <li>
      <div class='logo'>
        <h1>Welcome</h1>
      </div>
    </li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
  </ul>
</nav>

如果您想在徽标和列表项之间添加 space,您可以将 margin-right: ( n ) 添加到徽标本身。见下文:

* {
  box-sizing: border-box;
  padding: 0;
  margin: 0;
}
body {
  font-family: 'Open Sans', sans-serif;
  background: #fff;
  color: #333;
  line-height: 1.6;
  background: url('/img/showcase.jpg.jpg');
}
#navbar, li { /* added flex to li's so content stays vertically centered */
  display: flex;
  justify-content: center;
  align-items: center;
  flex: 1;
  top: 0;
  width: 100%;
  height: 60px;
  background-color: rgba(10, 10, 10, 0.5);
}
#navbar ul {
  display: flex;
  justify-content: space-around;
  text-align: center;
  /* margin: 0 auto; REMOVE THIS LINE */
  padding: 0;
}
#navbar li {
  list-style: none;
  padding: 0 20px; /* fixed vertical padding to 0 from 20px */
  background-color: transparent; /* Added for continuity with your design */
}
.logo { /* Added space between logo and list items */
  margin-right: 5rem; /* you can add units in pixels as well if you would like */
}
<nav id="navbar">
  <ul>
    <li>
      <div class='logo'>
        <h1>Welcome</h1>
      </div>
    </li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
  </ul>
</nav>

请注意,无论您向 .logo 添加多少边距,所有内容都会自动居中。

如果我正确理解了你的问题(请告诉我),这就是你想要的:

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background: #fff;
  background: url('/img/showcase.jpg.jpg');
  color: #333;
  font-family: 'Open Sans', sans-serif;
  line-height: 1.6;
}

#navbar,
#navbar ul {
  display: flex;
}

#navbar {
  align-items: center;
  background-color: rgba(10, 10, 10, 0.5);
  height: 60px;
  justify-content: space-between;
}

#navbar ul {
  justify-content: center;
  width: 100%;
}

#navbar li {
  list-style: none;
  margin: 0 10px 0 10px;
}

.logo {
  margin-left: 20px;
}
<nav id="navbar">
  <div class="logo">
    <h1>Welcome</h1>
  </div>
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
    <li><a href="#">Home</a></li>
  </ul>
</nav>