EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

CSS object-fit

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » CSS Tutorial » CSS object-fit

CSS object-fit

Introduction to CSS object-fit

CSS object-fit is one of the CSS properties and will define how to specify the resizing property in an image or video that fits into a content box. An object-fit fix an issue related to image resizing like loss of aspect ratio and squished images. The object-fit property makes more sense when an image is part of the content which comes with image tag <img> to achieve the desired result. And they are element inserted directly into HTML code. When we need all of our images on a web page of small values (like profile pictures) on height and width this object-fit helps well.

Syntax and parameters:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The syntax to use this property in CSS is given below:

Object-fit: property values;

The possible Property values are fill, cover, contain, initial, scale-down, inherit and none.

How object-fit Property works in CSS?

As we all knew videos and images have problems while layout the websites. Like when we have limited space then we need to alter an images height therefore resizing the browsers will not meet aspect ratio so these problems can be solved using CSS-object fit and positioning as well. Before that, we should know the difference between an image and image content-box. The image says that it’s an original image with height and width and the content box has an element <img> inside the web page by specifying their width and height values.

These properties are very similar to the background-colour properties of CSS. This Object-fit works well with replaced elements i.e. an image and background properties cannot be applied to video part but the object-fit goes well. And replaced element is said to be an element whose dimensions are defined out of boundaries of CSS. The exact examples are <img> and <video> in case of embedded documents it is <input> element.

The object-fit has following possible values which are shown below:

  • fill: It is considered to be a default value also initial value. This stretches an element container to make themselves to fit inside the container box. Here the height and width of the box are the same.
  • cover: This property covers an entire container with the picture means it enlarges itself to maintain its aspect ratio.
  • contain: In this property values the element content is resized to stay within the container box and still retains its aspect ratio. The main benefit is it makes the image to be available inside the content box. If it doesn’t fit inside the box then the background color which is been specified fills up the remaining space.
  • scale-down: This compares the value of none and contain and choose the element which is smaller in size (scales down the particular image).
  • none: Makes an element content to retain into original size by neglecting container size. This may lead the image out of the box or stretching out of the container sometimes. An intrinsic aspect ratio remains the same. Cropping may take place when the image overflow the box.

Examples of CSS object-fit

In this section we shall see an example with demonstration by changing an image width and height.

Example #1

So here is the short example for different attribute property values.

Code:

outfit.html

<!DOCTYPE html>
<html>
<head>
<title>
CSS Object-Fit Property Demo
</title>
<style>
img {
width: 230px;
height: 450px;
object-fit: Fill;
}
</style>
</head>
<body>
<h1>CSS Object-Fit Property Demo Using Fill </h1>
<imgsrc="cat.jpeg"lt=" Cat Animal Pic" width="550" height="150">
</body>
</html>

This fill property stretches an original image to fit the box which we could see in the below demo.

Popular Course in this category
CSS Training (9 Courses, 9+ Projects)9 Online Courses | 9 Hands-on Projects | 61+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,454 ratings)
Course Price

View Course

Related Courses
Bootstrap Training (2 Courses, 6+ Projects)jQuery Training (8 Courses, 5 Projects)

Output:

CSS object-fit 1

Using Cover:

An above HTML code is changed to this value to view different position of the Cat Image.

Code:

<style>
img {
width: 230px;
height: 450px;
object-fit: cover;
}
</style>

Here the image is cropped to fit in the box.

Output:

CSS object-fit 2

Using Contain:

For the same HTML code we have applied background color to differentiate the image. As the image fits with the box’s height and width the remaining space is showed as yellow here.

Code:

bg.html

<!DOCTYPE html>
<html>
<head>
<title>
CSS Object-Fit Property Demo
</title>
<style>
img {
width: 230px;
height: 450px;
background: yellow;
color: red;
object-fit: contain;
}
</style>
</head>
<body>
<h1>CSS Object-Fit Property Demo Using Contain </h1>
<imgsrc= "cat.jpeg"lt=" Cat Animal Pic" width="550" height="150">
</body>
</html>

Output:

