0

我正在尝试将下拉垂直菜单项打开到左侧,现在它在右侧打开,但我希望它应该在左侧打开,

我怎么能做到这一点?,我写了以下事情来完成这项工作

CSS:

#navContainer
{
    margin: 0;
    padding: 0;
    background: red;
    border: 1px solid #7398ba;
    text-align: center;
    width: 220px;
}

    #navContainer ul
    {
        margin: 0;
        padding: 0;
        list-style: none;
    }

        #navContainer ul li
        {
            position: relative;
        }

            #navContainer ul li span
            {
                display: block;
            }

            #navContainer ul li a
            {
                text-decoration: none;
                color: white;
                display: block;
                padding: 8px;
            }

            #navContainer ul li span:hover
            {
                /*background: pink;*/
            }

            #navContainer ul li a:hover
            {
                background: black;
            }

        #navContainer ul ul
        {
            position: absolute;
            display: none;
        }

            #navContainer ul ul li a
            {
                background: #bec8cb;
            }

        #navContainer ul li:hover ul
        {
            width: 80%;
            position: absolute;
            display: block;
            left: 218px;
            top: 0;
        }

HTML :-

<div id="navContainer">
        <ul>
            <li><span><a href="#">Home</a></span></li>
            <li>
                <span><a href="#">About </a></span>
                <ul>
                    <li><a href="#">Our business</a></li>
                    <li><a href="#">Our History </a></li>
                </ul>
            </li>
            <li>
                <span><a href="#">Services</a></span>
                <ul>
                    <li><a href="#">Web Design</a></li>
                    <li><a href="#">Web templates </a></li>
                    <li><a href="#">Tutorials </a></li>
                </ul>
            </li>
            <li><span><a href="#">Contact</a></span></li>
            <li><span><a href="#">News</a></span></li>
        </ul>
    </div>

当前结果:-

在此处输入图像描述

我想要的结果

在此处输入图像描述

4

6 回答 6

0

这次我和你有同样的问题,我通过ul用 a 包装元素来解决这个问题div,并使用了两次绝对位置。这样我才能达到这个结果。

  • 第一次左对齐包装器left: 0
  • 第二次离开扩大了下拉right: 0

代码示例:

<div class="parent">
    <div>Trigger Dropdown</div>
    <div class="wrapper">
        <ul class="dropdown">
            <li>foo</li>
            <li>bar</li>
        </ul>
    </div>
</div>
.parent {
    position: relative;
    margin-left: 100px;
}

.dropdown {
    background: red;
    position: absolute;
    top: 0;
    right: 0;
}

.wrapper {
    position: absolute;
    top: 0;
    left: 0;
}
于 2019-07-10T06:39:52.487 回答
-1

只需更改leftright您的绝对定位即可。在你的情况下:

改变:

#navContainer ul li:hover ul
    {
        width: 80%;
        position: absolute;
        display: block;
        left: 218px;
        top: 0;
    }

至:

#navContainer ul li:hover ul
    {
        width: 80%;
        position: absolute;
        display: block;
        right: 218px;
        top: 0;
    }
于 2013-01-28T09:19:12.067 回答
-1

或者您可以像这样将像素#navContainer ul li:hover ul从正变为负

left: 218px==> left: -218px/无论你的下拉菜单的宽度是多少/

于 2013-01-28T09:21:03.690 回答
-1

更改left:218pxto - 值并赋予margin-leftmainnavigation ul

#navContainer
{
    margin: 0 0 0 218px;
    padding: 0;
    background: red;
    border: 1px solid #7398ba;
    text-align: center;
    width: 220px;
}
     #navContainer ul li:hover ul
        {
            width: 80%;
            position: absolute;
            display: block;
            left: -178px;
            top: 0;
        }

演示

于 2013-01-28T09:21:41.283 回答
-1
#navContainer
{
    margin: 0;
    padding: 0;
    background: red;
    border: 1px solid #7398ba;
    text-align: center;
    width: 220px;
    position:relative;
    float:right;
}

#navContainer ul
{
    margin: 0;
    padding: 0;
    list-style: none;

}

#navContainer ul li
{
    position: relative;
}

#navContainer ul li span
{
    display: block;
}

#navContainer ul li a
{
    text-decoration: none;
    color: white;
    display: block;
    padding: 8px;
}

#navContainer ul li span:hover
{
    /*background: pink;*/
}

#navContainer ul li a:hover
{
    background: black;
}

#navContainer ul ul
{
    position: relative;
    display: none;

}

#navContainer ul ul li a
{
    background: #bec8cb;
}

#navContainer ul li:hover ul
{
    width: 80%;
    position: absolute;
    display: block;
    left: 218px;
    top: 0;
    margin-left:-380px;
}



<div id="navContainer">
        <ul>
            <li><span><a href="#">Home</a></span></li>
            <li>
                <span><a href="#">About </a></span>
                <ul>
                    <li><a href="#">Our business</a></li>
                    <li><a href="#">Our History </a></li>
                </ul>
            </li>
            <li>
                <span><a href="#">Services</a></span>
                <ul>
                    <li><a href="#">Web Design</a></li>
                    <li><a href="#">Web templates </a></li>
                    <li><a href="#">Tutorials </a></li>
                </ul>
            </li>
            <li><span><a href="#">Contact</a></span></li>
            <li><span><a href="#">News</a></span></li>
        </ul>
    </div>
于 2013-01-28T10:03:17.927 回答
-1

您需要将navContainer向右移动,例如:

#navContainer
{
    margin: 0;
    padding: 0;
    background: red;
    border: 1px solid #7398ba;
    text-align: center;
    width: 220px;
    float: right;
}

然后使用right属性而不是left属性ul

#navContainer ul li:hover ul
{
    width: 80%;
    position: absolute;
    display: block;
    left: 218px;
    top: 0;
}
于 2013-01-28T09:25:10.183 回答