Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set image max size

I need to set maximum height and width of a image in <img> tag. For example say max size is 400x400 px so if the image size is smaller than this then it will show the image as it is and if size is bigger then this then it should be compressed to this size. How can I do this in html or javascript?

like image 753
Harry Joy Avatar asked Jul 27 '11 12:07

Harry Joy


People also ask

How do I change the maximum width of an image?

The max-width property sets a maximum width for an element, which does not allow the width of that element to be larger than its max-width value (but it can be smaller). Therefore, you can define a max-width property for the image and set it to 100%, which shrinks the image of 500px to the space of 360px.

How do I make an image a certain size in CSS?

Sometimes, it is required to fit an image into a certain given dimension. We can resize the image by specifying the width and height of an image. A common solution is to use the max-width: 100%; and height: auto; so that large images do not exceed the width of their container.

How do I change the code size of a picture?

There is no command for changing an image size. Image dimensions are 'properties' which can be expressed in either the HTML <img> element, as width="150" height="100" attributes in the tag; or, the CSS as style rules applying to specific images.

How do I put an image size in HTML?

Step 1: Firstly, we have to type the Html code in any text editor or open the existing Html file in the text editor in which we want to change the size of an image. Step 2: Now, place the cursor inside the img tag. And then, we have to use the height and width attribute of the img tag for changing the size of an image.


4 Answers

You can also use object-fit: scale-down to make image fit a container size if it's too big and leave image size as is if it's smaller than the container.

CSS:

.img-scale-down {
    width: 100%;
    height: 100%;
    object-fit: scale-down;
    overflow: hidden;
}

HTML:

<div style="width: 400px;">
   <img src="myimage.jpg" class="img-scale-down"/>
</div>
like image 119
Michał Stochmal Avatar answered Oct 13 '22 23:10

Michał Stochmal


The way I did it:

  • When the image is being uploaded I use a server side library to resize if the image is bigger (only). You always resize down, never up.
  • Then on the client side I do not set the image size.

The image will be 400x400 only for those images that were exactly that size or bigger.

like image 45
Internet Engineer Avatar answered Oct 13 '22 21:10

Internet Engineer


try using a div tag of that size and putting image inside:

<div style="width:400px; height400px;>

  <img style="text-align:center; vertical-align:middle; max-width:400px;  max-height:400px;" />

</div>

or something like that. The div is the full size and the image can only expand to its borders.

like image 33
joncodo Avatar answered Oct 13 '22 23:10

joncodo


Set the max-width and max-height in CSS

max-width: 400px;
max-height: 400px;
like image 20
tskuzzy Avatar answered Oct 13 '22 22:10

tskuzzy