문제

I am trying to put a background to a empty anchor tag, but nothing will show up unless i place text there. What is the best approach to having a image in a list item and change it to another image on hover with css.

html

<ul>
    <li><a class="nav1" href="http://localhost:8888/fiftyfity/?page_id=2"></a></li>
</ul>

css

   ul li a.nav1{
    background-image: url("../images/nav1.gif");
    width:161px;
    height: 49px;
}
ul li a.nav1:hover{
    background-image: url("../images/navB1.gif");
    width:100px;
    height: 20px;
}
도움이 되었습니까?

해결책

Anchor links are inline elements by default, so applying a width and height won't work. You can make them block elements with CSS:

ul li a.nav1 {
 display: block;
}

More info: http://www.webdesignfromscratch.com/html-css/css-block-and-inline/

다른 팁

I needed to change the anchor tag into a block level element. It all works now

  ul li a.nav1{
    display:block;
    background-image: url("../images/nav1.gif");
    width:161px;
    height: 49px;
}
ul li a.nav1:hover{
    display:block;
    background-image: url("../images/navB1.gif");
    width:100px;
    height: 20px;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top