html - CSS scale down image to fit in containing div, without specifing original size -
i want have website can upload images of different sizes displayed in jquery slider. can't seem fit (scaling down) image in containing div. here's how i'm intending it
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org /tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>untitled document</title> <link rel="stylesheet" type="text/css" href="imtest.css"/> </head> <body> <div id="wrapper"> <div id="im"><img src="images/scarpa2_1.jpg" /></div> </div> </body> </html>
and css
#wrapper { width: 400px; height: 400px; border: 2px black solid; margin: auto; } #im { max-width: 100%; }
i've tried set max-width of image 100% in css. doens't work.
you can use background image accomplish this;
from mdn - background size: contain:
this keyword specifies background image should scaled large possible while ensuring both dimensions less or equal corresponding dimensions of background positioning area.
css:
#im { position: absolute; top: 0; left: 0; right: 0; bottom: 0; background-image: url("path/to/img"); background-repeat: no-repeat; background-size: contain; }
html:
<div id="wrapper"> <div id="im"> </div> </div>
Comments
Post a Comment