All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Resize Image In CSS

Last Updated : Mar 11, 2024

How To Resize Image In CSS

In this tutorial we will show you the solution of how to resize image in CSS, it is often necessary to fit an image within a specific dimension. Images must be exactly aligned with the layout while creating web pages.

It's equally as crucial to ensuring that your visuals maintain their best quality and correct aspect ratio across all screen sizes. Your images may appear distorted or cut off if you don't have them. This guide will show you how to resize an image with CSS.

Step By Step Guide On How To Resize Image In CSS :-

By defining the width and height of an image, we can resize it. To ensure that huge pictures do not exceed the width of their container, a typical method is to utilise the max-width: 100%; and height: auto; attributes.

In the following code, we'll show how to resize an image in CSS in a variety of ways:

<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
.resize {
width:auto;
text-align:center;
padding:25px;
}
Retention of Aspect Ratio
img {
max-width:100%;
height:auto;
}
/*
Absolute Resizing
img{
  height: 120px;
  width: 210px;
}
*/
/*
Relative Sizing
img{
  height: 100%;
  width: 100%;
  object-fit: contain;
}
*/
</style>
</head>
<body>
<div class = "resize">
<p id="image"><img src="www.abc.jpg">
</p>
</div>
</body>
</html>
  1. We begin by typing <! DOCTYPE html>, which informs the web browser of the file's HTML version.
  2. On the other hand, the <html> element is used to indicate the beginning of HTML content.
  3. The information about web pages is now contained in the <head> tag. This tag uses the <title> element to allow us to provide a web page title. The < head> and <title> tags are examples of paired tags. As a result, both have the closing tags </head> and </title>.
  4. Finally, the <body> element is used to specify the webpage's content. This is where all of the website's material will be written.
  5. In the head tag, a <style> tag was used. In the Retention of Aspect Ratio step, we have adjusted the height as auto to enable automatic sizing to keep the aspect ratio.
  6. In Absolute Resizing you can specify the height and width values and the static ones in pixels (px), ems (em), or any absolute-size format with absolute resizing. In the above example, we have defined the size in px.
  7. Sizing pictures relative to the parent <div> or the size of the client viewport is known as relative sizing. The viewport is the parent container when that attribute is applied to an image as the sole element.
  8. The image's original dimensions are preserved when object-fit: contain is used. CSS then adjusts the image size till the complete image is viewable. In the above example relative sizing, we have mentioned it and given the function object-fit: contain.
  9. Note that above we have commented on the Absolute Resizing and Relative Sizing.
  10. The</body> and </html> tags, respectively, are used to close the <body> and <html> tags.

Conclusion :-

CSS is the foundation of web pages and is used to style websites and web applications.

With proper resizing and scaling achieved through CSS you can ensure that your images look great on all devices and browsers. I hope this tutorial on how to resize image in CSS helps you.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