
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
How to auto-resize an image to fit a div container using CSS?
To auto-resize an image to fit a div container, we have set the following CSS fproperty or the img tag −
max-width: 100%; max-height: 100%;
First, we have set an image using the img tag in the div mydiv −
<div id="myDiv"> <img src="https://www.tutorialspoint.com/behave/images/behave.jpg"> </div>
We have set the mydiv with height and width auto to allow auto-resize the div −
#myDiv { height: auto; width: auto; border: 2px solid blue; }
Now, the image in the mydiv set with the following CSS properties max-width and max-height to allow auto-resize without any problem −
#myDiv img { max-width: 100%; max-height: 100%; margin: auto; display: block; }
Example
Let us now see the complete example to auto-resize an image to fit a div container using CSS −
<!DOCTYPE html> <html> <head> <style> #myDiv { height: auto; width: auto; border: 2px solid blue; } #myDiv img { max-width: 100%; max-height: 100%; margin: auto; display: block; } </style> </head> <body> <div id="myDiv"> <img src="https://www.tutorialspoint.com/behave/images/behave.jpg"> </div> </body> </html>
Output

- Related Articles
- How to Drag / Pan an Image in a Container div using jQuery?
- How to resize an image using Tkinter?
- How an or should be resized to fit its container with CSS
- How to resize an image in Android using Picasso?
- How to resize an image in OpenCV using Python?
- How to resize an Image C#?
- PyTorch – How to resize an image to a given size?
- How to Resize an image in Android using Picasso on Kotlin?
- How to resize an image in Node Jimp?
- How to fit the ellipse to an object in an image using OpenCV Python?
- How to resize Image in Android App using Kotlin?
- Difference between Auto-fit vs Auto-fill property in CSS grid
- Creating auto-resize text area using JavaScript
- How to Auto-Fit Column Width in Excel?
- How to resize image in PHP?

Advertisements