gpt4 book ai didi

html - 有没有办法将导航栏中 li 下的下拉菜单居中?

转载 作者:行者123 更新时间:2023-11-27 22:54:50 29 4
gpt4 key购买 nike

我正在尝试在 css 中的水平导航项下制作一个下拉菜单。它有效,但我希望它在本例中为“服务”的父菜单项下居中。这是它目前的状态。我尝试给它一个宽度并执行 margin:auto,但这没有用。 [更新:这是我的 jsfiddle: https://jsfiddle.net/7othf8z2/#&togetherjs=a10GMrJQcD

CSS:

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;
}

HTML:

<div id="wrapper">
/*more stuff goes here like header, main, aside, footer*/
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="services.html">Services</a>
</ul>
<ul>
<li><a href="postpartum.html">Postpartum</a></li>
<li><a href="depression.html">Anxiety</a></li>
</ul>
<ul>
</li>
<li><a href="appointments.html">Appointments</a></li>
<li><a href="qualifications.html">Qualifications</a></li>
<li><a href="resources.html">Resources</a></li>
</ul>
</nav>
</div>

最佳答案

您好,请尝试以下代码:

<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: ''; }

关于html - 有没有办法将导航栏中 li 下的下拉菜单居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59297903/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com