浮动子 UL 保持父级宽度
这里有一些类似的问题,但没有一个解决方案适合我的情况。我有一个需要居中的导航。 UL 中有一个子导航,需要与其父 LI 左对齐并水平布局。当我将子导航 LI 向左浮动时,UL 保持其父 LI 的宽度。
我设置了一个固定的 PX,但子导航需要可扩展,以便客户端可以添加或删除导航项目并保持布局。
这是我的 HTML:
<nav>
<ul>
<li><a href="../who_we_are/who_we_are.html">Who We Are</a>
<ul>
<li><a href="../who_we_are/our_approach.html">Our Approach</a></li>
<li><a href="../who_we_are/what_we_do/what_we_do.html">What We Do</a></li>
<li><a href="../who_we_are/leadership.html">Leadership</a></li>
</ul>
</li>
<li><a href="../our_results/our_results.html">Our Results</a>
<ul>
<li><a href="../our_results/case_study_a.html">Case Study A</a></li>
<li><a href="../our_results/case_study_b.html">Case Study B</a></li>
</ul>
</li>
<li><a href="../our_experience/our_experience.html">Our Experience</a>
<ul>
<li><a href="#">Category Experience</a></li>
</ul>
</li>
<li class="current"><a href="what_we_think.html">What We Think</a>
<ul>
<li><a href="#">Articles</a></li>
<li><a href="#">Category Insight</a></li>
<li><a href="#">Research</a></li>
<li><a href="#">Training</a></li>
</ul>
</li>
<li><a href="#">News</a>
<ul>
<li><a href="#">Press Releases</a></li>
<li><a href="#">Articles</a></li>
</ul>
</li>
<li><a href="#">Connect With Us</a>
<ul>
<li><a href="#">Join Us</a></li>
<li><a href="../connect_with_us/find_us.html">Find Us</a></li>
</ul>
</li>
<li><div class="search"><input name="searchbox" type="text" id="searchbox" class="input_style" value="SEARCH" onFocus="if (this.value == 'SEARCH') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'SEARCH';}"><button></button></div></li>
</ul>
</nav>
这是我的 CSS:
nav a,
nav a:visited {
color: #888888;
text-decoration: none;
display: inline-block;
line-height: 30px;
}
nav a:hover {
color: #CCC;
}
nav a:active {
color: #FFF;
}
nav {
text-align: center;
}
nav ul {
display: inline-block;
position: relative;
z-index: 597;
}
nav ul li {
float: left;
padding-left: 15px;
padding-right: 15px;
position: relative;
z-index: 599;
line-height: 30px;
}
nav ul li ul {
display: none;
}
nav li.current a {
background: url(../img/nav_arrow.png) no-repeat center bottom;
color: #FFF;
}
nav li.current ul {
display: block;
position: absolute;
z-index: 598;
top: 31px;
left: 1px;
width: auto;
}
nav li.current ul a {
background: url(none);
color: #888888;
}
nav li.current ul li {
float: left;
display: inline-block;
}
nav .search {
height: 18px;
width: 84px;
padding-left: 4px;
background: url(../img/search_bk.png) no-repeat;
margin-left: 20px;
margin-top: 5px;
float: left;
}
nav .search input {
height: 14px;
width: 68px;
float: left;
background-color: transparent !important;
border-style: none;
}
nav .search input.input_style {
font-family: "proxima-nova", sans-serif;
padding-top: 3px;
color: #777777;
}
nav .search button {
height: 11px;
width: 10px;
float: left;
background-image: url(../img/search_btn.png);
background-color: transparent;
border-style: none;
background-repeat: no-repeat;
margin-top: 3px;
}
这是屏幕截图: 我的屏幕截图
提前致谢!
There are some similar problems here but none of the solutions fit my situation. I've got a nav that needs to be centered. There is a sub nav in a UL that needs to be left-aligned to its parent LI and laid out horizontally. When I float the sub nav LI's left, the UL keeps the width of its parent LI.
I'd set a fixed PX but the sub nav needs to be expandable so that the client can add or remove nav items and keep the layout.
Here is my HTML:
<nav>
<ul>
<li><a href="../who_we_are/who_we_are.html">Who We Are</a>
<ul>
<li><a href="../who_we_are/our_approach.html">Our Approach</a></li>
<li><a href="../who_we_are/what_we_do/what_we_do.html">What We Do</a></li>
<li><a href="../who_we_are/leadership.html">Leadership</a></li>
</ul>
</li>
<li><a href="../our_results/our_results.html">Our Results</a>
<ul>
<li><a href="../our_results/case_study_a.html">Case Study A</a></li>
<li><a href="../our_results/case_study_b.html">Case Study B</a></li>
</ul>
</li>
<li><a href="../our_experience/our_experience.html">Our Experience</a>
<ul>
<li><a href="#">Category Experience</a></li>
</ul>
</li>
<li class="current"><a href="what_we_think.html">What We Think</a>
<ul>
<li><a href="#">Articles</a></li>
<li><a href="#">Category Insight</a></li>
<li><a href="#">Research</a></li>
<li><a href="#">Training</a></li>
</ul>
</li>
<li><a href="#">News</a>
<ul>
<li><a href="#">Press Releases</a></li>
<li><a href="#">Articles</a></li>
</ul>
</li>
<li><a href="#">Connect With Us</a>
<ul>
<li><a href="#">Join Us</a></li>
<li><a href="../connect_with_us/find_us.html">Find Us</a></li>
</ul>
</li>
<li><div class="search"><input name="searchbox" type="text" id="searchbox" class="input_style" value="SEARCH" onFocus="if (this.value == 'SEARCH') {this.value = '';}" onBlur="if (this.value == '') {this.value = 'SEARCH';}"><button></button></div></li>
</ul>
</nav>
Here is my CSS:
nav a,
nav a:visited {
color: #888888;
text-decoration: none;
display: inline-block;
line-height: 30px;
}
nav a:hover {
color: #CCC;
}
nav a:active {
color: #FFF;
}
nav {
text-align: center;
}
nav ul {
display: inline-block;
position: relative;
z-index: 597;
}
nav ul li {
float: left;
padding-left: 15px;
padding-right: 15px;
position: relative;
z-index: 599;
line-height: 30px;
}
nav ul li ul {
display: none;
}
nav li.current a {
background: url(../img/nav_arrow.png) no-repeat center bottom;
color: #FFF;
}
nav li.current ul {
display: block;
position: absolute;
z-index: 598;
top: 31px;
left: 1px;
width: auto;
}
nav li.current ul a {
background: url(none);
color: #888888;
}
nav li.current ul li {
float: left;
display: inline-block;
}
nav .search {
height: 18px;
width: 84px;
padding-left: 4px;
background: url(../img/search_bk.png) no-repeat;
margin-left: 20px;
margin-top: 5px;
float: left;
}
nav .search input {
height: 14px;
width: 68px;
float: left;
background-color: transparent !important;
border-style: none;
}
nav .search input.input_style {
font-family: "proxima-nova", sans-serif;
padding-top: 3px;
color: #777777;
}
nav .search button {
height: 11px;
width: 10px;
float: left;
background-image: url(../img/search_btn.png);
background-color: transparent;
border-style: none;
background-repeat: no-repeat;
margin-top: 3px;
}
Here is a screenshot:
My Screenshot
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将子菜单 UL 的宽度设置为可笑的,例如 width:2000px 而不是 width:auto;这样,项目就可以在子导航 ul 中水平浮动。
通常,网站菜单不会超过页面区域的宽度,那么为什么子菜单 UL 的宽度过大会很重要呢?它只是为了容纳你的 li 元素,它会漂浮在任何内容之上。
我用这个: http://matthewjamestaylor.com/blog/centered-dropdown-menus
所以我的子菜单 ul 看起来像;
Just make the width of the sub-menu UL something ridiculous like width:2000px instead of width:auto; this way the items can float horizontally in the sub-nav ul.
Commonly a website menu isn't going to exceed the width of the page area so why does it matter if the sub-menu UL has a excessively large width? It's only meant to house your li elements which will float above any content.
I use this: http://matthewjamestaylor.com/blog/centered-dropdown-menus
So my sub menu ul looks like;