Thursday, 28 October 2021

Re-sizing images automatically to div size

 



To get a responsive image to the div container size max-width property is used. The resize property will not work without the image width and height defined. Width property is also be used instead of max-width but the height value should be auto to achieve the responsive image

img {
max-width: 100%;
height: auto;
}


The above property will work fine to get a responsive image.

The width of 100% takes the full width of the parent element and similarly for the height. The width and height of the image are adjusted to the div container.

Example:

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

<head>
<title>Image resising</title>
<style>
.container {
width: 50%;
height: 500px;
margin: auto;
}

.empty {
width: 50%;
height: 100px;
background-color: blueviolet;
}

img {
width: 100%;
height: auto;
}
</style>
</head>

<body>
<div class="container">
<div class="empty"></div>
<div class="image">
<img src="./CSS.jpeg" alt="CSS">
</div>
</div>
</body>

</html>

Output:



In the above case, the image width will be 50% of the window it is displayed. Because the container size is 50% of the body element. The size of the image is changed according to the window size.

Thank You!!!

Have a good day!!!!

No comments:

Post a Comment

Array Destructuring

  Destructuring  Destructuring is an ES6/ES2015 feature. Destructuring allows us to break down the complex data structure into a simple data...