简体   繁体   English

使背景图像适合动态div

[英]fit background image in to a dynamic div

i have a background image (700x500px) and i want to fill wit it a dynamic div. 我有一个背景图片(700x500px),我想用它填充一个动态div。 in this example the div is only 20px but it can have a height over 500px. 在此示例中,div仅为20px,但高度可以超过500px。

<div style="background-image:url('background.jpg');width:710px">
some content some content
</div>

Using standard CSS you can't just fit a background image. 使用标准CSS,您不能只适合背景图片。 But you can use an image tag in a div and make it resize. 但是您可以在div中使用图片标签并调整其大小。 And then use your content in front of it, so the image will be used as a background. 然后在其前面使用您的内容,因此该图像将用作背景。

You can find more details, css usage and an example page here : 您可以在此处找到更多详细信息,css用法和示例页面:
http://www.quackit.com/html/codes/html_stretch_background_image.cfm http://www.quackit.com/html/codes/html_stretch_background_image.cfm

You can just add background-size: cover; 您可以只添加background-size: cover; to your style attribute 您的样式属性

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

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