嵌套的li元素未正确对齐

时间:2015-07-21 15:41:30

标签: html css

我能够显示所有嵌套的li's内联。我不明白的是为什么AboutOur Team li元素之间存在差距。我已尝试将marginpadding元素的ulli设置为0

<div ng-show = "buttonDisplay" id = "buttonDisplayContent">
          <ul>
            <li><a href = "#"> Home </a></li>
            <li class = "subLi"><a href = "#"> About </a></li>
              <ul class = "nested">
                <li> <a href = "#"> Our Team </a> </li>
                <li> <a href = "#"> Our efforts </a> </li>
              </ul>
            <li class = "nextToNested"><a href = "#"> Blog </a></li>
            <li class = "subLi"><a href = "#"> Services </a></li>
              <ul class = "nested">
                <li> <a href = "#"> Design </a> </li>
                <li> <a href = "#"> Web </a> </li>
                <li> <a href = "#"> Learn </a> </li>
                <li> <a href = "#"> Invent </a> </li>
              </ul>
            <li class = "nextToNested"><a href = "#"> Portfolio </a></li>
            <li><a href = "#"> Contact </a></li>
          </ul>
</div>

CSS

#buttonDisplayContent ul {
  list-style-type: none;
  padding: 0;
}

#buttonDisplayContent ul ul {
  list-style-type: none;
  padding: 0;
}

#buttonDisplayContent ul a {
  text-decoration: none;
  color: #000;
  font-size: 25px;
  font-weight: bold;
}

#buttonDisplayContent ul ul a {
  text-decoration: none;
  color: lightgray;
  font-size: 15px;
  font-weight: bold;
}

.subLi {
  float: left;
  margin: 0;
  padding: 0;
  list-style-type: none;
}

.nested {
  margin-left: 0;
}

.nested li {
  display: inline-block;
  padding-bottom: 6px;
  padding-right: 1%;
  padding-left: 1%;
  padding-top: 8px;
}

#buttonDisplayContent ul li:hover {
  background-color: black;
}

更新

感谢您的帮助。我更改了我的HTMLCSS,如下所示。 Li个元素仍未根据需要对齐。这是一个小提琴:https://jsfiddle.net/qvq87ke1/2/

HTML

<div ng-show = "buttonDisplay" id = "buttonDisplayContent" >
          <ul>
            <li><a href = "#"> Home </a></li>
            <li class = "subLi"><a href = "#">About </a>
              <ul class = "nested">
                <li> <a href = "#"> Our Team </a> </li>
                <li> <a href = "#"> Our efforts </a> </li>
              </ul>
            </li>
            <li class = "nextToNested"><a href = "#"> Blog </a></li>
            <li class = "subLi"><a href = "#"> Services </a>
              <ul class = "nested">
                <li> <a href = "#"> Design </a> </li>
                <li> <a href = "#"> Web </a> </li>
                <li> <a href = "#"> Learn </a> </li>
                <li> <a href = "#"> Invent </a> </li>
              </ul>
            </li>
            <li class = "nextToNested"><a href = "#"> Portfolio </a></li>
            <li><a href = "#"> Contact </a></li>
          </ul>
        </div>

CSS

#buttonDisplayContent {
    background-color: #141516;
    width: 100%;
    margin-top: 9%;
    text-align: center;
    position: absolute;
    opacity: 0.9;
}

#buttonDisplayContent ul {
        list-style-type: none;
        padding: 0;
}

#buttonDisplayContent ul ul { 
            list-style-type: none;
            padding: 0;
}

#buttonDisplayContent ul a { 
            text-decoration: none;
            color: #fff;
            font-size: 50px;
            font-weight: bold;
}

#buttonDisplayContent ul ul a {
                text-decoration: none;
                color: lightgray;
                font-size: 40px;
                font-weight: bold;
}

.subLi {
        float: left;
        margin: 0;
        padding: 0;
        list-style-type: none;
}

.nested {
    float: right;
    margin-left: 0;
}

