CSS 下拉菜单中的定位、块大小和背景
这可能是一件容易的事。我查看了以前的问题(以及网络上的其他地方),但找不到解决我当前问题的好方法。
我正在尝试使用基于列表的 CSS 居中下拉菜单。没什么太复杂的。
这个一个有一个非常简单的解决方案,但我找不到我做错了什么。
我现在有两个问题(主要在第一个列表中,还没有真正查看列表中的链接): (1) 我希望列表的背景是一个大矩形,包含子列表中的所有项目 (2) 子列表中的项目被“截断”,插入新行,使其宽度不超过列表标题的宽度。
谢谢。
CSS 部分
#navbar ul {
text-align: center;
width: 100%;
font-variant: small-caps;
padding: 5px 0;
margin-top: 0;
margin-left: 0;
}
#navbar ul li {
background-color: #ccc;
margin-right: 2%;
color: #069;
text-decoration: none;
position: relative;
display: inline;
padding: 5px 4px;
}
#navbar li a {
text-decoration: none; }
#navbar li ul {
display: none;
}
#navbar li:hover ul, #navbar li.hover ul {
display: block;
position: absolute;
text-align: left;
margin: 8px 0 0 0;
padding: 0;
background-color: #eee; }
#navbar li:hover li, #navbar li.hover li {
padding: 4px 0;
clear: left;
}
#navbar li:hover li a, #navbar li.hover li a {
background-color: #eee;
border-bottom: 1px solid #fff;
color: #000; }
#navbar li li a:hover {
background-color: #333; }
HTML 部分
<div id="navbar">
<ul>
<li>
<a href="#" title="">Link 1</a>
<ul>
<li>item 1.1 and more</li>
<li>item 1.2</li>
<li>item 1.3</li>
<li>item 1.4 truncated?</li>
<li>item 1.5</li>
<li>item 1.6</li>
</ul>
</li>
<li>
<a href="#" title="">Link 2</a>
<ul>
<li><a href="#" title="">Link 2.1</a></li>
<li><a href="#" title="">Link 2.2</a></li>
<li><a href="#" title="">Link 2.3</a></li>
<li><a href="#" title="">Link 2.4</a></li>
<li><a href="#" title="">Link 2.5</a></li>
<li><a href="#" title="">Link 2.6</a></li>
</ul>
</li>
<li>
<a href="#" title="">Link 3</a>
<ul>
<li><a href="#" title="">Link 3.1</a></li>
<li><a href="#" title="">Link 3.2</a></li>
<li><a href="#" title="">Link 3.3</a></li>
<li><a href="#" title="">Link 3.4</a></li>
<li><a href="#" title="">Link 3.5</a></li>
<li><a href="#" title="">Link 3.6</a></li>
</ul>
</li>
</ul>
</div>
This may be an easy one. I looked through previous questions (and other places on the web) but cannot find a good solution for my current problem.
I am trying to have a centered drop down menu with CSS based on a list. Nothing very complicated.
This one has a very simple solution for it, but I cannot find what I am doing wrong.
I have two problems at this point (it's mostly in the first list, have not really looked at the links in the list yet) :
(1) I would like the list coming down to have a background as a large rectangle that encompass all the items in the sub-list
(2) the items in the sub-list are "truncated", a new line is inserted so that the width does not exceed the width of the list title.
Thanks.
The CSS part
#navbar ul {
text-align: center;
width: 100%;
font-variant: small-caps;
padding: 5px 0;
margin-top: 0;
margin-left: 0;
}
#navbar ul li {
background-color: #ccc;
margin-right: 2%;
color: #069;
text-decoration: none;
position: relative;
display: inline;
padding: 5px 4px;
}
#navbar li a {
text-decoration: none; }
#navbar li ul {
display: none;
}
#navbar li:hover ul, #navbar li.hover ul {
display: block;
position: absolute;
text-align: left;
margin: 8px 0 0 0;
padding: 0;
background-color: #eee; }
#navbar li:hover li, #navbar li.hover li {
padding: 4px 0;
clear: left;
}
#navbar li:hover li a, #navbar li.hover li a {
background-color: #eee;
border-bottom: 1px solid #fff;
color: #000; }
#navbar li li a:hover {
background-color: #333; }
The HTML part
<div id="navbar">
<ul>
<li>
<a href="#" title="">Link 1</a>
<ul>
<li>item 1.1 and more</li>
<li>item 1.2</li>
<li>item 1.3</li>
<li>item 1.4 truncated?</li>
<li>item 1.5</li>
<li>item 1.6</li>
</ul>
</li>
<li>
<a href="#" title="">Link 2</a>
<ul>
<li><a href="#" title="">Link 2.1</a></li>
<li><a href="#" title="">Link 2.2</a></li>
<li><a href="#" title="">Link 2.3</a></li>
<li><a href="#" title="">Link 2.4</a></li>
<li><a href="#" title="">Link 2.5</a></li>
<li><a href="#" title="">Link 2.6</a></li>
</ul>
</li>
<li>
<a href="#" title="">Link 3</a>
<ul>
<li><a href="#" title="">Link 3.1</a></li>
<li><a href="#" title="">Link 3.2</a></li>
<li><a href="#" title="">Link 3.3</a></li>
<li><a href="#" title="">Link 3.4</a></li>
<li><a href="#" title="">Link 3.5</a></li>
<li><a href="#" title="">Link 3.6</a></li>
</ul>
</li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)看起来你已经有一个背景矩形了。您只是想要更多填充吗?
如果是这样,请添加填充,如下所示:
#navbar li:hover ul, #navbar li.hover ul {padding:10px}
2) 至于截断,请尝试
#navbar li:hover ul, #navbar li.hover ul {word-wrap: break-word;}
不过,我建议您看一下这篇文章,以帮助您使用下拉菜单:http://matthewjamestaylor.com/blog/centered-dropdown-menus
编辑:不幸的是,自动换行是 CSS3 属性,并且并非所有浏览器都支持。此外,使用跨浏览器 CSS 进行自动换行似乎也并非微不足道。这篇文章 css / js 中的自动换行提供了一些更多信息。
1) It looks like you already have a background rectangle. Do you just want more padding?
If so, add padding like so:
#navbar li:hover ul, #navbar li.hover ul {padding:10px}
2) As for truncating, try
#navbar li:hover ul, #navbar li.hover ul {word-wrap: break-word;}
I would however suggest you look take a look at this article, to help you with drop down menus: http://matthewjamestaylor.com/blog/centered-dropdown-menus
EDIT: Unfortunately, word-wrap is a CSS3 property, and is not supported by all browsers. Additionally, word wrap with cross browser CSS does not appear to be trivial. This post word wrap in css / js has some more information.