using contain

Using Scale-down:

Code:

<style>
img {
width: 230px;
height: 450px;
object-fit: scale-down;
}
</style>

Output:

using scale-down

Example #2

Implementation showing all the values together.

Code:

outfit.html

<html>
<head>
<title>My CSS </title>
<link rel="stylesheet" href="css/outfit.css">
</head>
<body>
<h1>CSS object-fit</h1>
<section id="falls">
<h1>original images</h1>
<imgsrc="old-falls-street-logo-spotlight.jpg" alt>
<imgsrc="banner-niagara1.jpg" alt>
</section>
<section>
<h1>Original structure: <code>img { width: 150px; height: 150px; border: 1.5px solid; background:purple; }</code></h1>
<imgsrc="old-falls-street-logo-spotlight.jpg" alt>
<imgsrc="banner-niagara1.jpg.jpg" alt>
<h2>Squished Picture</h2>
</section>
<section class="contain">
<h1>Original Structure along with the image + <code>img { object-fit: contain }</code></h1>
<imgsrc="old-falls-street-logo-spotlight.jpg" alt>
<imgsrc="banner-niagara1.jpg" alt>
<h2>Images are appropriate with the Aspect ratio</h2>
</section>
<section class="cover">
<h1>Original Structure + <code>img { object-fit: cover; }</code></h1>
<imgsrc="old-falls-street-logo-spotlight.jpg" alt>
<imgsrc="banner-niagara1.jpg" alt>
<h2>Changes in image to cover the width and height</h2>
</section>
<section class="none">
<h1>Original Structure + <code>img { object-fit: none; }</code></h1>
<imgsrc="old-falls-street-logo-spotlight.jpg" alt>
<imgsrc="banner-niagara1.jpg" alt>
<h2>Images are cropped to maintain the original pic</h2>
</section>
</body>
</html>

And the CSS file for the above HTML code is given here.

Code:

outfit.css

body {
font-family: calibri, sans-serif;
margin: 1;
text-align: center; }
h1, h2 { font-weight: 80; }
section:nth-child(even) {
background: green;
}
#falls img {
width: 35%;
max-width: 250px;
height: auto;
margin: 1.5%;
}
section h1 {
font-weight: 400;
padding-top: 2.5rem;
}
code { font-family: inherit; font-weight: 80; }
section img {
width: 150px;
height: 150px;
border: 1.5px solid;
background: purple;
}
section.containimg {
object-fit: contain;
}
section.coverimg {
object-fit: cover;
}
section.noneimg {
object-fit: none;
}

Output:

CSS object-fit 5

Conclusion

Therefore, we have seen how this CSS Object-fits provides magic is manipulating with the image. It helps the web developers to give control access over the image content or media like video. Along with object-fit object-position plays an important role.

Recommended Articles

This is a guide to CSS object-fit. Here we discuss how object-fit property works in CSS with programming examples respectively. You may also have a look at the following articles to learn more –

  1. Checkbox CSS
  2. CSS tricks
  3. CSS line height
  4. CSS Compressor

CSS Training (9 Courses, 9+ Projects)

9 Online Courses

9 Hands-on Projects

