2

I have a problem in my CSS. I am creating a vertical menu but my problem is I don't know how to align the second level of categories, with the first. This is also a problem going from the second level to a third level; it also align to the parent level.

My format is like this:

Parent 1
   - Second Level 1
      - Third Level 1
      - Third Level 2
      - Third Level 3
Parent 2
   - Second Level 2
      - Third Level 1
      - Third Level 2
      - Third Level 3

Here's my desired output:

enter image description here

Here's my CSS:

#category-navigation ul ul { display: none }
#category-navigation ul li:hover > ul { 
    display: inline-block;
    position: absolute;
    background-color: #F5F5F5;
    border: 1px solid #CCC;
    top: 20px;
    left: 15%;
    z-index: 4;
    width: 30%;
    box-shadow: 5px 5px 5px #CCC;
}

#category-navigation ul.parent { 
    border-bottom: 1px solid #ccc; 
    padding: 7px 0px;
}

#category-navigation ul.parent li { 
   border-bottom: 1px solid #ccc; 
   padding: 7px 0px; 
}

Here's the fiddle of my code:

http://jsfiddle.net/rochellecanale/4fh680uv/8/

How do I fix the alignment?

2 Answers 2

5

You should give the li elements in the first layer position: relative. That way, the positioning of the child ul elements is relative to the li elements.

To position the child ul elements next to the li elements, you then could give them

top: 0px;
left: 100%;

I updated the jsfiddle accordingly. Please note that this needs some additional tweaking to look really good, but I will not do all the work for you.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks sir, I am really poor in css. :)
Even your html isnt perfect. Please follow the correct syntax while closing the tags.
Ok Im sorry for that. :)
I have modified the code and added some css. Check this link codepen.io/anon/pen/MYZvyQ
^ Goodjob. I was just going to point out how bad his mark-up is <li> <a href="http://">Women's Shoes</li></a>
0

Please check out this updated code

    #category-navigation ul li{position:relative; width: 100%; display: inline-block;}
    #category-navigation ul ul { display: none; }
    #category-navigation ul li:hover > ul { 
        display: inline-block;
        position: absolute;
        background-color: #F5F5F5;
        border: 1px solid #CCC;
        top: 0;
        left: 100%;
        z-index: 4;
        width: 100%;
        box-shadow: 5px 5px 5px #CCC;
    }

1 Comment

Could you explain what you've changed, and why that achieves what the asker wanted?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.