Dropdown Menu CSS

前端 未结 4 522
我寻月下人不归
我寻月下人不归 2020-12-11 14:32

I cannot figure out what is wrong with my dropdown menu. When I over over the main level link, the drop down appear but at the left of my screen instead of underneath the ma

相关标签:
4条回答
  • 2020-12-11 14:38

    It is probably jumping towards the closest relative container. So configure your list to act as relative container:

    ul#menu > li {
      position: relative;
    }
    

    Also there was unnecessary float in your anchor tags, your li are already set to display as inline there is no point in float them

    ul#menu a {
        display: inline-block;
        padding: 10px 16px;
        /* ... */
    }
    

    Here your fixed code

    0 讨论(0)
  • 2020-12-11 14:49

    Try clearing your floats on the parent elements.

    0 讨论(0)
  • 2020-12-11 14:53

    If cannot want use : position:relative, then use : left:auto; instead : left:0; http://codepen.io/anon/pen/KAGhL But position relative will be usefull and will give ability to set z-index to keep menu on top of other element

    0 讨论(0)
  • 2020-12-11 14:58

    If you are free to change the CSS style, why not think of doing something like this. Why not try with this CSS style?

    HTML

    <ul class="nav">
        <li>
            <a href="#">Menu 1</a>
            <ul>
                <li><a href="#">Sub Menu Item</a></li>
                <li><a href="#">Sub Menu Item</a></li>
                <li><a href="#">Sub Menu Item</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Menu 2</a>
            <ul>
                <li><a href="#">Sub Menu Item</a></li>
                <li><a href="#">Sub Menu Item</a></li>
                <li><a href="#">Sub Menu Item</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Menu 3</a>
            <ul>
                <li><a href="#">Sub Menu Item</a></li>
                <li><a href="#">Sub Menu Item</a></li>
                <li><a href="#">Sub Menu Item</a></li>
            </ul>
        </li>
    </ul>
    

    CSS

    * {font-family: "Segoe UI", Tahoma;}
    ul.nav {border-bottom: 1px solid #999;}
    ul.nav li a {display: block; text-decoration: none; color: #333; padding: 5px; border: 1px solid #fff;}
    ul.nav > li:hover {border: 1px solid #666; border-bottom: 1px solid #fff;}
    ul.nav li a:hover {background: #ccc; border: 1px solid #999;}
    ul.nav > li {display: inline-block; position: relative; border: 1px solid #fff;}
    ul.nav > li ul {display: none; position: absolute; left: -1px; width: 150px; border: 1px solid #666; border-top-color: #fff; margin-top: 1px;}
    ul.nav > li:hover ul {display: block;}
    ul.nav > li ul li {display: block;} /* Vertical Menu */
    ul.nav > li ul li {display: inline-block;} /* Horizontal Menu */
    

    Fiddle:
    http://jsfiddle.net/vMuxA/ (Vertical Menu)
    http://jsfiddle.net/vMuxA/1/ (Horizontal Menu)

    0 讨论(0)
提交回复
热议问题