- How to Resize Images Proportionally for Responsive Web Design With CSS
- Resize images with the HTML width and height attributes
- Example of resizing an image proportionally with the width and height attributes:
- Result
- Resize images with the CSS width and height properties
- Example of resizing an image proportionally with the width and height properties:
- Resize images with the CSS max-width property
- Example of adding a responsive resized image with the max-width property:
- Example of resizing an image proportionally using the max-width property:
- 3 Ways To Auto Resize Images In HTML CSS (Simple Examples)
- TLDR – QUICK SLIDES
- TABLE OF CONTENTS
- DOWNLOAD & NOTES
- QUICK NOTES
- EXAMPLE CODE DOWNLOAD
- AUTO-RESIZING IMAGES
- EXAMPLE 1) RESIZE IMAGE WHILE MAINTAINING ASPECT RATIO
- EXAMPLE 2) RESIZE IMAGE TO FIT CONTAINER
- EXAMPLE 3) RESIZE BACKGROUND IMAGE
- EXTRA BITS & LINKS
- LINKS & REFERENCES
- INFOGRAPHIC CHEAT SHEET
- THE END
- Leave a Comment Cancel Reply
- Search
- Breakthrough Javascript
- Socials
- About Me
How to Resize Images Proportionally for Responsive Web Design With CSS
One of the main parts of responsive web design is resizing the image automatically to fit the width of its container. Since nowadays people use different kinds of desktops, our web pages must be appropriate for all types of screens.
So, let’s learn three ways of resizing images and making responsive images with only HTML and CSS.
Resize images with the HTML width and height attributes
Example of resizing an image proportionally with the width and height attributes:
html> html> head> title>Title of the document title> head> body> p>Resized image: p> img src="/uploads/media/default/0001/01/25acddb3da54207bc6beb5838f65f022feaa81d7.jpeg" alt="Aleq" width="160" height="145" /> p>Original image size: p> img src="/uploads/media/default/0001/01/25acddb3da54207bc6beb5838f65f022feaa81d7.jpeg" alt="Aleq" /> body> html>
Result
While resizing an image, the aspect ratio (the viewable area and display shape) should be preserved. Otherwise, the image can get distorted and lose its quality.
Resize images with the CSS width and height properties
Another way of resizing images is using the CSS width and height properties. Set the width property to a percentage value and the height to «auto». The image is going to be responsive (it will scale up and down).
Example of resizing an image proportionally with the width and height properties:
html> html> head> title>Responsive Resized Image title> style> img < width: 100%; height: auto; > style> head> body> h2>Responsive Resized Image h2> p>Resize the browser to see the responsive effect: p> img src="/uploads/media/default/0001/03/5bfad15a7fd24d448a48605baf52655a5bbe5a71.jpeg" alt="New York" /> body> html>
Note that the image could be scaled more than the original size. In several cases, a better option is to use the max-width property alternatively.
Resize images with the CSS max-width property
There is a better way for resizing images responsively. If the max-width property is set to 100%, the image will scale down if it has to, but never scale up to be larger than its original size. The trick is to use height: auto; to override any already present height attribute on the image.
Example of adding a responsive resized image with the max-width property:
html> html> head> title>Responsive Resized Image with max-width title> style> img < max-width: 100%; height: auto; > style> head> body> h2>Responsive Resized Image with max-width h2> p>Resize the browser to see the responsive effect: p> img src="/uploads/media/default/0001/03/5bfad15a7fd24d448a48605baf52655a5bbe5a71.jpeg" alt="New York" /> body> html>
Let’s see another live web page example with the same method.
Example of resizing an image proportionally using the max-width property:
html> html> head> title>Responsive Resized Image in a Web Page title> style> * < box-sizing: border-box; > html < font-family: "Lucida Sans", sans-serif; > img < width: 100%; height: auto; > .row:after < content: ""; clear: both; display: table; > .menu, .content < float: left; padding: 15px; width: 100%; > @media only screen and (min-width: 600px) < .menu < width: 25%; > .content < width: 75%; > > @media only screen and (min-width: 768px) < .menu < width: 20%; > .content < width: 79%; > > .header < background-color: #1c87c9; color: #ffffff; padding: 10px; text-align: center; > .menu ul < list-style-type: none; margin: 0; padding: 0; > .menu li < padding: 8px; margin-bottom: 7px; background-color: #666666; color: #ffffff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12), 0 1px 2px rgba(0, 0, 0, 0.24); > .menu li:hover < background-color: #1c87c9; > .footer < background-color: #1c87c9; color: #ffffff; text-align: center; font-size: 12px; padding: 15px; > style> head> body> div class="header"> h1>W3DOCS h1> div> div class="row"> div class="menu"> ul> li>Books li> li>Snippets li> li>Quizzes li> li>Tools li> ul> div> div class="content"> h2>About Us h2> p> W3docs provides free learning materials for programming languages like HTML, CSS, Java Script, PHP etc. p> p> It was founded in 2012 and has been increasingly gaining popularity and spreading among both beginners and professionals who want to enhance their programming skills. p> img src="/uploads/media/default/0001/02/8070c999efd1a155ad843379a5508d16f544aeaf.jpeg" alt="Team Work" /> div> div> div class="footer"> p> Resize the browser window to see how the content respond to the resizing. p> div> body> html>
3 Ways To Auto Resize Images In HTML CSS (Simple Examples)
Welcome to a quick tutorial on how to auto-resize images in HTML and CSS. Just started with HTML CSS and struggling to fit images?
The easiest way to create an auto-resize, scale-to-fit image is to set 100% width on it –
Yes, that’s all. We don’t need even crazy Javascript calculations – Read on for more examples and ways to resize an image in CSS.
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
TLDR – QUICK SLIDES
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTES
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming .
EXAMPLE CODE DOWNLOAD
Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
AUTO-RESIZING IMAGES
All right, let us now get into the examples of auto-resizing images in HTML and CSS.
EXAMPLE 1) RESIZE IMAGE WHILE MAINTAINING ASPECT RATIO
.scaleA![]()
- Setting width: 100% on the image will make it fill the full width of its container automatically.
- By default, images are set to height: auto . So it scales while retaining its original aspect ratio.
- Lastly, max-width: max-content is optional. It limits the maximum resize to the image’s original width. For example, if the image has a width of 1000 px, width: 100% will only scale up to 1000 px. For you guys who are new, over-stretched images will turn blurry, and this is to prevent that.
EXAMPLE 2) RESIZE IMAGE TO FIT CONTAINER
/* (A) DESIRED FIXED DIMENSION */ .scaleB < width: 200px; height: 200px; border: 1px solid black; >/* (B) RESIZE METHOD */ .fill < object-fit: fill; >.contain < object-fit: contain; >.cover < object-fit: cover; >.scale
If you want to resize the image to fit certain dimensions, object-fit is the way to go.
- none The default if nothing is defined. No scaling or resizing.
- fill This one is funky. The image is simply stretched to the specified dimensions, ignoring the original aspect ratio.
- contain The image will be contained within the specified dimensions, but scaled according to the original aspect ratio.
- cover The image is scaled to its original aspect ratio and clipped to cover the given dimensions.
- scale-down This is either none or contain . Whichever fits better.
EXAMPLE 3) RESIZE BACKGROUND IMAGE
- background-image Self-explanatory, which image to use as the background.
- background-position Center the image in the .
- background-size As with the previous example, we can set the resize method to either contain or cover .
- background-repeat Since this example is a single image, we set it to no-repeat . This will resize the image to fit the container, instead of repeating it to fill the container.
- background-attachment Which element the background image is “attached to”, and how it acts while scrolling.
- fixed Relative to the viewport. Background does not move while scrolling.
- scroll Relative to the itself, does not move while scrolling.
- local Relative to the contents of the , moves along while scrolling.
EXTRA BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
LINKS & REFERENCES
INFOGRAPHIC CHEAT SHEET
THE END
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!
Leave a Comment Cancel Reply
Search
Breakthrough Javascript
Take pictures with the webcam, voice commands, video calls, GPS, NFC. Yes, all possible with Javascript — Check out Breakthrough Javascript!
Socials
About Me
W.S. Toh is a senior web developer and SEO practitioner with over 20 years of experience. Graduated from the University of London. When not secretly being an evil tech ninja, he enjoys photography and working on DIY projects.
Code Boxx participates in the eBay Partner Network, an affiliate program designed for sites to earn commission fees by linking to ebay.com. We also participate in affiliate programs with Bluehost, ShareASale, Clickbank, and other sites. We are compensated for referring traffic.