如何将列表项目居中

问题描述 投票:0回答:3

我只是不明白。如何将列表项目置于ul中心?

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>
html css html-lists listitem
3个回答
0
投票

您可以使用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>

0
投票

你可以添加

 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 */
}

然后根据需要使用边距右边的锚点。


0
投票

只需将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>
© www.soinside.com 2019 - 2024. All rights reserved.