Setting Up Width of Images In CSS

Ayu is a tech blogger, open source contributor & maintainer, and tech community enthusiast based in the Netherlands. She loves photography and iced coffee!
Search for a command to run...

Ayu is a tech blogger, open source contributor & maintainer, and tech community enthusiast based in the Netherlands. She loves photography and iced coffee!
Thank you, Mohammed! I'm glad to hear that it could help you to learn something new ๐
This series is my log of what I learned about responsiveness in CSS
Hello Fellow Codenewbies ๐ Recently I learned more about flexbox in CSS. I will write this topic in several posts as I learn and dive into it ๐ Flexbox Flexbox is a layout model or displaying items in a single dimension โ as a row or as a colum...
Hi friends ๐, I've been writing tech blog posts for a few years now. If you don't know me yet, English is my second language. My mother tongue is Indonesian, and I'm also surrounded by Dutch every da

I frequently receive messages from enthusiastic contributors who have completed 2 or 3 high-quality pull requests (PRs) in just 1 month. Theyโre proactive, their work is top-tier, and theyโve handled

A few months ago, I shared a story about building an automated open source portfolio using just my smartphone and an AI assistant while on vacation. My main goal was to stop the "spreadsheet struggle"

Recently, I read Bekah Hawrot Weigelโs blog post, "When 'Local' Went Global: The Pandemic Era of International Communities." As someone active in many online communities, I found it a bit sad, but I agree with her points and think they apply to open ...

Hacktoberfest is almost over. How has your contribution journey been so far? Are you confident about contributing to open source projects, or are you still struggling to find your way? If you're new to open source and still feel overwhelmed, that's c...

Hello Fellow Codenewbies ๐
You probably use px or em to set up the size of your image in CSS.
Well, I did.
But there is a good practice that I learned recently on how to set up the size of images, particularly the width.
Percentage is better to be used when we want to set the width of an image.
Let's take a look at this example.
In this example, we set the width of the image to 100%.
Oops.
Are you seeing a blurred image?
Maybe now you think that this image has bad quality?
The image has a relatively small resolution of 800 x 532 pixels.
What will happen if we expand the size of an image to be more than its own size?
Precisely! The quality will be reduced and we start to see pixels.
So it is not the image that has bad quality, but percentage as one of the relative CSS units makes the width of the image relative to its parent.
Setting up the width to 100% to the image means that the width of the image is as big as the width of the parent, which in this example, we've set the parent's width to 200%.
We use the max-width instead of width and set it to 100%.
img {
max-width: 100%;
}
We cannot change the quality of an image.
But when we work with images, especially images with relatively small-resolution, we better keep their original resolution to maintain their quality.
By setting max-width to 100% to an image, we are setting the maximum width to 100% of its own size.
So, the image would never get bigger than it is supposed to be even though the width of its parent gets bigger.
max-width instead of width to maintain the quality of the image.