[英]How do I customize dropdown menu in CSS?
http://jsfiddle.net/zcfqu/ http://jsfiddle.net/zcfqu/
Been playing around with this piece of code for a while and am confused a bit. 在这段代码上玩了一段时间,感到有些困惑。
How do I: 我如何:
HTML 的HTML
<ul id="menu">
<li><a href="#">This is the button</a>
<ul class="submenu">
<li><a href="#">Button one</a>
</li>
<li><a href="#">Button two</a>
</li>
<li><a href="#">Button three</a>
</li>
</ul>
</li>
</ul>
ul.submenu a:hover {
background-color: red !important;
}
This changes on hover. 悬停时会发生变化。 If you want it always the same color remove
:hover
如果您希望它总是相同的颜色,请删除
:hover
ul.submenu, ul.submenu>li {
width: 100%;
}
This way you don't need to apply a fixed width
. 这样,您无需应用固定
width
。 The browser will calculate it using parents adapted width
. 浏览器将使用父母调整的
width
来计算它。
Here is the correct approach in tackling your issues 这是解决您的问题的正确方法
DEMO http://jsfiddle.net/kevinPHPkevin/zcfqu/37/ 演示http://jsfiddle.net/kevinPHPkevin/zcfqu/37/
// be more specific when targeting
ul#menu ul.submenu li a:hover {
background-color: green;
}
// set width to match button size
ul.submenu, ul.submenu>li {
width: 100%;
}
// assign classes for different coloured buttons. You could do this with css3 and `nth child` but it would limit your browser support considerably.
ul#menu .submenu li.btn1 a {
background: red;
}
ul#menu .submenu li.btn2 a {
background: yellow;
}
ul#menu .submenu li.btn3 a {
background: blue;
}
I just removed all floats
(which was causing funny jumping of li and really not needed) and position:absolute
(which was causing menu to shift sideways) 我刚刚删除了所有
floats
(这会引起li的有趣跳跃,但实际上并不需要)和position:absolute
(这会导致菜单向侧面移动)
Also, I didn't read through all your CSS to find which background
property is overriding which one, but I just removed them all and added new ones at bottom. 另外,我没有通读所有CSS来查找哪个
background
属性覆盖了哪个background
属性,但是我只是删除了所有background
属性并在底部添加了新属性。
#menu > li { background-color: red; }
#menu > li:hover { background-color: green; }
.submenu li { background-color: blue; }
.submenu li:hover { background-color: yellow; }
EDIT 1 编辑1
Its a good idea to use CSS shorthands and reduce CSS size and also make it more readable. 使用CSS速记并减小CSS大小并使其更具可读性是一个好主意。 Also, remove all incorrect CSS and you can also write
border-radius: 2px 2px 2px 2px
as border-radius: 2px
(and save 12 bytes :O
) 另外,删除所有不正确的CSS,还可以将
border-radius: 2px 2px 2px 2px
用作border-radius: 2px
(并保存12个字节:O
)
EDIT 2 编辑2
CSS shorthands - MDN CSS简写-MDN
font
shorthand - W3C font
速记-W3C
background
shorthand - W3C (scroll to the very bottomo of the page) background
速记 -W3C(滚动到页面的底部)
You can try the css as below with no changes on the html elements. 您可以按以下方式尝试使用CSS,而无需更改html元素。 I have added some comments for your references.
我添加了一些评论供您参考。 Only 3 changes made on the css.
在CSS上仅进行了3次更改。
/*Initialize*/
ul#menu, ul#menu ul.sub-menu {
font-family: Helvetica;
background-color: #57AD68;
color: #FFFFFF;
border-radius: 3px 3px 3px 3px;
font-size: 12px;
font-style: normal;
font-weight: 400;
height: 40px;
line-height: 39px;
padding: 0 20px;
position: relative;
text-align: center;
vertical-align: bottom;
border-radius: 3px 3px 3px 3px;
border-style: none none solid;
border-width: 0 0 1px;
cursor: pointer;
display: inline-block;
float: center;
list-style-type: none;
text-decoration: none;
}
ul#menu, ul.submenu{
margin: 0;
padding: 0;
list-style: none;
float: left;
width: 134px; /*Adjust the sub menu width*/
}
ul#menu li{
float: left;
}
/* hide the submenu */
li ul.submenu {
display: none;
}
/* Main Button */
ul#menu li a{
display: block;
text-decoration: none;
color: #ffffff;
padding: 0 20px;
background: ; /*Remove the color here to avoid overlapped*/
float:right;
border-radius: 2px 2px 2px 2px;
font-family: Helvetica;
}
ul.submenu a:hover {
background: red;
}
/* show the submenu */
ul#menu li:hover ul.submenu{
display: block;
position: absolute;
float:right;
background-color:green; /*Adjust the color of sub menu.*/
}
ul#menu li:hover li, ul#menu li:hover a {
float: none;
background: ;
}
ul#menu li:hover li a:hover {
opacity:0.9;
}
I've set each li as 150px width. 我将每个li设置为150px宽度。 This has fixed the issue.
这已解决了该问题。 http://jsfiddle.net/andaywells/zcfqu/34/
http://jsfiddle.net/andaywells/zcfqu/34/
ul#menu ul.submenu li {width: 150px;}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.