I want to show an image from an URL with a certain width and height even if it has a different size ratio. So I want to resize (maintaining the ratio) and then cut the image to the size I want.
I can resize with html img
property and I can cut with background-image
.
How can I do both?
Example:
This image:
Has the size 800x600
pixels and I want to show like an image of 200x100
pixels
With img
I can resize the image 200x150px
:
<img
style="width: 200px; height: 150px;"
src="http://img1.jurko.net/wall/paper/donald_duck_4.jpg">
That gives me this:
<img style="width: 200px; height: 150px;" src="http://img1.jurko.net/wall/paper/donald_duck_4.jpg">
And with background-image
I can cut the image 200x100
pixels.
<div
style="background-image:
url('http://img1.jurko.net/wall/paper/donald_duck_4.jpg');
width:200px;
height:100px;
background-position:center;"> </div>
Gives me:
<div style="background-image:url('http://img1.jurko.net/wall/paper/donald_duck_4.jpg'); width:200px; height:100px; background-position:center;"> </div>
How can I do both?
Resize the image and then cut it the size I want?
In the crop class, place the image size that you want to appear:
The html will look like:
Live Example: https://jsfiddle.net/de4Lt57z/
HTML:
CSS:
Explanation: Here image is resized by width and height value of the image. And crop is done by clip property.
For details about clip property follow: http://tympanus.net/codrops/2013/01/16/understanding-the-css-clip-property/
What I've done is to create a server side script that will resize and crop a picture on the server end so it'll send less data across the interweb.
It's fairly trivial, but if anyone is interested, I can dig up and post the code (asp.net)
You can put the img tag in a div tag and do both, but I would recommend against scaling images in the browser. It does a lousy job most of the time because browsers have very simplistic scaling algorithms. Better to do your scaling in Photoshop or ImageMagick first, then serve it up to the client nice and pretty.
If you are using Bootstrap, try using
{ background-size: cover; }
for the<div>
maybe give the div a class say<div class="example" style=url('../your_image.jpeg');>
so it becomesdiv.example{ background-size: cover}