【问题标题】:Can't get my list items to center无法让我的列表项居中
【发布时间】:2012-12-11 02:40:59
【问题描述】:

我有以下代码,我希望列表居中(现在它拉到左边)。知道为什么吗?

<div class="row" style="width:100%;text-align:center;">
<div class ="banner span12" style="padding-top: 0px; padding-bottom:0px;text-align:center;">
<p style="font-size:18px;padding-top:6px;"> Or Start Browsing the Library ... </p></center>
</div>
<br>
<br>
<ul class="nav nav-pills" style="text-align:center;">
<li class="active" style="text-align:center;">
    <a href ="#"> Library1 </a>
</li>
<li>
    <a href ="#"> Library2 </a>
</li>
<li>
    <a href ="#"> Library3 </a>
</li>
</ul>

导航药丸 CSS:

.nav-pills > li > a {
  padding-top: 8px;
  padding-bottom: 8px;
  margin-top: 2px;
  margin-bottom: 2px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
.nav-pills > .active > a,
.nav-pills > .active > a:hover {
  color: #ffffff;
  background-color: #0088cc;
}

【问题讨论】:

标签: html css twitter-bootstrap


【解决方案1】:

给它一个宽度并应用边距:0 auto;

ul.nav-pills {
    width: 300px;
    margin: 0 auto;
}

<div class="row" style="width:100%;text-align:center;">
<div class ="banner span12" style="padding-top: 0px; padding-bottom:0px;text-align:center;">
<p style="font-size:18px;padding-top:6px;"> Or Start Browsing the Library ... </p></center>
</div>
<br>
<br>
<ul class="nav nav-pills" style="text-align:center;">
<li class="active" style="text-align:center;">
    <a href ="#"> Library1 </a>
</li>
<li>
    <a href ="#"> Library2 </a>
</li>
<li>
    <a href ="#"> Library3 </a>
</li>
</ul>

【讨论】:

  • 您的代码在我的页面上将 ul 框居中(但 li 元素不在 ul 中居中)...我现在如何将 ul 中的各个列表元素居中以在 ul 定义中居中?另外,你能解释一下保证金的作用吗?
  • jsfiddle.net/Gfjg2 这是代码...如果您没有任何其他 CSS 覆盖您的样式,它应该可以工作.. 如果没有,请返回 :)
  • 我仍然遇到不准确居中的问题。这是更新后的代码:jsfiddle.net/U8HGz/688
  • jsfiddle.net/U8HGz/691 ...也许这会有所帮助。 ul 的宽度需要完全匹配,如果您关心 ul 的左右填充,它将居中
猜你喜欢
  • 2020-09-25
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
  • 2019-06-12
  • 1970-01-01
  • 2020-12-20
  • 2013-09-29
  • 2014-08-30
相关资源
最近更新 更多