简体   繁体   中英

hover a whole li rather than the text

I'm trying to have a link upon hovering, it'll go around the whole li, rather than just the text itself, if you know what I mean.

Apologies in advance if my CSS is in complete disarray.

CSS:

#navi {
    font-size:14px;
    text-align:left;
    text-transform:uppercase;

}

#navi ul {
    padding: 5px;
    margin: 0;
}

#navi li {
    position:relative;
    display:inline-block;   
}

#navi a {
    padding:.03em 1em;
    text-decoration: none;
}

#navi a:hover { 
    background-color: #e0e0e0;
    height: 50px;
}

Index:

<div id="navi">
<ul class="ul">
    <li><a href="">Test 1</a></li>
    <li><a href="">Test 1</a></li>
    <li><a href="">Test 1</a></li>                      
</ul>
</div>

What it looks like:

在此输入图像描述

this is what it looks like when hovered over:

在此输入图像描述

any suggestions? I just want it to go around the whole box. Thanks!

Make the links a block and then add as much padding as needed. For better looks I'd remove the padding from <ul>

#navi a {
  display: block;
  padding:.03em 1em;
  text-decoration: none;
}

Example: http://jsfiddle.net/6Cz7X/ - has some more changes to your CSS:

#navi {
  font-size:14px;
  text-transform:uppercase;
}

#navi ul {
  margin: 0;
  list-style-type: none;
  padding: 0;
}

#navi li {
  float:left; 
}

#navi a {
  display: block;
  padding: 1em 1em;
  text-decoration: none;
}

#navi a:hover { 
  background-color: #e0e0e0;
}

Or add a line-height

you can set <a> as display:block, and apply your vertical padding there: http://codepen.io/anon/pen/nmqKr

#navi {
  font-size:14px;
  text-align:left;
  text-transform:uppercase;
  background:blue;
border-bottom:solid turquoise 5px;
}

#navi ul {
  padding:0 5px;/* send vertical padding into a tag */
  margin: 0;
}

#navi li {
  display:inline-block;   
}

#navi a {
  padding:5px 1em;/* apply here the vertical-padding */
  text-decoration: none;
  display:block;/* take all width and have no gaps at bottom */
  height:100%;
  color:white;
}

#navi a:hover { 
  background-color: #e0e0e0;
  color:black;
}

Instead of an ordinary link you could use a div. You can then change its background-color an hovering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM