Scale HTML image width and height in %

My suugestion is to remove the specified sizes you put. Yes there is a code that can be specified in css. in html do like that:

<img src="Debian.jpg" alt="Debian Image" class="myCustomImages">

in css do something like that:

.myCustomImages {
  max-width: 50%;
  max-height: 50%;

If you set the width to a fixed number of pixels, e.g. img { width: 500px; }, the height will adjust accordingly to maintain the same aspect ratio. If you set the height, the width will adjust accordingly to maintain the same aspect ratio.

If you set the width to a percentage, e.g. img { width: 50% }, the browser will assume you mean a percentage of the element's container. The height will adjust accordingly to maintain the same aspect ratio.

However, if you set the height to a percentage, e.g. img { height: 50% }, that just won't work for various reasons.

The solution is really simple. To preserve the aspect ratio, all you need to do is set the height and width CSS properties like:

#theImage {
  width: 100%;
  height: auto;
<img id="theImage" src="">

By setting the width property to 100%, you are telling the image to take up all the horizontal space that is available. With the height property set to auto, your image's height changes proportionally with the width to ensure the aspect ratio is maintained. The end result is an image that scales up or down perfectly.

The downside of this solution is that a single image cannot effectively display across the range of resolutions on which your content may be viewed. For a more comprehensive solution, you need to bring in media queries and images of varying sizes to replace a lower resolution image when the time is right

Yes, there is a way to maintain the image's aspect ratio and resize the image to a fraction of its original size. However, CSS cannot know the intrinsic size (the original size) of the image. Therefore, you can only do two things:

  • Tell the CSS explicitly the original size
  • Use JS to get the original size upon image load

What doesn't work

Using percentage value as the width value for img doesn't work simply because percentage value resolves to, well, a percentage of its container size, not its original size. Below, I demonstrated the not-working examples.

That being said, I personally usually want to specify the width of the image explicitly. For example, on large devices, I want the image to be 1080px. On smaller devices, I want the image to be 560px. I can simply make a container for the image of an explicit size, place the image inside the container, and specify the image's width to 100% (of its container size).

What works

As mentioned, there are two ways to make an image 50% of its original width. First, using JS. Second, tell the CSS explicitly the original image width.

  • Using the JS approach, you simply need to change the width of the image. Upon load, get the intrinsic width of the image, then set the new width to the intrinsic width divided by 2. Simple.
  • Using the telling-CSS-explicitly approach is less advantageous. This solution presumes that the image will always be the same and needs you, the developer, to know the original image size beforehand. When you change the original image size, you will also need to update your code. That being said, you can achieve this by specifying a CSS custom property inside the CSS class, specifying an attribute (in HTML) that gives the intrinsic width then using attr() (still experimental and mostly not supported), or using an intrinsicsize attribute and set the width and style through CSS (still experimental and not supported). The last two solutions, as mentioned, are not supported by most browsers and may not work properly yet.

In my opinion, your best bet to set an image's width to 50% its intrinsic width is by using JS. Here's a solution demonstrating the what-doesn't-work solutions and the JS solution. Aspect ratio is automatically maintained if you only change one of the image's size (width/height).

const imageJS = document.querySelector('.image--changed-with-js')

imageJS.onload = () => {
  const intrinsicWidth = imageJS.width
  imageJS.width = imageJS.width / 2
* {
  box-sizing: border-box;

html {
  margin: 0px;

img {
  margin: 20px 0;

/* Image has its intrinsic size (i.e. the original image size) */
.image--original {
  width: auto;

/* Image contained within a 500px container
 Image has a width of 50% of 500px = 250px */
.container {
  width: 500px;

.image--changed-with-container {
  width: 50%;

/* Image is not contained within a div
However, image is inside body
<body> is its container
It now has a width of 50% of the body
NOT 50% of its intrinsic width */
.image--changed-without-container {
  width: 50%;

/* Image changed using JS
You can get the intrinsic size through JS and modify its size there */
.image--changed-with-js {}
<img class="image--original" src="">

<div class="container">
  <img class="image--changed-with-container" src="">

<img class="image--changed-without-container" src="">

<img class="image--changed-with-js" src="">