CSS下拉菜单边框和轻微偏移

时间:2018-11-26 19:17:23

标签: html css menu navbar dropdown

我需要创建以下下拉菜单: CSS dropdown menu SOLUTION

我的代码无法正确完成工作: My (wrong) SOLUTION

此刻我的代码:

.parent-menu li {
        display: block;
        text-transform: uppercase;
        border-bottom:none;
        border-top:none;
        padding:10px;
        border-right:none;
    }
    .sub-menu-caption, .upper-menu{
        border:1px solid red;
    }
    .parent-menu{
        width:100%;
    }
    .sub-menu {
        font-style: italic;
        display: none;
        width:170px;
        height:90px;
        position:absolute;
        background:black;
        margin:10px 0 0 -11px;
        font-size:1em;    
    }
    .sub-menu li{
        border-bottom:1px solid red;
        z-index: 1;
    }
    .sub-menu li:last-child {
        border-bottom: none;
    }
    .sub-menu-caption{
        left:auto;
        right:auto;
        text-align:center;
    }
    .sub-menu-caption:hover .sub-menu {
        display: block;
        border:solid red 1px;
        border-top:none;
        padding-bottom:5%;
    }
<nav id="main-menu" class="section">
        <ul class="parent-menu">
            <li class="sub-menu-caption">Automobiles
                <ul class="sub-menu">
                    <li><a href="https://auto.ferrari.com/en_US/sports-cars-models/car-range/812-superfast">812 Superfast</a></li>
                    <li><a href="https://auto.ferrari.com/en_US/sports-cars-models/car-range/gtc4lusso">GTC4Lusso</a></li>
                    <li><a href="https://auto.ferrari.com/en_US/sports-cars-models/car-range/ferrari-488-gtb">488 GTB</a></li>
                    <li><a href="https://auto.ferrari.com/en_US/sports-cars-models/car-range/488-spider">488 Spider</a></li>
                </ul>
            </li>
            <li class="upper-menu"><a href="https://auto.ferrari.com/en_US/owners/">services</a></li>
            <li class="upper-menu"><a href="https://preowned.ferrari.com/us">pre-owned</a></li>
            <li class="upper-menu"><a href="https://auto.ferrari.com/en_US/car-dealers/">Find a dealer</a></li>
            <li class="upper-menu"><a href="https://auto.ferrari.com/en_US/register/">my ferrari</a></li>
        </ul>
    </nav>

下拉菜单的右侧略有偏移,边框并没有完全穿过:( 可能是什么问题呢?我该如何解决?不,我不为法拉利工作:D 谢谢!

1 个答案:

答案 0 :(得分:0)

没关系。对CSS代码进行了一些不同的调整,找到了正确的解决方案。

.parent-menu {
    overflow: hidden;
    background-color: black;
}
.parent-menu > li{
    padding: 10px;
    padding-left: 16px;
    padding-right: 16px;
    float: left;
    color: rgba(255,255,255,0.7);
    border-right: 1px solid rgba(255,0,0,0.5);
}

.parent-menu > :first-child {
    color: red;
    padding-left: 18px;
    padding-right: 18px;
    border-left: 1px solid rgba(255,0,0,0.5);
}

.sub-menu li{
    display: none;
    position: absolute;
    z-index:1;
}
.sub-menu {
    display:block;
    color: blue;
    float: left;
    text-align:left;
}
.sub-menu-caption:hover .sub-menu{
    display: block;
    position: absolute;
    top: 30px;
    left: 0px;
}   
.sub-menu-caption:hover .sub-menu li{
    display: block;
    position: static; 
    background-color: black;
    width: 100%;
    border: 1px solid rgba(255,0,0,0.5);
    border-top: 0;
    font-style: italic;
    font-size: 1em;
    padding-top: 5px;
    padding-bottom: 5px;
    padding-left: 10px; 
    padding-right: 3px;
}
.sub-menu-caption:hover .sub-menu :last-child{
    border-radius: 0px 0px 4px 4px;
}
.sub-menu-caption:hover .sub-menu :first-child{
    padding-top: 12px;
}

.sub-menu-caption:hover .sub-menu li a:hover{
    color: red;
}
#main-menu {
    top: 0;
    text-transform: uppercase;
    position: sticky;
    z-index:1;
    margin-bottom: 30px;
}

article {
    position: relative;
    width: 90%;
    margin: auto;
}
article.main-container > :first-child {
    font-variant: small-caps;
}

.parent-menu  a:link {
    text-decoration: none;
    font-weight: 400;
    color: rgba(255,255,255,0.7);
}
.parent-menu  a:visited {
    text-decoration: none;
    font-weight: 400;
    color: rgba(255,255,255,0.7);
}
.parent-menu  a:hover {
    text-decoration: underline;
    font-weight: 400;
    color: rgba(255,255,255,0.7);
}
.parent-menu a:active {
    text-decoration: none;
    font-weight: 400;
    color: rgba(255,255,255,0.7);
}
li.right-menu.upper-menu {
    background-color: rgba(255,0,0,0.5);
    float: right;
    background-clip: padding-box;
}