×

How to resize an image in CSS

How to resize an image in CSS?

The resize image attribute is used in responsive web design to resize images to fit within the div container automatically. The resize image attribute is created using the CSS max-width property. If the width and height of the picture are defined in HTML, the resize Property will not operate. One of the most important aspects of responsive web design is dynamically resizing images to match the width of their containers. Because individuals increasingly use a variety of desktop computers, the web pages must be compatible with all of them. To prevent huge images from exceeding the width of their container, use max-width: 100 percent; height: auto.

The ways for resizing an image in CSS to fit it appropriately in a div while keeping its height and width are as follows: -

1) Resizing an image with CSS, use the max-width and max-height properties.

When adding an image into HTML, it takes up all the available pixels. When an image is contained within a container, the picture's size may be larger than the container's size. The image will take up more screen real estate, taking areas from other items. As a result, the content will not adhere to our style and will be unappealing. To solve this issue, we can automatically utilize the max-width and max-height CSS values to resize the picture to fit the container. These parameters determine an element's maximum height and width. If the element's content has a width and height greater than the max-width and max-height values, the element's sizes will be reduced.

Example: -

<!DOCTYPE html>
<html>
<head>
<style>
img {
 max-width: 100%;
 max-height: 100%;
}
.cat_pic {
 height: 350px;
 width: 350px;
}
</style>
</head>
<body>
<div class="cat_pic">
 <img src="https://loremflickr.com/320/240?lock=212" />
</div>
</body>
</html>

Output: -

How to resize an image in CSS

We've added an arbitrary image from LoremFlickr that's 320px wide and 240px tall in the sample below. However, the container cat pic has a 350px height and width. The larger photos compress to the size of the container because we are using the max-height and max-width parameters. As a result, the image was auto-resized.

2) To resize an image in CSS, use the object-fit Property.

To adjust the image to fit its container, one can even use the object-fit property in CSS. The image may be smaller or larger than the container. The attribute allows us to scale the picture or video to fit the container's dimensions. Using the object-fit attribute, we can determine how the image fits. The property accepts the fill, contain, cover, none, and scale-down values. We can utilize the content value to make the larger image fit into the container's dimensions.

Example: -

<!DOCTYPE html>
<html>
<head>
<style>
img {
 height: 100%;
 width: 100%;
 object-fit: contain;
}
.cat_pic {
 height:350px;
 width: 350px;
}
</style>
</head>
<body>
<div class="cat_pic">
    <img src="https://loremflickr.com/300/240/dog">
</div>
</body>
</html>

Output: -

How to resize an image in CSS

Here, we used a larger picture than the container's dimensions. The image is 600 pixels wide, but the container is only 300 pixels wide. We can compress the image to the container's dimensions using the object-fit attribute. As a result, we can resize the image automatically.

3)To resize an image with CSS, use the auto-width and max-height properties.

To determine the picture's width to fit in a container, we can use the auto value for the width and set the max-height property. The image's height will be reduced to match the container's height. Insert a picture like the one above in HTML and set the container's height to 200px in CSS. Choose the img tag, then change the width to auto and the max-height value to 100%.

Example: -

<!DOCTYPE html>
<html>
<head>
<style>
.cat_pic {
 height:250px;
}
img {
 width: auto; 
 max-height: 100%; 
}
</style>
</head>
<body>
<div class="cat_pic">
    <img src="https://loremflickr.com/300/240/dog">
</div>
</body>
</html>

Output: -

How to resize an image in CSS

The image in the example below was created with a height and width of 300 pixels. The container's height has been set to 200px. The image is shrunk to 240px when the max-height setting is set to 100 percent. As a result, we scaled the image's height to match the container's height.

Conclusion: -

In this tutorial, we have learned how to resize the image using suitable Properties in CSS.


Related Topics

CSS Introduction

CSS stands for Cascading Style Sheet. It is a design language which is used to design the web pages. It was developed by Håkon Wium Lie on October 10, 1994. It provides...

1 minute read.

CSS Masking

CSS Masking The CSS mask property is applied to hide a component with the use of the masking or clipping an image at any specific point. The masking depicts the use...

3 minutes read.

What is a CSS File?

What is a CSS File and How to use it in Html? CSS: - CSS stands for Cascading Style Sheet. CSS is a file of style sheets that describe the presentation of...

4 minutes read.

CSS Counters

CSS Counters: The CSS counter property is similar to the variables. CSS counters are controlled via CSS, and its values are incremented via rules of CSS to record how much...

4 minutes read.

CSS Text-Overflow

Introduction: The text-overflow property of CSS is used to format the text which is overflowing the boundaries of the container. This property helps user to clip the text, to display the...

5 minutes read.

CSS Page-break-inside Attribute

Page-break-inside Attribute As its name implies, the page-break-before property is applied to include page break inside any element, at the time of printing any document. It adds the page break before...

3 minutes read.

How to include CSS

How to include CSS CSS is included in HTML pages for formatting the documents based on the information inside a style sheet. We can add CSS in HTML documents in the following three ways: Inline...

5 minutes read.

Combine Background Image with Gradient Overlay

With the help of CSS gradient, we can display smooth and straightforward transitions between two or more colours. We can add the background images by combining the gradient property with...

2 minutes read.

CSS Lists

CSS Lists: Several properties are available in CSS that are used to manage the lists. Lists are distributed as ordered and unordered kind of lists. The ordered lists provide the...

7 minutes read.

How to resize an image in CSS

How to resize an image in CSS? The resize image attribute is used in responsive web design to resize images to fit within the div container automatically. The resize image attribute...

3 minutes read.

CSS Button Style

CSS button style: Using CSS properties, we can easily modify the default BUTTON and enhance its appearances using simple graphic commands. TYPES OF BUTTON STYLE: - Program to change button using CSS ...

35 minutes read.

CSS Pagination

CSS Pagination The pagination in CSS is an advantageous approach for indexing of a website’s distinct pages over the homepage. When our site contains multiple pages, we must insert a few...

13 minutes read.

CSS Display

CSS Display: The CSS Display property is one of an essential property among all other properties of CSS. The display property specifies how the HTML elements should be displayed on...

4 minutes read.

CSS Variables

CSS Variables These variables of CSS are applied to add the custom property values to our various web-pages. The custom properties are often called as CSS variables or cascading variables. Various...

4 minutes read.

CSS z-index Property

CSS z-index: CSS z-index permits us to represent any visual hierarchy over a 3D plane that is the z-axis. This index can be applied to describe the positioned element’s stacking...

2 minutes read.

How to change background color in CSS

How to change background color in CSS? The background-color property in CSS would be used to specify the color of an element's background. As the element's background, it uses solid colors. The...

3 minutes read.

CSS Margin

CSS Margin: The CSS margin property is employed to describe the space throughout the elements. It clears the range throughout the element. These properties do not have a background color...

2 minutes read.

CSS Descendant Selector

Descendant Selector The Descendant selector in CSS is used to match the descendant components of a particular component. The term descendant depicts nested in whatever place, inside a DOM tree. This...

2 minutes read.

CSS Font

CSS Font: CSS has font property, which makes the text more attractive. We can alter the entire look by changing font color, font size, font style, and much more. In...

5 minutes read.

CSS Visibility

Visibility in CSS: This visibility property in CSS is employed to describe whether the components are visible or not. Note: An invisible component also grabs the space over the page. If...

2 minutes read.