如何让 div 高度自动调整为背景大小? [英] How to get div height to auto-adjust to background size?

查看:30
本文介绍了如何让 div 高度自动调整为背景大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