0

I am able to display all the nested li's inline. What I don't understand is why there is a gap between About and Our Team li elements. I have already tried setting the margin and padding of both ul and li elements to 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;
}

UPDATE

Thanks for all your help. I changed my HTML and CSS as follows. Li elements are still not aligning as desired. Here is a fiddle: 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 Answers5

3

Your markup looks like it is incorrect. When nesting un-ordered or ordered lists they should be contained withing the li they will be creating a sub list for.

Like this:

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

You are adding your ul between the 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>
hungerstar
  • 21,206
  • 6
  • 50
  • 59
  • 1
    was about to post the same thing – Mir Jul 21 '15 at 15:52
  • I changed my markup as suggested, but the `li` elements are still not aligning as desired. Please see the update on my question. –  Jul 21 '15 at 16:24
0

Updated answer based on updated question..

Try setting display:inside; on your nested <ul> and put your nested <ul> inside of the <li>

P.S. A screenshot or a fiddle (or at least your plain CSS) would be really helpful..

mouses
  • 69
  • 1
  • 8
0

You should post a JSFiddle with your code, that would be easier to help you.

Otherwise, as a good practice, the UL nested nested should be in the previous LI (which should it parent so).

<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>

Maybe this is where the problem comes from.

If it can helps you: Proper way to make HTML nested list?

Good Luck'

Community
  • 1
  • 1
Alexis B.
  • 1,105
  • 8
  • 13
  • I changed my markup as suggested, but the `li` elements are still not aligning as desired. Please see the update on my question. –  Jul 21 '15 at 16:25
0

It's possible there is some padding/margin on the "a" element as well from the browser. Best practices is to import a reset.css file at the beginning of your project to clear all browser-applied CSS and style it on your own or using a framework.

ajain
  • 75
  • 5
0

I figured it out. I was floating the li elements unnecessarily.

<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