CSS Object-fit Property - W3Schools
Có thể bạn quan tâm
The CSS object-fit property is used to specify how an <img> or <video> should be resized to fit its container.
The CSS object-fit Property
The CSS object-fit property is used to specify how an <img> or <video> should be resized to fit its container.
This property tells the content to fill the container in a variety of ways; such as "preserve that aspect ratio" or "stretch up and take up as much space as possible".
Look at the following image from Paris. This image is 400 pixels wide and 300 pixels high:
![Paris](paris.jpg)
However, if we style the image above to be half its width (200 pixels) and same height (300 pixels), it will look like this:
![Paris](paris.jpg)
Example
img { width: 200px; height: 300px;} Try it Yourself »We see that the image is being squished to fit the container of 200x300 pixels (its original aspect ratio is destroyed).
Here is where the object-fit property comes in. The object-fit property can take one of the following values:
- fill - This is default. The image is resized to fill the given dimension. If necessary, the image will be stretched or squished to fit
- contain - The image keeps its aspect ratio, but is resized to fit within the given dimension
- cover - The image keeps its aspect ratio and fills the given dimension. The image will be clipped to fit
- none - The image is not resized
- scale-down - the image is scaled down to the smallest version of none or contain
Using object-fit: cover;
If we use object-fit: cover; the image keeps its aspect ratio and fills the given dimension. The image will be clipped to fit:
![Paris](paris.jpg)
Example
img { width: 200px; height: 300px; object-fit: cover;} Try it Yourself »Using object-fit: contain;
If we use object-fit: contain; the image keeps its aspect ratio, but is resized to fit within the given dimension:
![Paris](paris.jpg)
Example
img { width: 200px; height: 300px; object-fit: contain;} Try it Yourself »Using object-fit: fill;
If we use object-fit: fill; the image is resized to fill the given dimension. If necessary, the image will be stretched or squished to fit:
![Paris](paris.jpg)
Example
img { width: 200px; height: 300px; object-fit: fill;} Try it Yourself »Using object-fit: none;
If we use object-fit: none; the image is not resized:
![Paris](paris.jpg)
Example
img { width: 200px; height: 300px; object-fit: none;} Try it Yourself »Using object-fit: scale-down;
If we use object-fit: scale-down; the image is scaled down to the smallest version of none or contain:
![Paris](paris.jpg)
Example
img { width: 200px; height: 300px; object-fit: scale-down;} Try it Yourself »Another Example
Here we have two images and we want them to fill the width of 50% of the browser window and 100% of the height.
In the following example we do NOT use object-fit, so when we resize the browser window, the aspect ratio of the images will be destroyed:
Example
![Norway](rock600x400.jpg)
![Paris](paris.jpg)
In the next example, we use object-fit: cover;, so when we resize the browser window, the aspect ratio of the images is preserved:
Example
![Norway](rock600x400.jpg)
![Paris](paris.jpg)
CSS object-fit More Examples
The following example demonstrates all the possible values of the object-fit property in one example:
Example
.fill {object-fit: fill;} .contain {object-fit: contain;} .cover {object-fit: cover;} .scale-down {object-fit: scale-down;} .none {object-fit: none;} Try it Yourself »CSS Object-* Properties
The following table lists the CSS object-* properties:
Property | Description |
---|---|
object-fit | Specifies how an <img> or <video> should be resized to fit its container |
object-position | Specifies how an <img> or <video> should be positioned with x/y coordinates inside its "own content box" |
Từ khóa » Html Css Stretch Image To Fit Div
-
How To Auto-resize An Image To Fit Into A DIV Container Using CSS
-
Stretch Image To Fit 100% Of Div Height And Width - Stack Overflow
-
How To Auto-resize An Image To Fit A Div Container Using CSS?
-
Make Image Stretch To Fit Div Code Example
-
Css Stretch Image To Fill Div Code Example - Code Grepper
-
How To Auto-Resize The Image To Fit An HTML Container - W3docs
-
Css Resize Image To Fit Div No Stretching - MaxInterview
-
How To Resize An Image In HTML?
-
Object-fit | CSS-Tricks
-
How To Scale And Crop Images With CSS Object-fit | DigitalOcean
-
How To Stretch A Background Image To Fit A Web Page - ThoughtCo
-
Object Fit - Tailwind 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