【问题标题】:Nested li elements not aligning properly嵌套的 li 元素未正确对齐
【发布时间】:2015-10-11 04:20:47
【问题描述】:

我能够显示所有嵌套的li's 内联。我不明白为什么AboutOur Team li 元素之间存在差距。我已经尝试将ulli 元素的marginpadding 设置为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;
}

【问题讨论】:

  • UL has margin on the left的可能重复
  • 另外,我几乎 100% 确定这与 sass 无关。请改为发布您的原版 CSS。
  • @TylerH 我已经编辑了我的问题以解释为什么它不是重复的。
  • 很有可能是因为你的HTML无效!
  • 我认为,你有错误的 UL > LI > UL 语法,子 ul,应该是父 li 项的一部分

标签: html css


【解决方案1】:

您的标记看起来不正确。当嵌套无序或有序列表时,它们应该包含在li 中,它们将为它们创建一个子列表。

像这样:

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

您在li 之间添加您的ul

<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 元素仍然没有按需要对齐。请查看我的问题的更新。
【解决方案2】:

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

尝试在嵌套的&lt;ul&gt; 上设置display:inside;,并将嵌套的&lt;ul&gt; 放在&lt;li&gt;

附:屏幕截图或小提琴(或至少您的纯 CSS)会非常有帮助..

【讨论】:

    【解决方案3】:

    你应该在你的代码中发布一个 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?

    祝你好运

    【讨论】:

    • 我按照建议更改了我的标记,但 li 元素仍然没有按需要对齐。请查看我的问题的更新。
    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      我想通了。我不必要地浮动了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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-05-27
        • 1970-01-01
        • 1970-01-01
        • 2012-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-18
        相关资源
        最近更新 更多