How To Auto-resize An Image To Fit A Div Container Using CSS?
Có thể bạn quan tâm
- Tutorials
- Python Tutorial
- Taking Input in Python
- Python Operators
- Python Data Types
- Python Numbers
- Python String
- Python Lists
- Python Tuples
- Sets in Python
- Python Dictionary
- Python Loops and Control Flow
- Python Conditional Statements
- Python Loops
- Python Functions
- Python OOPS Concept
- Python Data Structures
- Python DSA
- Linked List
- Stack
- Queue
- Tree
- Heap
- Hashing
- Graph
- Sets
- Map
- Advance Data Structure
- Sorting Algorithms
- Searching Algorithms
- Python Exception Handling
- Python File Handling
- Python Exercises
- Python List Exercise
- Python String Exercise
- Python Tuple Exercise
- Python Dictionary Exercise
- Python Set Exercise
- Python Design Patterns
- Python Programming Examples
- Python Practice Questions
- Java
- Java Programming Language
- Java Tutorial
- Data Types
- Variables
- Operators
- Flow Control in Java
- Loops in Java
- Methods
- Strings
- Arrays
- OOPs Concepts
- OOPs Concepts
- Classes and Objects
- Access Modifiers
- Inheritance
- Abstraction
- Encapsulation
- Polymorphism
- Interface
- Packages
- Multithreading
- File Handling
- Regular Expression
- Java Collections
- Java Collections
- Collection Class
- List Interface
- ArrayList
- LinkedList Class
- Queue Interface
- Set Interface
- HashSet Class
- Map Interface
- HashMap Class
- HashTable Class
- Iterator
- Comparator
- Collection Interview Questions
- Java 8 Tutorial
- Java Programs
- Java Programming Examples
- Java Array Programs
- Java String Programs
- Java Date-Time Programs
- Java File Handling Programs
- Java Collection Programs
- Java JDBC Programs
- Java Apache POI Programs
- Java OpenCV Programs
- Java Interview Questions
- Java Interview Questions
- Core Java Interview Questions-Freshers
- Java Multithreading Interview Questions
- OOPs Interview Questions and Answers
- Java Exercises
- Java Quiz
- Java Quiz
- Core Java MCQ
- Java Projects
- Advance Java
- Spring Tutorial
- Spring Boot Tutorial
- Spring Boot Interview Questions
- Spring MVC Tutorial
- Spring MVC Interview Questions
- Hibernate Tutorial
- Hibernate Interview Questions
- Java Programming Language
- Programming Languages
- C
- C++
- JavaScript
- PHP
- R Tutorial
- C#
- SQL
- Scala
- Perl
- Go Language
- Kotlin
- System Design
- System Design Tutorial
- What is System Design
- Key Terminologies in System Design
- Analysis and Architecture of Systems
- Scalability in System Design
- Databases in System Design
- High Level Design or HLD
- Low Level Design or LLD
- Case Studies in Designing Systems
- Complete System Design Tutorial
- Software Design Patterns
- System Design Roadmap
- Top 10 System Design Interview Questions and Answers
- System Design Tutorial
- Interview Corner
- Company Preparation
- Top Topics
- Practice Company Questions
- Interview Experiences
- Experienced Interviews
- Internship Interviews
- Competitive Programming
- Multiple Choice Quizzes
- Aptitude for Placements
- Computer Science Subjects
- Operating System
- DBMS
- Computer Networks
- Engineering Mathematics
- Computer Organization and Architecture
- Theory of Computation
- Compiler Design
- Digital Logic
- Software Engineering
- DevOps
- GIT
- AWS
- Docker
- Kubernetes
- Microsoft Azure Tutorial
- Google Cloud Platform
- Linux
- Linux Tutorial
- Linux Commands A-Z
- Linux Commands Cheatsheet
- File Permission Commands
- Linux System Administration
- Linux File System
- Linux Shell Scripting
- Linux Networking
- Linux Interview Questions
- Software Testing
- Software Testing Tutorial
- Software Engineering Tutorial
- Testing Interview Questions
- Jira
- Databases
- DBMS Tutorial
- SQL Tutorial
- PostgreSQL Tutorial
- MongoDB Tutorial
- SQL Interview Questions
- MySQL Interview Questions
- PL/SQL Interview Questions
- Android
- Android Tutorial
- Android Studio Tutorial
- Kotlin For Android
- Android Projects
- Android Interview Questions
- 6 Weeks of Android App Development
- Excel
- MS Excel Tutorial
- Introduction to MS Excel
- Data Analysis in Excel
- Basic Excel Formulas & Functions
- Data Analysis in Advanced Excel
- Workbooks
- Statistical Functions
- Data Visualization in Excel
- Pivot Tables in Excel
- Excel Spreadsheets in Python
- Basic Excel Shortcuts
- Mathematics
- Number System
- Algebra
- Linear Algebra
- Trigonometry
- Set Theory
- Statistics
- Probability
- Geometry
- Mensuration
- Logarithms
- Calculus
- Python Tutorial
- DSA
- Data Structures
- Arrays
- Matrix
- Strings
- Linked List
- Singly Linked List
- Doubly Linked List
- Circular Linked List
- Doubly Circular Linked List
- Linked List Tutorial
- Stack
- Queue
- Tree
- Generic Tree
- Binary Tree
- Binary Search Tree
- AVL Tree
- B Tree
- B+ Tree
- Red Black Tree
- Tree Data Structure Tutorial
- Heap
- Hashing
- Graph
- Set Data Structure
- Map Data Structure
- Advanced Data Structure
- Data Structures Tutorial
- Algorithms
- Analysis of Algorithms
- Design and Analysis of Algorithms
- Asymptotic Analysis
- Asymptotic Notations
- Worst, Average and Best Cases
- Searching Algorithms
- Linear Search
- Binary Search
- Searching Algorithms Tutorial
- Sorting Algorithms
- Selection Sort
- Bubble Sort
- Insertion Sort
- Merge Sort
- Quick Sort
- Heap Sort
- Counting Sort
- Radix Sort
- Bucket Sort
- Sorting Algorithms Tutorial
- Greedy Algorithms
- Dynamic Programming
- Graph Algorithms
- Pattern Searching
- Recursion
- Backtracking
- Divide and Conquer
- Mathematical Algorithms
- Geometric Algorithms
- Bitwise Algorithms
- Randomized Algorithms
- Branch and Bound
- Algorithms Tutorial
- Analysis of Algorithms
- DSA Tutorial
- Practice
- All DSA Problems
- Problem of the Day
- Company Wise Coding Practice
- Amazon
- Microsoft
- Flipkart
- Explore All
- GfG SDE Sheet
- Practice Problems Difficulty Wise
- School
- Basic
- Easy
- Medium
- Hard
- Language Wise Coding Practice
- CPP
- Java
- Python
- Curated DSA Lists
- Beginner's DSA Sheet
- Top 50 Array Problems
- Top 50 String Problems
- Top 50 DP Problems
- Top 50 Graph Problems
- Top 50 Tree Problems
- Competitive Programming
- Company Wise SDE Sheets
- Facebook SDE Sheet
- Amazon SDE Sheet
- Apple SDE Sheet
- Netflix SDE Sheet
- Google SDE Sheet
- DSA Cheat Sheets
- SDE Sheet
- DSA Sheet for Beginners
- FAANG Coding Sheet
- Product-Based Coding Sheet
- Company-Wise Preparation Sheet
- Top Interview Questions
- Puzzles
- All Puzzles
- Top 100 Puzzles Asked In Interviews
- Top 20 Puzzles Commonly Asked During SDE Interviews
- Data Structures
- Data Science
- Python Tutorial
- R Tutorial
- Machine Learning
- Data Science using Python
- Data Science using R
- Data Science Packages
- Pandas Tutorial
- NumPy Tutorial
- Data Visualization
- Python Data Visualization Tutorial
- Data Visualization with R
- Data Analysis
- Data Analysis with Python
- Data Analysis with R
- Deep Learning
- NLP Tutorial
- Web Tech
- HTML Tutorial
- CSS Tutorial
- JavaScript Tutorial
- PHP Tutorial
- ReactJS Tutorial
- NodeJS Tutorial
- AngularJS Tutorial
- Bootstrap Tutorial
- Typescript
- Web Development Using Python
- Django
- Django Tutorial
- Django Projects
- Django Interview Questions
- Flask
- Flask Tutorial
- Flask Projects
- Flask Interview Questions
- Postman
- Github
- Django
- Wordpress Tutorial
- Web Design
- Cheat Sheets
- HTML Cheat Sheet
- CSS Cheat Sheet
- JavaScript Cheat Sheet
- React Cheat Sheet
- Angular Cheat Sheet
- jQuery Cheat Sheet
- Bootstrap Cheat Sheet
- Learn Complete Web Development
- Courses
- Coding for Everyone
- DSA to Development
- Machine Learning & Data Science
- Generative AI & ChatGPT
- Become AWS Certified
- DSA Courses
- Data Structure & Algorithm(C++/JAVA)
- Data Structure & Algorithm(Python)
- Data Structure & Algorithm(JavaScript)
- Programming Languages
- CPP
- Java
- Python
- JavaScript
- C
- CSS Tutorial
- CSS Exercises
- CSS Interview Questions
- CSS Selectors
- CSS Properties
- CSS Functions
- CSS Examples
- CSS Cheat Sheet
- CSS Templates
- CSS Frameworks
- Bootstrap
- Tailwind
- CSS Formatter
- Space between two rows in a table using CSS?
- What is the difference between display: inline and display: inline-block in CSS?
- How to write a:hover in inline CSS?
- How to place two div side-by-side of the same height using CSS?
- What is a clearfix?
- Making a div vertically scrollable using CSS
- How to give a div tag 100% height of the browser window using CSS
- Wildcard Selectors (*, ^ and $) in CSS for classes
- How to style a dropdown using CSS?
- Remove border from IFrame using CSS
- Hide scroll bar, but while still being able to scroll using CSS
- How to apply !important in CSS?
- How to use a not:first-child selector in CSS?
- How to auto-resize an image to fit a div container using CSS?
- How to disable resizable property of textarea using CSS?
- Create an unordered list without any bullets using CSS
- Set cellpadding and cellspacing in CSS
- How to overlay one div over another div using CSS
- How to disable a link using only CSS?
- What does the “+” (plus sign) CSS selector mean?
- How to Vertically Center Text with CSS ?
- How to horizontally center a div using CSS ?
- Change an HTML5 input placeholder color with CSS
- How to disable text selection highlighting using CSS?
- How to align content of a div to the bottom using CSS ?
To resize an image or video to fit inside a div container, you can use the object-fit property. This property is used to specify how the content should fit inside the container. With object-fit, you can set the content to maintain its aspect ratio or stretch to fill the entire container. This allows you to control how the content is displayed within a specific div container.
Example 1: This example describes the auto-resize image fit to the div container. This example does not contain object-fit property.
html
<!DOCTYPE html> <html> <head> <title>To auto-resize an image</title> <style> body { display: flex; justify-content: center; } .geeks { width: 60%; height: 300px; border: 1px solid #ccc; } img { width: 100%; height: 100%; } </style> </head> <body> <div class="geeks"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png" alt="Geeks Image" /> </div> </body> </html> |
Output:
In the above example as the object-fit property is not used, the image is being squeezed to fit the container, and its original aspect ratio is destroyed.
Example 2: This example is used to display the part of image when use resize the div container.
HTML
<!DOCTYPE html> <html> <head> <title>To auto-resize an image</title> <style> body { display: flex; justify-content: center; } .geeks { width: 60%; height: 300px; border: 1px solid #ccc; } img { width:100%; height:100%; object-fit:cover; } </style> </head> <body> <div class="geeks"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png" alt="Geeks Image" /> </div> </body> </html> |
Output:
Note: Using object-fit: cover; will cut off the sides of the image, preserving the aspect ratio, and also filling in space.
Example 3: This example displays an image without using object-fit property. In this example, the size of the image is set manually and the image will not be able to maintain its aspect ratio and adjust or resize according to div container on resizing the browser window.
html
<!DOCTYPE html> <html lang="en"> <head> <title>Auto-resize of an image</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { display: flex; justify-content: center; } .container { width: 400px; height: 250px; border: 1px solid #ccc; overflow: hidden; } .container img { width: 100%; height: auto; display: block; } </style> </head> <body> <div class="container"> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png" alt="GeeksforGeeks Image"> </div> </body> </html> |
Output:
Example 4: This example displays the part of image or image using object-fit property. In this example, the size of the image is set manually and the object-fit property is also used. In this case, on resizing the browser the image will maintain its aspect ratio and will not be resized according to div container.
html
<!DOCTYPE html> <html> <head> <title>To auto-resize an image</title> <style> body { text-align: center; } img { width: 400px; height: 200px; object-fit: cover; } </style> </head> <body> <img src= "https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-25.png" alt="Geeks Image"> </body> </html> |
Output: Note: The property object-fit: cover; will cut the sides of the image, preserving the aspect ratio, and also filling in space.
CSS is the foundation of webpages, is used for webpage development by styling websites and web apps. You can learn CSS from the ground up by following this CSS Tutorial and CSS Examples.
S
Sabya_Samadder Follow Improve Previous Article How to use a not:first-child selector in CSS? Next Article How to disable resizable property of textarea using CSS?Please Login to comment...
Similar Reads
How to stretch div to fit the container ? A child div inside a container can be made to take the complete width and height of the parent div. There are two methods to stretch the div to fit the container using CSS that are discussed below: Method 1: The first method is to simply assign 100% width and 100% height to the child div so that it will take all available space of the parent div. E 2 min read Auto-Fit vs Auto-Fill Property in CSS Grid One of the most important features in CSS Grid is that we can create a responsive layout without using a media query. We don't need to write a media query for each viewport rather it can be adjusted by using some of the properties of CSS-Grid. It can be adjusted by simply using grid-template-columns property, repeat() function along with auto-fit a 4 min read How to Fit Background Image to Div using CSS ? In this article, we will see how to fit background images into HTML div. The main problem that is being solved here is that the dimensions of the background image of a div are not exactly the same as the dimensions of the actual div, so there arises a mismatch. Due to this mismatch, the background image doesn't fit properly in the div. Now, there a 3 min read How to fit text container width dynamically according to screen size using CSS ? In this article, we have given a text paragraph and the task is to fit the text width dynamically according to screen size using HTML and CSS. Some of the important properties used in the code are as follows- display-grid: It helps us to display the content on the page in the grid structure.grid-gap: This property sets the minimum amount of gap bet 7 min read How to Set Div Width to Fit Content using CSS? The div can be managed to set the whole screen width or the width according to the need. In this article, we are going to discuss how can we set a div width to fit content using CSS. we will discuss different approaches and code examples. Below are the approaches to set div width to fit content using CSS: Table of Content Using Default CSSUsing inl 3 min read How to make CSS Font Size Fit Container Effectively ? Making CSS font size fit a container effectively involves ensuring that the text scales appropriately based on the dimensions of the container. This is crucial for maintaining readability and a consistent user experience regardless of the device being used. Table of Content Using ViewportUsing Media QueriesUsing CSS ClampUsing ViewportWe can use vi 5 min read How to position a div at the bottom of its container using CSS? To position a div at the bottom of its container in CSS, you can use various approaches. Using absolute positioning involves setting `position: absolute;` and `bottom: 0;`, aligning the div at the container's bottom. Flexbox achieves this by employing `display: flex;` and `margin-top: auto;`, while Grid utilizes `display: grid;` with a defined temp 2 min read How to resize SVG to fill its container in ReactJS? We can resize SVG to fill its container in ReactJS using the type props. We can add the following style to the SVG CSS Syntax: svg { width: inherit; height: inherit; } React Syntax: <svg style = {{ width: "inherit", height:"inherit"}} // Child elements </svg>Creating React Application: Step 1: Create a React application using the following 2 min read How to overlay one div over another div using CSS Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple to design. Overlays can create using two simple CS 2 min read How to Make a Child Div Element Wider than Parent Div using CSS ? When the child div element has a fixed width that is wider than the parent div element, it will display overflow outside of the parent element. Because the overflow is hidden by default, one approach is to utilize the overflow attribute to enable it on an element. Another approach includes setting the position property of the parent div element to 2 min read View More Articles Article Tags :- CSS-Questions
- Technical Scripter 2018
- CSS
- Technical Scripter
What kind of Experience do you want to share?
Interview Experiences Admission Experiences Career Journeys Work Experiences Campus Experiences Competitive Exam Experiences Can't choose a topic to write? click here for suggested topics Write and publish your own ArticleTừ khóa » Html Scale Image To Fit Div
-
How Do I Auto-resize An Image To Fit A 'div' Container? - Stack Overflow
-
How To Auto-resize An Image To Fit Into A DIV ... - Tutorial Republic
-
CSS Object-fit Property - W3Schools
-
How To Auto-Resize The Image To Fit An HTML Container - W3docs
-
Auto Resize An Image To Fit Into A HTML Div Using CSS
-
Image Fit To Div Code Example - Code Grepper
-
How To Resize An Image In HTML?
-
Object Fit - Tailwind CSS
-
How To Auto-resize An Image To Fit Into A DIV Container Using CSS?
-
How To Fill A Box With An Image Without Distorting It - MDN Web Docs
-
Object-fit - CSS: Cascading Style Sheets - MDN Web Docs - Mozilla
-
How To Scale And Crop Images With CSS Object-fit | DigitalOcean
-
Object-fit | CSS-Tricks
-
How To Auto-resize An Image To Fit Into A DIV Container Using CSS