gpt4 book ai didi

css - 以最基本的 CSS 下拉菜单为中心的问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:48:20 29 4
gpt4 key购买 nike

我有一个相当简单的 CSS 下拉菜单,我终其一生都无法弄清楚如何在不破坏下拉效果的情况下使页面居中,这是我尝试的几乎所有方法的结果。我做错了什么?

片段中的代码

html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,
big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,
dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,
details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video
{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline;color:black;text-decoration:none;}
article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block;}
body{line-height:1;}ol,ul{list-style:none;}blockquote,q{quotes:none;}blockquote:before,blockquote:after,
q:before,q:after{content:'';content:none;}table{border-collapse:collapse;border-spacing: 0;}

#wrap{max-width:1280px;width:100%;margin:auto;}

nav ul li{line-height:44px;float:left;background-color:#64abfb;padding:10px;}
nav ul li a{color:#FFF;padding:10px;font-size:20px;}
nav ul li ul{display:none;}
nav ul li:hover ul{display:list-item;position:absolute;margin-top:5px;margin-left:-10px;}
nav ul li:hover ul li{float:none;}

li a:hover{border-bottom:3px red solid;}
li > a:after{content:' »';}
li > a:only-child:after{content:'';}
<body>
<div id="wrap">
<nav>
<ul>
<li><a href="#">Menu 1</a></li>
<li><a href="#">Menu 2</a></li>
<li>
<a href="#">Menu 3</a>
<ul>
<li><a href="#">Drop 1</a></li>
<li><a href="#">Drop 2</a></li>
<li><a href="#">Drop 3</a></li>
</ul>
</li>
<li><a href="#">Menu 4</a></li>
</ul>
</nav>
</div>
</body>

最佳答案

简单地设置 ul 元素的宽度和边距:

#wrap nav ul {
width: 430px;
margin: auto;
}

片段:

html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,
big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,
dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,
details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video
{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline;color:black;text-decoration:none;}
article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block;}
body{line-height:1;}ol,ul{list-style:none;}blockquote,q{quotes:none;}blockquote:before,blockquote:after,
q:before,q:after{content:'';content:none;}table{border-collapse:collapse;border-spacing: 0;}

#wrap{max-width:1280px;width:100%;margin:auto;}

nav ul {width: 430px; margin: auto;}
nav ul li{line-height:44px;float:left;background-color:#64abfb;padding:10px;}
nav ul li a{color:#FFF;padding:10px;font-size:20px;}
nav ul li ul{display:none;}
nav ul li:hover ul{display:list-item;position:absolute;margin-top:5px;margin-left:-10px;}
nav ul li:hover ul li{float:none;}

li a:hover{border-bottom:3px red solid;}
li > a:after{content:' »';}
li > a:only-child:after{content:'';}
<body>
<div id="wrap">
<nav>
<ul>
<li><a href="#">Menu 1</a></li>
<li><a href="#">Menu 2</a></li>
<li>
<a href="#">Menu 3</a>
<ul>
<li><a href="#">Drop 1</a></li>
<li><a href="#">Drop 2</a></li>
<li><a href="#">Drop 3</a></li>
</ul>
</li>
<li><a href="#">Menu 4</a></li>
</ul>
</nav>
</div>
</body>

请注意,如果您希望避免指定 ul 宽度,您可以将其显示为 inline-block 并将其父元素的 text-align 设置为居中。

片段 2:

html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,
big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,
dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,
details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video
{margin:0;padding:0;border:0;font-size:100%;font:inherit;vertical-align:baseline;color:black;text-decoration:none;}
article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block;}
body{line-height:1;}ol,ul{list-style:none;}blockquote,q{quotes:none;}blockquote:before,blockquote:after,
q:before,q:after{content:'';content:none;}table{border-collapse:collapse;border-spacing: 0;}

#wrap{max-width:1280px;width:100%;margin:auto;}

nav {text-align: center}
nav ul {display: inline-block; margin: auto;}
nav ul li{line-height:44px;float:left;background-color:#64abfb;padding:10px;}
nav ul li a{color:#FFF;padding:10px;font-size:20px;}
nav ul li ul{display:none;}
nav ul li:hover ul{display:list-item;position:absolute;margin-top:5px;margin-left:-10px;}
nav ul li:hover ul li{float:none;}

li a:hover{border-bottom:3px red solid;}
li > a:after{content:' »';}
li > a:only-child:after{content:'';}
<body>
<div id="wrap">
<nav>
<ul>
<li><a href="#">Menu 1</a></li>
<li><a href="#">Menu 2</a></li>
<li>
<a href="#">Menu 3</a>
<ul>
<li><a href="#">Drop 1</a></li>
<li><a href="#">Drop 2</a></li>
<li><a href="#">Drop 3</a></li>
</ul>
</li>
<li><a href="#">Menu 4</a></li>
</ul>
</nav>
</div>
</body>

关于css - 以最基本的 CSS 下拉菜单为中心的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26330881/

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