How To Fill A Box With An Image Without Distorting It - MDN Web Docs
Có thể bạn quan tâm
- Guides
- CSS
- Use CSS to solve common problems
- How to fill a box with an image without distorting it
- English (US)
- 日本語
- Using object-fit
- Complete beginners start here!
- Getting started with the web
- Getting started with the web
- Installing basic software
- What will your website look like?
- Dealing with files
- HTML basics
- CSS basics
- JavaScript basics
- Publishing your website
- How the web works
- HTML — Structuring the web
- Introduction to HTML
- Introduction to HTML
- Getting started with HTML
- What's in the head? Metadata in HTML
- HTML text fundamentals
- Creating hyperlinks
- Advanced text formatting
- Document and website structure
- Debugging HTML
- Marking up a letter
- Structuring a page of content
- Multimedia and embedding
- Multimedia and embedding
- Images in HTML
- Video and audio content
- From object to iframe — other embedding technologies
- Adding vector graphics to the web
- Responsive images
- Mozilla splash page
- HTML tables
- HTML tables
- HTML table basics
- HTML table advanced features and accessibility
- Structuring planet data
- CSS — Styling the web
- CSS first steps
- CSS first steps
- What is CSS?
- Getting started with CSS
- How CSS is structured
- How CSS works
- Styling a biography page
- CSS building blocks
- CSS building blocks
- CSS selectors
- Type, class, and ID selectors
- Attribute selectors
- Pseudo-classes and pseudo-elements
- Combinators
- Cascade, specificity, and inheritance
- Cascade layers
- The box model
- Backgrounds and borders
- Handling different text directions
- Overflowing content
- CSS values and units
- Sizing items in CSS
- Images, media, and form elements
- Styling tables
- Advanced styling effects
- Debugging CSS
- Organizing your CSS
- Fundamental CSS comprehension
- Creating fancy letterheaded paper
- A cool-looking box
- Styling text
- CSS styling text
- Fundamental text and font styling
- Styling lists
- Styling links
- Web fonts
- Typesetting a community school homepage
- CSS layout
- CSS layout
- Introduction to CSS layout
- Normal Flow
- Flexbox
- Grids
- Floats
- Positioning
- Multiple-column layout
- Responsive design
- Beginner's guide to media queries
- Legacy layout methods
- Supporting older browsers
- Fundamental layout comprehension
- JavaScript — Dynamic client-side scripting
- JavaScript first steps
- JavaScript first steps
- What is JavaScript?
- A first splash into JavaScript
- What went wrong? Troubleshooting JavaScript
- Storing the information you need — Variables
- Basic math in JavaScript — numbers and operators
- Handling text — strings in JavaScript
- Useful string methods
- Arrays
- Silly story generator
- JavaScript building blocks
- JavaScript building blocks
- Making decisions in your code — conditionals
- Looping code
- Functions — reusable blocks of code
- Build your own function
- Function return values
- Introduction to events
- Event bubbling
- Image gallery
- Introducing JavaScript objects
- Introducing JavaScript objects
- JavaScript object basics
- Object prototypes
- Object-oriented programming
- Classes in JavaScript
- Working with JSON
- Object building practice
- Adding features to our bouncing balls demo
- Asynchronous JavaScript
- Asynchronous JavaScript
- Introducing asynchronous JavaScript
- How to use promises
- How to implement a promise-based API
- Introducing workers
- Sequencing animations
- Client-side web APIs
- Client-side web APIs
- Introduction to web APIs
- Manipulating documents
- Fetching data from the server
- Third-party APIs
- Drawing graphics
- Video and Audio APIs
- Client-side storage
- Web forms — Working with user data
- Web form building blocks
- Web form building blocks
- Your first form
- How to structure a web form
- Basic native form controls
- The HTML5 input types
- Other form controls
- Styling web forms
- Advanced form styling
- UI pseudo-classes
- Client-side form validation
- Sending form data
- Advanced web form techniques
- How to build custom form controls
- Sending forms through JavaScript
- CSS property compatibility table for form controls
- HTML forms in legacy browsers
- Accessibility — Make the web usable by everyone
- Accessibility guides
- Accessibility
- What is accessibility?
- HTML: A good basis for accessibility
- CSS and JavaScript accessibility best practices
- WAI-ARIA basics
- Accessible multimedia
- Mobile accessibility
- Assessment: Accessibility troubleshooting
- Performance — Making websites fast and responsive
- Performance guides
- Web performance
- The "why" of web performance
- What is web performance?
- Perceived performance
- Measuring performance
- Multimedia: Images
- Multimedia: video
- JavaScript performance optimization
- HTML performance optimization
- CSS performance optimization
- The business case for web performance
- MathML — Writing mathematics with MathML
- MathML first steps
- MathML first steps
- Getting started with MathML
- MathML Text Containers
- MathML fractions and roots
- MathML scripted elements
- MathML tables
- Three famous mathematical formulas
- Games — Developing games for the web
- Guides and tutorials
- Introduction to game development for the Web
- Techniques for game development
- Tutorials
- Publishing games
- Tools and testing
- Client-side web development tools
- Understanding client-side web development tools
- Client-side tooling overview
- Command line crash course
- Package management basics
- Introducing a complete toolchain
- Deploying our app
- Introduction to client-side frameworks
- Introduction to client-side frameworks
- Framework main features
- React
- Getting started with React
- Beginning our React todo list
- Componentizing our React app
- React interactivity: Events and state
- React interactivity: Editing, filtering, conditional rendering
- Accessibility in React
- React resources
- Ember
- Getting started with Ember
- Ember app structure and componentization
- Ember interactivity: Events, classes and state
- Ember Interactivity: Footer functionality, conditional rendering
- Routing in Ember
- Ember resources and troubleshooting
- Vue
- Getting started with Vue
- Creating our first Vue component
- Rendering a list of Vue components
- Adding a new todo form: Vue events, methods, and models
- Styling Vue components with CSS
- Using Vue computed properties
- Vue conditional rendering: editing existing todos
- Vue refs and lifecycle methods for focus management
- Vue resources
- Svelte
- Getting started with Svelte
- Starting our Svelte to-do list app
- Dynamic behavior in Svelte: working with variables and props
- Componentizing our Svelte app
- Advanced Svelte: Reactivity, lifecycle, accessibility
- Working with Svelte stores
- TypeScript support in Svelte
- Deployment and next steps
- Angular
- Getting started with Angular
- Beginning our Angular todo list app
- Styling our Angular app
- Creating an item component
- Filtering our to-do items
- Building Angular applications and further resources
- Git and GitHub
- Git and GitHub
- Cross browser testing
- Cross browser testing
- Introduction to cross-browser testing
- Strategies for carrying out testing
- Handling common HTML and CSS problems
- Handling common JavaScript problems
- Handling common accessibility problems
- Implementing feature detection
- Introduction to automated testing
- Setting up your own test automation environment
- Server-side website programming
- First steps
- Server-side website programming first steps
- Introduction to the server side
- Client-Server Overview
- Server-side web frameworks
- Website security
- Django web framework (Python)
- Django Web Framework (Python)
- Django introduction
- Setting up a Django development environment
- Django Tutorial: The Local Library website
- Django Tutorial Part 2: Creating a skeleton website
- Django Tutorial Part 3: Using models
- Django Tutorial Part 4: Django admin site
- Django Tutorial Part 5: Creating our home page
- Django Tutorial Part 6: Generic list and detail views
- Django Tutorial Part 7: Sessions framework
- Django Tutorial Part 8: User authentication and permissions
- Django Tutorial Part 9: Working with forms
- Django Tutorial Part 10: Testing a Django web application
- Django Tutorial Part 11: Deploying Django to production
- Django web application security
- Assessment: DIY Django mini blog
- Express Web Framework (Node.js/JavaScript)
- Express web framework (Node.js/JavaScript)
- Express/Node introduction
- Setting up a Node development environment
- Express Tutorial: The Local Library website
- Express Tutorial Part 2: Creating a skeleton website
- Express Tutorial Part 3: Using a Database (with Mongoose)
- Express Tutorial Part 4: Routes and controllers
- Express Tutorial Part 5: Displaying library data
- Express Tutorial Part 6: Working with forms
- Express Tutorial Part 7: Deploying to production
- Further resources
- Common questions
- Common questions
- Use HTML to solve common problems
- Use CSS to solve common problems
- Solve common problems in your JavaScript code
- Web mechanics
- Tools and setup
- Design and accessibility
- Using object-fit
In this guide you can learn a technique for causing an HTML image to completely fill a box.
Using object-fit
When you add an image to a page using the HTML <img> element, the image will maintain the size and aspect ratio of the image file, or that of any HTML width or height attributes. Sometimes you would like the image to completely fill the box that you have placed it in. In that case you first need to decide what happens if the image is the wrong aspect ratio for the container.
- The image should completely fill the box, retaining aspect ratio, and cropping any excess on the side that is too big to fit.
- The image should fit inside the box, with the background showing through as bars on the too-small side.
- The image should fill the box and stretch, which may mean it displays at the wrong aspect ratio.
The object-fit property makes each of these approaches possible. In the example below you can see how different values of object-fit work when using the same image. Select the approach that works best for your design.
Help improve MDN
Was this page helpful to you?YesNoLearn how to contribute.This page was last modified on Jul 2, 2024 by MDN contributors.
View this page on GitHub • Report a problem with this contentTừ 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 A Div Container Using CSS?
-
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?
-
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