user4982150
user4982150

Reputation:

Nested li elements not aligning properly

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

Upvotes: 0

Views: 1890

Answers (5)

user4982150
user4982150

Reputation:

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

Upvotes: 0

hungerstar
hungerstar

Reputation: 21685

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>

Upvotes: 3

ajain
ajain

Reputation: 75

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.

Upvotes: 0

Alexis B.
Alexis B.

Reputation: 1115

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'

Upvotes: 0

mouses
mouses

Reputation: 69

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

Upvotes: 0

Related Questions