Open In App

How To Change Image Size In HTML?

Last Updated : 16 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To change the size of an image in HTML, you can use width and height attribute within <img> tag. Alternatively, we can use CSS properties to set or change the image size. Change image size feature is useful while developing a responsive web page.

Change Image Size using HTML width and height Attributes

The simplest way to resize images in HTML is by using the width and height attributes directly in the <img> tag. This method sets the dimensions of the image in pixels, forcing the browser to resize the image to the specified values.

Syntax

<img src="image" width="300" height="200" alt="Description">

Example: This example will display the image with a fixed width of 300 pixels and a height of 200 pixels. The browser will resize the image to these specific dimensions regardless of the original size of the image.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Image Resizing with HTML Attributes</title>
</head>

<body>
    <h1 style="color: green;">GeeksforGeeks</h1>
    <h2>Fixed Size Image Using HTML Attributes</h2>
    <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240911105255/geekforgeek.png" 
        width="300" height="200" alt="Fixed Size Image">
</body>

</html>

Output: The image will appear at the size of the 300*200pixels, no matter what the original size is.

adjust1
Adjust Image Size Using HTML Attributes

Change Image size using CSS width and height Properties

CSS provides more flexibility to change the image size. CSS can dynamically resize images based on the parent container's size, enabling responsive design. With CSS, you can set fixed dimensions, percentage-based dimensions, or use properties like max-width and min-width for more control.

Syntax

img {
width: 300px; /* Sets the image width */
height: 200px; /* Sets the image height */
}

Example: Set the image width ad height to 300px and 200px respectively.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Responsive Image Using CSS</title>
  
    <style>
        img {
            /* Set the full width of its parent container */
          	width: 100%;
            
          	/* Maintains aspect ratio */
            height: auto;
        }
    </style>
</head>

<body>
    <h1 style="color: green;">GeeksforGeeks</h1>
    <h2>Responsive Image Using CSS</h2>
    <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240911105255/geekforgeek.png" 
         alt="Responsive Image">
</body>

</html>


Output: The output image will adjust its width to fit the parent container and maintaining its original aspect ratio.

Change Image size using object-fit to Control Aspect Ratio

The object-fit CSS property allows to control over how an image fits within a container without distortion or stretching. It is useful for displaying images inside fixed-size containers while maintaining the original aspect ratio. You can use values like cover, contain, fill, and scale-down to specify how the image should be scaled or cropped.

Syntax

img {
width: 100%;
height: 300px;
object-fit: cover; /* Options: contain, fill, cover, scale-down */
}

Example: The image will cover the entire 300px height of the container and the width will adjust to fill the container's width. Some parts of the image may be cropped to achieve this.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Image Resizing with object-fit Property</title>
  
    <style>
        img {
            width: 100%;
            height: 300px;
            object-fit: cover;
        }
    </style>
</head>

<body>
    <h1 style="color: green;">GeeksforGeeks</h1>
    <h2>Image Covering a Container</h2>
    <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240911105255/geekforgeek.png" 
         alt="Covered Image">
</body>

</html>

Output: The output image will completely fil the containers height and width with some potential cropping to the fit aspect ratio.


Next Article
Article Tags :

Similar Reads