如何居中列表项

How to center list items

我只是没弄清楚。如何在 ul 中将 list items 居中?

body {margin: 0}

ul {
  width: 1000px;
  margin: 0 auto;
  padding: 0;
  list-style-type: none;
  margin-top: 30px;
  overflow: hidden;
  background-color: #333;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

ul li {
  display: inline;
}

li a {
  display: inline-block;
  padding: 8px 11px;
  margin-right: 10px;
  color: #fff;
  text-decoration: none;
  text-align: center;
  font-size: 18px;
  line-height: 50px;
}

li a:hover {
  background-color: #111;
}
<ul>
  <li><a href="#">Home</a><li>
  <li><a href="#">Over mij</a><li>
  <li><a href="#">PO's</a><li>
  <li><a href="#">Contact</a><li>
</ul>

您可以使用 Flexbox:

body {margin: 0}

ul {
  margin: 0 auto;
  padding: 0;
  list-style-type: none;
  margin-top: 30px;
  overflow: hidden;
  background-color: #333;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  display: flex; /* displays flex-items (children) inline */
  justify-content: center; /* centers them horizontally */
}

ul li {
  display: inline;
}

li a {
  display: inline-block;
  padding: 8px 11px;
  margin-right: 10px;
  color: #fff;
  text-decoration: none;
  text-align: center;
  font-size: 18px;
  line-height: 50px;
}

li a:hover {
  background-color: #111;
}
<ul>
  <li><a href="#">Home</a><li>
  <li><a href="#">Over mij</a><li>
  <li><a href="#">PO's</a><li>
  <li><a href="#">Contact</a><li>
</ul>

您只需添加

 text-align:center; 

在 ul 样式中。你会实现的。

ul {
  margin: 0 auto;
  padding: 0;
  list-style-type: none;
  margin-top: 30px;
  overflow: hidden;
  text-align:center; 
  background-color: #333;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  display: flex; /* added */
  justify-content: center; /* added */
}

然后根据需要调整锚点的边距。

只需将 text-align:center 添加到 ul

ul {
  width: 1000px;
  margin: 0 auto;
  padding: 0;
  list-style-type: none;
  margin-top: 30px;
  text-align: center;
  overflow: hidden;
  background-color: #333;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
}

ul li {
  display: inline;
}

li a {
  display: inline-block;
  padding: 8px 11px;
  margin-right: 10px;
  color: #FFFFFF;
  text-decoration: none;
  text-align: center;
  font-size: 18px;
  line-height: 50px;
}

li a:hover {
  background-color: #111;
}
<ul>
  <li><a href="#">Home</a></li>
  <li><a href="#">Over mij</a></li>
  <li><a href="#">PO's</a></li>
  <li><a href="#">Contact</a></li>
</ul>