61+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
CSS Tutorial
  • CSS
    • Introduction To CSS
    • What is CSS?
    • Uses Of CSS
    • Advantages of CSS
    • Career In CSS
    • CSS Commands
    • Is Css Case Sensitive
    • CSS object-fit
    • Types of CSS Selectors
    • CSS Radio Button
    • CSS Attribute Selector
    • CSS first child of class
    • CSS Selector nth Child
    • CSS Parent Selector
    • CSS Child Selector
    • CSS Not Selector
    • CSS Descendant Selector
    • CSS Inline Style
    • Checkbox CSS
    • CSS Appearance
    • CSS Font Properties
    • CSS font-variant
    • CSS Pagination
    • CSS Table Styling
    • CSS Table Cell Padding
    • CSS Padding Color
    • CSS Text Formatting
    • CSS for Text-Shadow
    • CSS text-stroke
    • CSS text-indent
    • CSS Rotate Text
    • CSS Text Color
    • CSS Center Div
    • CSS Arrow
    • CSS Arrow Down
    • CSS offset
    • CSS Cursor
    • CSS Layout
    • CSS Grid Layout
    • Button in CSS
    • CSS Button Border
    • text-align in CSS
    • CSS Horizontal Align
    • CSS Position
    • CSS Box Sizing
    • CSS box-shadow
    • CSS Text Underline
    • CSS Text Outline
    • CSS Blinking Text
    • Text Decoration CSS
    • CSS Vertical Align
    • CSS Word Wrap
    • CSS Padding
    • CSS Font Color
    • CSS Color Generator
    • CSS Margin Right
    • CSS Margin Color
    • CSS Color Codes
    • CSS Color Transparent
    • CSS Color Chart
    • CSS Link Color
    • CSS z-index
    • CSS Curved Border
    • CSS Border Left
    • CSS left
    • CSS Gradient Generator
    • Radial Gradient in CSS
    • CSS Shape Generator
    • CSS Triangle Generator
    • CSS background-color
    • CSS Background Image
    • CSS background-clip
    • CSS background-blend-mode
    • CSS Drop Shadow
    • CSS line height
    • CSS line break
    • Sticky Footer CSS
    • CSS Header Design
    • CSS Border Style
    • CSS Border Generator
    • Sticky Sidebar CSS
    • CSS Transparent Border
    • CSS Border Radius
    • CSS translate
    • CSS transform
    • CSS 3D Transforms
    • CSS Text Transform
    • CSS Transition Effects
    • CSS Transition Property
    • CSS Animation Transition
    • Negative Margin CSS
    • CSS Navigation Bar
    • CSS Overflow
    • CSS overflow-wrap
    • CSS Lists
    • CSS list-style
    • CSS Order
    • CSS Box Model
    • CSS Inner Border
    • CSS Icon
    • Menu Icon CSS
    • CSS Multiple Borders
    • Opacity in CSS
    • CSS Float Right
    • CSS Clear Float
    • CSS clip
    • CSS disabled
    • CSS Border Padding
    • Border Images in CSS
    • CSS Visibility
    • CSS Validator
    • CSS Clearfix
    • CSS Counter
    • CSS Letter Spacing
    • CSS root
    • CSS zoom
    • CSS calc()
    • CSS.supports()
    • CSS Loader
    • Media Query CSS
    • CSS @keyframes
    • CSS @bottom
    • CSS page-break-after Property
    • CSS page-break
    • CSS Position Fixed
    • CSS skew()
    • CSS Row
    • CSS Masking
    • CSS Scrollbar
    • CSS Overlay
    • CSS Important
    • CSS Cursor Hand
    • CSS Inherit
    • CSS Position Relative
    • CSS Compressor
    • CSS tricks
    • CSS Outline Property
    • CSS Flexbox Properties
    • CSS flex-direction
    • CSS content property
    • CSS Typography
    • CSS Formatter
    • CSS nowrap
    • CSS Column
    • GridView CSS
    • CSS Viewport
    • CSS Minify
    • CSS Combinators
    • CSS in React
    • CSS Matrix 
    • CSS Pseudo Elements
    • CSS Pseudo Classes
    • CSS Pointer Events
    • CSS Resize
    • CSS Inheritance
    • CSS Interview Questions
    • Cheat Sheet CSS
  • CSS3
    • What is CSS3?
    • CSS3 Interview Questions
    • Cheat sheet CSS3
  • sass
    • How to Install SASS
    • SASS Interview Questions
    • What is Sass
    • SASS Comments
    • Sass Variables
    • SASS Import
    • SASS if else
    • SASS Nesting
    • SASS @each
    • SASS @at-root
    • SASS @extend
    • SASS @media
    • SASS @for
    • SASS Map
    • SASS Selectors
    • SASS Color Functions
    • SASS Mixins

Related Courses

CSS Training Course

Bootstrap Training Course

JQuery Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - CSS Training (9 Courses, 9+ Projects) Learn More