- CSS Introduction
- CSS Syntax
- CSS Selectors
- How To Add CSS
- CSS Comments
- CSS Backgrounds
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Height - Width
- CSS Box Model
- CSS Outline
- CSS Text
- CSS Links
- CSS Lists
- CSS Tables
- CSS Display
- CSS Max-width
- CSS Position
- CSS Overflow
- CSS Float
- CSS Inline-block
- CSS Align
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Image Gallery
- CSS Attr Selectors
- CSS Forms
- CSS Counters
- CSS Website Layout
- CSS !important
- CSS Rounded Corners
- CSS Border Images
- CSS Backgrounds
- CSS Colors
- CSS Color Keywords
- CSS Gradients
- CSS Radial Gradients
- CSS Text Effects
- CSS Web Fonts
- CSS 2D Transforms
- CSS 3D Transforms
- CSS Transitions
- CSS Animations
- CSS Tooltip
- CSS Image Reflection
- CSS The Object-fit
- CSS Object-position
- CSS Button
- CSS Pagination
- CSS Multiple Columns
- CSS User Interface
- CSS Variables
- CSS Box-sizing
- CSS Media Queries
- CSS Flexbox
CSS The object-fit
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
width: 200px;
height: 300px;
}
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 fitcontain
- The image keeps its aspect ratio, but is resized to fit within the given dimensioncover
- The image keeps its aspect ratio and fills the given dimension. The image will be clipped to fitnone
- The image is not resizedscale-down
- the image is scaled down to the smallest version ofnone
orcontain
<html>
<head>
<style>
img {
width:200px;
height:300px;
}
</style>
</head>
<body>
<h2>Image</h2>
<img src="paris.jpg" alt="Paris" width="400" height="300">
</body>
</html>
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
width: 200px;
height: 300px;
object-fit: cover;
}
<html>
<head>
<style>
img {
width: 200px;
height: 300px;
object-fit: cover;
}
</style>
</head>
<body>
<h2>Using object-fit: cover;</h2>
<img src="paris.jpg" alt="Paris" width="400" height="300">
</body>
</html>
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
width: 200px;
height: 300px;
object-fit: contain;
}
<html>
<head>
<style>
img {
width: 200px;
height: 300px;
object-fit: contain;
}
</style>
</head>
<body>
<h2>Using object-fit: contain;</h2>
<img src="paris.jpg" alt="Paris" width="400" height="300">
</body>
</html>
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.
width: 200px;
height: 300px;
object-fit: fill;
}
<html>
<head>
<style>
img {
width: 200px;
height: 300px;
object-fit: fill;
}
</style>
</head>
<body>
<h2>Using object-fit: fill;</h2>
<img src="paris.jpg" alt="Paris" width="400" height="300">
</body>
</html>
Using Object-fit: None
If we use object-fit: none;
the image is not resized.
width: 200px;
height: 300px;
object-fit: none;
}
<html>
<head>
<style>
img {
width: 200px;
height: 300px;
object-fit: none;
}
</style>
</head>
<body>
<h2>Using object-fit: none;</h2>
<img src="paris.jpg" alt="Paris" width="400" height="300">
</body>
</html>
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.
width: 200px;
height: 300px;
object-fit: scale-down;
}
<html>
<head>
<style>
img {
width: 200px;
height: 300px;
object-fit: scale-down;
}
</style>
</head>
<body>
<h2>Using object-fit: scale-down;</h2>
<img src="paris.jpg" alt="Paris" width="400" height="300">
</body>
</html>
CSS Object-fit
The following example demonstrates all the possible values of the object-fit
property in one example
.contain {object-fit: contain;}
.cover {object-fit: cover;}
.scale-down {object-fit: scale-down;}
.none {object-fit: none;}
<html>
<head>
<style>
.fill {object-fit: fill;}
.contain {object-fit: contain;}
.cover {object-fit: cover;}
.scale-down {object-fit: scale-down;}
.none {object-fit: none;}
</style>
</head>
<body>
<h1>The object-fit Property</h1>
<h2>No object-fit:</h2>
<img src="paris.jpg" alt="Paris" style="width:200px;height:300px">
<h2>object-fit: fill (this is default):</h2>
<img class="fill" src="paris.jpg" alt="Paris" style="width:200px;height:300px">
<h2>object-fit: contain:</h2>
<img class="contain" src="paris.jpg" alt="Paris" style="width:200px;height:300px">
<h2>object-fit: cover:</h2>
<img class="cover" src="paris.jpg" alt="Paris" style="width:200px;height:300px">
<h2>object-fit: scale-down:</h2>
<img class="scale-down" src="paris.jpg" alt="Paris" style="width:200px;height:300px">
<h2>object-fit: none:</h2>
<img class="none" src="paris.jpg" alt="Paris" style="width:200px;height:300px">
</body>
</html>
COMPANY
Products
Partner
Copyright © RVR Innovations LLP 2024 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP