有没有一种方法可以将导航菜单中li下方的下拉菜单居中?

时间:2019-12-12 04:51:01

标签: html css drop-down-menu position nav

我正在尝试在CSS的水平导航项下创建一个下拉菜单。它可以工作,但是我希望它位于父菜单项(在本例中为“服务”)的下方。这是它的当前状态。我试着给它一个宽度并做margin:auto,但这没用。 [更新:这是我的jsfiddle: https://jsfiddle.net/7othf8z2/#&togetherjs=a10GMrJQcD

CSS:

DATE   BUSIEST DAY    NO. OF DISHES
-----------------------------------
1      SUN            37
14     SAT            10

HTML:

body{
      font-family: Arial, Verdana, sans-serif;
      background: #24425a;
    }
#wrapper{
             background: #a4c1da;
             color: #172E40;
             width: 80%;
             margin: auto;
             grid-template-columns: 170px 1fr 217px;
             grid-template-rows: 200px auto 50px;
        }
nav {  background: #24425a;  
       padding: 0.05em 0.25em;
       position: relative;
       grid-row: 2/3;
       grid-column: 1/3;
} 
nav ul {
            text-align: center;
       }
nav ul ul { 
            position: absolute; 
            top: 100%;
            left: 21%;
            display: none; 
            text-align: center;
            background: #24425a;
          }

nav li:active ul { display: block;}
nav li { 
      display: inline-block;
       }
nav a:link{
            color: #fff;
          }
nav a:active{
                text-shadow: .05em .05em .05em #fff;
            }
nav a:visited{
                color: #CDDAE5;
             }

2 个答案:

答案 0 :(得分:0)

嗨,请尝试以下代码:

<div id="container">
    <nav>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">WordPress</a>
            <!-- First Tier Drop Down -->
            <ul>
                <li><a href="#">Themes</a></li>
                <li><a href="#">Plugins</a></li>
                <li><a href="#">Tutorials</a></li>
            </ul>        
            </li>

        </ul>
    </nav>

</div>

css:

/* CSS Document */

body {
    background: #212121;
    font-size:22px;
    line-height: 32px;
    color: #ffffff;
    word-wrap:break-word !important;
    font-family: 'Open Sans', sans-serif;
    }


#container {
    margin: 0 auto;
}


nav {
    margin: 50px 0;
    background-color: #E64A19;
}

nav ul {
    padding: 0;
  margin: 0;
    list-style: none;
    position: relative;
    }

nav ul li {
    display:inline-block;
    background-color: #E64A19;
    }

nav a {
    display:block;
    padding:0 10px; 
    color:#FFF;
    font-size:20px;
    line-height: 60px;
    text-decoration:none;
}

nav a:hover { 
    background-color: #000000; 
}

/* Hide Dropdowns by Default */
nav ul ul {
    display: none;
    position: absolute; 
    top: 60px; /* the height of the main nav */
}

/* Display Dropdowns on Hover */
nav ul li:hover > ul {
    display:inherit;
}

/* Fisrt Tier Dropdown */
nav ul ul li {
    width:170px;
    float:none;
    display:list-item;
    position: relative;
  text-align:center;
}

/* Second, Third and more Tiers */
nav ul ul ul li {
    position: relative;
    top:-60px; 
    left:170px;
}


/* Change this in order to change the Dropdown symbol */
li > a:after { content:  ' +'; }
li > a:only-child:after { content: ''; }

答案 1 :(得分:0)

尝试将此设置留给16%,宽度为1024

@media only screen and (max-width: 1024px)
nav ul ul {
    position: absolute;
    top: 100%;
    left: 16%;
    display: none;
    text-align: center;
    background: #24425a;
}