2012-06-21 111 views
2

通常情况下,我使用背景图像将图像调整为div,具体取决于宽度和高度属性。现在我有完全相反的问题。将div调整为图像大小

我想要做的是调整div大小的图像,但最大的宽度和高度。假设图像是200x200,div必须调整到这个大小,但如果图像超过300x300,它必须受到限制。

任何想法,我应该怎么做呢?

+0

确认,背景图像或img标签? – Fewfre

+0

使用背景图像 – domoindal

回答

1

您可以将max-widthmax-height样式属性应用于图像元素。

根据您需要包含的元素做什么,这可能工作:

<div> 
    <div style="position: absolute;">Element content here</div> 
    <img src="image.png" style="max-width: 300px; max-height: 300px;" /> 
</div> 
+0

你可以改变“位置:绝对”,让你得到一个居中的图像? – FaCoffee

0

什么是你的页面的结构?

我猜你用这样的:

<div id="container"> 
    <img src="img.jpg" /> 
</div> 

<style> 
    #container { 
     display: inline-block; 
     border:1px solid blue; 
     max-width:300px; 
     max-height:300px; 
    } 
</style> 
+0

问题是我正在使用background-image来放置带div的图像。 – domoindal

+0

http://stackoverflow.com/questions/600743/how-to-get-div-height-to-auto-adjust-to-background-size – meneerfab