简体   繁体   English

具有背景图像且没有内容不显示的 Div

[英]Div with background image and no content not displaying

I currently have a div on my page that I have given a background image.我目前在我的页面上有一个 div,我给了一个背景图片。 This div does not have any content and so it does not display on my page, if I add any content it displays.此 div 没有任何内容,因此如果我添加它显示的任何内容,它不会显示在我的页面上。 I would like it display even without content because I want that background image to show up as well as I would like to assign some hover/click events to it.即使没有内容,我也希望它显示,因为我希望显示该背景图像,并且我想为其分配一些悬停/单击事件。

Is there any way to force the div to display?有没有办法强制div显示?

You need to specify a size:您需要指定大小:

<div style="width: 100px; height: 100px"></div>

You can set the size to the dimensions of your background image, or any other values you desire.您可以将大小设置为背景图像的尺寸,或您想要的任何其他值。

Add a height and a width to the div.为 div 添加高度和宽度。 You may want to use min-height and min-width and set them equal to the size of your background image.您可能想要使用 min-height 和 min-width 并将它们设置为等于背景图像的大小。

Adding an empty comment will help browsers that strip empty tags.添加空注释将有助于去除空标签的浏览器。

You need to specify width and height of that div using css (or min-width and min-height)您需要使用 css (或 min-width 和 min-height)指定该 div 的宽度和高度

Add height and width to it.为其添加高度和宽度。

Jsfiddle: http://jsfiddle.net/GFTtY/ jsfiddle: http://jsfiddle.net/GFTtY/

Adding an empty //content// worked for me.添加一个空的 //content// 对我有用。 Setting width/height or min-width/min-height didn't force the background image to appear.设置宽度/高度或最小宽度/最小高度不会强制显示背景图像。 (Chrome 51 on Win8) (Win8 上的 Chrome 51)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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