Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Crop images keeping aspect ratio

Tags:

html

css

I have a list of images with text wrapped around but images I receive come in different sizes (all larger than 150px). I need to crop them all to 150x100px but keep correct aspect ratio. Could anyone please suggest the best way of solving this problem? Thanks!

HTML:

<ul>        
    <div class="post" id="post">
    <div>
        <li>
        <img class="TextWrap" src="{{ picture }}">
        <a href="{{ link }}">{{ message }}</a><p>
        {{ time }}
        </li>   
    </div>
</ul> 

CSS:

.post {
    width: 500px;
    margin-top: 15px;
    float: left;
}

.post img {
    width: 150px;
    height: 100px;
    margin-bottom: 15px;
    margin-right: 20px;
}

.TextWrap {
    float: left;
}
like image 680
aviss Avatar asked Jan 05 '17 17:01

aviss


People also ask

Does cropping change the aspect ratio?

Cropping is the process of removing a portion of an image in order to improve framing, emphasize a subject, or change the aspect ratio. This type of photo editing allows you to keep all of the elements you want while removing those you don't.

How do I lock the aspect ratio of an image?

Simply left-click on the image to select it. Then set “ Lock aspect ratio” to true in the Properties window. You can also preserve the aspect ratio by pressing the Ctrl-key while resizing a shape. Let's now have a look at an example to get a better understanding.

How do I crop without losing resolution?

One way to do this is to use a program like Photoshop. With Photoshop, you can resize an image without losing quality by using the "Image Size" dialog box. In the "Image Size" dialog box, you can change the width and height of the image. You can also change the resolution.


2 Answers

As long as you do not want server side cropping, you can add following css:

.post img {
    object-fit: cover;
}

Please check more possible values for object-fit

like image 70
Codemole Avatar answered Nov 04 '22 08:11

Codemole


As an alternative to Ray's answer, you can set the images as backgrounds and use:

background-size: cover;
like image 30
Serg Chernata Avatar answered Nov 04 '22 06:11

Serg Chernata