Background image does not show in <a> tag

柔情痞子 提交于 2021-02-05 12:15:20

问题


I use bootstrap 3. I try to use "icon link" by using tag <a> as shown below:

HTML:
<a href="#" class="link"></a>

CSS:
.link {
    background-image: url(img/icon.png);
}

It is important to say, that my stylesheet is in "main folder", that is in folder, where is a img folder with icon.png file. So it seems wrong url is not the case.

I can't figure out why image is not showing.


回答1:


The anchor element has no content, and it has no styles that would affect it's dimensions, consequently it has an effective area of zero square pixels.

The background image is probably being applied just fine, you can't see it because there is no area on which it can be displayed.

The code implies that the image is there to tell the visitor where the link goes, that would mean that the image is content and not background and should be expressed as an image element (which would take on the dimensions of the image file automatically).

Using an image element also provides you with the opportunity to supply alt text for the benefit of screen readers / search engines / people with internet connections that briefly fell over while loading the image / etc.

<a href="#"><img src="img/icon.png" alt="top of page"></a>



回答2:


Because your <a href="#" class="link"></a> is empty.

You need to give it a size :

.link {
    background-image: url(img/icon.png);
    height:100px;
    width:100px;
    display:block;
}



回答3:


You have to make the tag enought big to show the image Example: CSS:

.link {
  background-image: url(img/icon.png);
  display: block;
  width: 100px;
  height: 100px;
}


来源:https://stackoverflow.com/questions/33762453/background-image-does-not-show-in-a-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!