.nested li {
        display: inline-block;
        padding-bottom: 6px;
        padding-right: 1%;
        padding-left: 1%;
        padding-top: 8px;
}

#buttonDisplayContent ul li:hover {
            background-color: black;
}

5 个答案:

答案 0 :(得分:3)

您的标记看起来不正确。嵌套未排序或有序列表时,它们应与li一起包含,它们将创建一个子列表。

像这样:

<ul>
    <li>One</li>
    <li>Two
        <ul>
            <li>One - Sub Two</li>
            <li>Two - Sub Two</li>
        </ul>
    </li>
    <li>Three</li>
</ul>

您要在ul

之间添加li
<li class="subLi"><a href="#">About</a></li>
<ul class="nested">
    <li><a href="#">Our Team</a></li>
    <li><a href="#">Our efforts</a></li>
</ul>
<li class="nextToNested"><a href="#">Blog</a></li>

答案 1 :(得分:0)

根据更新的问题更新了答案..

尝试在嵌套display:inside;上设置<ul>,并将嵌套的<ul>放在<li>

P.S。屏幕截图或小提琴(或至少是你的普通CSS)真的很有用..

答案 2 :(得分:0)

您应该使用您的代码发布JSFiddle,这将更容易为您提供帮助。

否则,作为一种良好做法,UL嵌套嵌套应该在之前的LI中(这应该是父级的)。

<li class = "subLi">
  <a href = "#">About </a>
  <ul class = "nested">
    <li> <a href = "#"> Our Team </a> </li>
    <li> <a href = "#"> Our efforts </a> </li>
  </ul>
</li>

也许这就是问题所在。

如果它可以帮助你: Proper way to make HTML nested list?

祝你好运&#39;

答案 3 :(得分:0)

“a”元素也可能在浏览器中有一些填充/边距。最佳实践是在项目开始时import a reset.css file清除所有应用浏览器的CSS并自行设置样式或使用框架。

答案 4 :(得分:0)

我明白了。我不必要地浮动了li个元素。

<div ng-show = "buttonDisplay" id = "buttonDisplayContent" >
  <ul>
    <li><a href = "#"> Home </a></li>
    <li class = "subLi"><a href = "#">About </a>
      <ul class = "nested">
        <li> <a href = "#"> Our Team </a> </li>
        <li> <a href = "#"> Our efforts </a> </li>
      </ul>
    </li>
    <li class = "nextToNested"><a href = "#"> Blog </a></li>
    <li class = "subLi"><a href = "#"> Services </a>
      <ul class = "nested">
        <li> <a href = "#"> Design </a> </li>
        <li> <a href = "#"> Web </a> </li>
        <li> <a href = "#"> Learn </a> </li>
        <li> <a href = "#"> Invent </a> </li>
      </ul>
    </li>
    <li class = "nextToNested"><a href = "#"> Portfolio </a></li>
    <li><a href = "#"> Contact </a></li>
  </ul>
</div>

SASS

#buttonDisplayContent 
    background-color: #141516
    width: 100%
    margin-top: 9%
    text-align: center
    position: absolute
    opacity: 0.9

#buttonDisplayContent
    ul
        list-style-type: none
        padding: 0

#buttonDisplayContent
    ul
        ul 
            list-style-type: none
            padding: 0

#buttonDisplayContent
    ul
        a 
            text-decoration: none
            color: #fff
            font-size: 50px
            font-weight: bold

#buttonDisplayContent
    ul
        ul
            a
                text-decoration: none
                color: lightgray
                font-size: 40px
                font-weight: bold

.subLi
        //float: left
        margin: 0
        padding: 0
        list-style-type: none

.nested
    //float: right
    margin-left: 0
    display: inline

.nested
    li
        display: inline
        padding-bottom: 6px
        padding-right: 1%
        padding-left: 1%
        padding-top: 8px

#buttonDisplayContent
    ul
        li:hover
            background-color: black
相关问题