如何让 div 高度自动调整到背景大小? [英] How to get div height to auto-adjust to background size?
本文介绍了如何让 div 高度自动调整到背景大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何让 div 自动调整到我为它设置的背景的大小而不为其设置特定的高度(或最小高度)?
解决方案
另一种可能效率低下的解决方案是将图像包含在设置为 visibility: hidden;
img
的元素下/代码>.然后让周围div的background-image
和图片一样.
这会将周围的 div 设置为 img
元素中图像的大小,但将其显示为背景.
<div style="background-image: url(http://your-image.jpg);"><img src="http://your-image.jpg" style="visibility: hidden;"/>
How do I get a div to automatically adjust to the size of the background I set for it without setting a specific height (or min-height) for it?
解决方案
Another, perhaps inefficient, solution would be to include the image under an img
element set to visibility: hidden;
. Then make the background-image
of the surrounding div the same as the image.
This will set the surrounding div to the size of the image in the img
element but display it as a background.
<div style="background-image: url(http://your-image.jpg);">
<img src="http://your-image.jpg" style="visibility: hidden;" />
</div>
这篇关于如何让 div 高度自动调整到背景大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文