Reputation: 5417
I made a simple CSS drop-down menu but am facing some issues with padding. When I hover on an element, I want the new background color to nicely fill the whole elements, but it's just limited to the text right now. Could someone please have a look and tell me what's wrong: http://jsfiddle.net/2pdxx4fq/
HTML:
<ul id="navbar">
<li><a href="#">Attendance</a>
<ul>
<li><a href="#">Upload</a></li>
<li><a href="#">Report</a></li>
</ul>
</li>
<li><a href="#">Leave</a>
<ul>
<li><a href="#">Report</a></li>
<li><a href="#">Holidays</a></li>
</ul>
</li>
<li><a href="#">Profile</a></li>
<li><a href="#">Log out</a></li>
</ul>
CSS:
/* http://meyerweb.com/eric/tools/css/reset/
* v2.0 | 20110126
* License: none (public domain)
*/
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;
}
/* HTML5 display-role reset for older browsers */
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;
}
/* ========= End of CSS Reset ============ */
#navbar {
margin: 20px 0px;
background-color: #444;
}
#navbar a {
text-decoration: none;
color: #fff;
}
#navbar a:hover {
background-color: #AAA;
color: #000;
}
#navbar li {
display: inline-block;
position: relative;
padding: 10px;
}
#navbar li ul {
background-color: #444;
position: absolute;
display: none;
}
#navbar li:hover > ul {
display: block;
}
Upvotes: 1
Views: 252
Reputation: 7139
Remove padding from li
and add it to a
along with display:block
(also on the anchor).
Upvotes: 0
Reputation: 22998
Apply the hover on li
.
/* http://meyerweb.com/eric/tools/css/reset/
* v2.0 | 20110126
* License: none (public domain)
*/
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;
}
/* HTML5 display-role reset for older browsers */
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;
}
/* ========= End of CSS Reset ============ */
#navbar {
margin: 20px 0px;
background-color: #444;
}
#navbar a {
text-decoration: none;
color: #fff;
}
#navbar li:hover {
background-color: #AAA;
color: #000;
}
#navbar li ul {
margin-top: 10px;
margin-left: -10px;
}
#navbar li {
display: inline-block;
position: relative;
padding: 10px;
width: 70px;
text-align: center;
}
#navbar li ul {
background-color: #444;
position: absolute;
display: none;
}
#navbar li:hover > ul {
display: block;
}
<ul id="navbar">
<li><a href="#">Attendance</a>
<ul>
<li><a href="#">Upload</a>
</li>
<li><a href="#">Report</a>
</li>
</ul>
</li>
<li><a href="#">Leave</a>
<ul>
<li><a href="#">Report</a>
</li>
<li><a href="#">Holidays</a>
</li>
</ul>
</li>
<li><a href="#">Profile</a>
</li>
<li><a href="#">Log out</a>
</li>
</ul>
Upvotes: 1
Reputation: 1661
Change this:
#navbar a:hover {
To this:
#navbar li:hover {
That way will be the whole li
element what will change on hover, not just the anchor a
.
Upvotes: 1