×

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

How to use google fonts in CSS

Fonts: - Fonts are used to make our website look more beautiful. Choosing appropriate font according to the web design or layout is also important. Many typefaces are accessible in...

4 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.

CSS Pseudo-class

Pseudo-class The pseudo-class may be depicted as the keyword which can be mixed to any selector that specifies the elected element’s unique state. It can be included to a selector to...

6 minutes read.

CSS Text Effects

Text Effects in CSS We can use distinct text effects used inside the HTML document. Few attributes can be applied to insert the text effects. We can also style various web documents...

5 minutes read.

CSS Outline

CSS Outline: This CSS property is quite similar to the border property of CSS. It eases us to design an additional border around the element to obtain visual attention. The...

4 minutes read.

CSS Align

CSS Center Align Elements CSS center align is used to create align by using block element. You should specify margin: auto;properties. The element will take up the specified width and the remaining space....

2 minutes read.

How to change the font in CSS

How to change the font in CSS? Professionals can't deny typefaces — how users organize and style text — if we want to completely customize the website's design. One may wish...

4 minutes read.

CSS Word Wrap

Word Wrap in CSS: The word-wrap property in CSS is referred to break any lengthy word and wrap towards the next or following line. It is employed for preventing overflow...

1 minute read.

CSS Tutorial

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

9 minutes read.

CSS Font-Size

A Cascading Style Sheet (CSS) is a simple design scripting language intended to simplify the process of making web pages more visually appealing with the kind of high-level formatting. Cascading...

3 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 Browser Support

The desk underneath lists of all CSS properties and the way each belongs to it is supported in the generally unique browsers: - The quantity to the right of the browser...

2 minutes 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.

CSS 3D Transform

Introduction To understand 3D transformation property of transform you need to first understand the difference between 2D and 3D transform. 2D transform is a CSS effect which transforms the elements according to...

6 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.

How to edit CSS in WordPress

Developing a new website or blog is a difficult undertaking. We want to create something that the consumers will adore, but we might not want the setting to take months and...

7 minutes read.

CSS calc()

CSS calc(): The CSS calc() function is a CSS function of inbuilt type, which permits us to implement calculations. It is used for calculating the angle, integer frequency, number, time,...

3 minutes read.

Cascading Style Sheet

CSS refers to the Cascading Style Sheet. CSS is a form of a programming language. In other words, CSS is a language of the style sheet that is utilised for...

7 minutes read.

CSS Radio Button

Radio Button A radio button has been defined as an element of HTML. It can support us to take any input from the user. But it is challenging to design a...

4 minutes read.

CSS Navigation bar

CSS Navigation bar: A navigation system or navigation bar comes upon GUI that can help many visitors to access the information. A Navigation bar is a UI component on the...

11 minutes read.