CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Object Fit & Object Position


Object-Fit

The object-fit property in CSS dictates how the content of a replaced element, such as an <img> or <video>, should be resized to fit its container. This property is essential for maintaining the aspect ratio of media without distortion. It provides more control over the display of images and videos within specific dimensions.


Example 1: object-fit: fill

.container {
  width: 200px;
  height: 300px;
  border: 2px solid #ccc;
}

.container img {
  width: 100%;
  height: 100%;
  object-fit: fill; /* Stretches the image to fit the container, potentially distorting it */
}

Explanation The fill value is the default. It stretches the image to completely fill the container's dimensions, which can lead to the image appearing squished or stretched if the container's aspect ratio differs from the image's.


Example 2: object-fit: contain

.container {
  width: 200px;
  height: 300px;
  border: 2px solid #ccc;
  background-color: #f0f0f0;
}

.container img {
  width: 100%;
  height: 100%;
  object-fit: contain; /* Scales the image to fit within the container while maintaining its aspect ratio */
}

Explanation The contain value scales the image up or down to fit inside the container while preserving its original aspect ratio. This may result in letterboxing (empty space) if the aspect ratios of the image and container do not match.


Example 3: object-fit: cover

.container {
  width: 200px;
  height: 300px;
  border: 2px solid #ccc;
}

.container img {
  width: 100%;
  height: 100%;
  object-fit: cover; /* Scales the image to cover the entire container, cropping as needed */
}

Explanation With cover, the image is scaled to completely cover the container, also while maintaining its aspect ratio. If the aspect ratios differ, the image will be clipped to fit.


Example 4: object-fit: none

.container {
  width: 200px;
  height: 300px;
  border: 2px solid #ccc;
  overflow: hidden; /* Hides the parts of the image that overflow the container */
}

.container img {
  width: 100%;
  height: 100%;
  object-fit: none; /* Does not resize the image */
}

Explanation The none value does not resize the image at all. The image will be displayed at its original size and may be clipped if it is larger than the container.


Example 5: object-fit: scale-down

.container {
  width: 200px;
  height: 300px;
  border: 2px solid #ccc;
  background-color: #f0f0f0;
}

.container img {
  width: 100%;
  height: 100%;
  object-fit: scale-down; /* Compares 'none' and 'contain' and chooses the smaller size */
}

Explanation scale-down selects the smaller of either the none or contain behavior. This means the image will either be its natural size or scaled down to fit if it's larger than the container, preventing it from ever being scaled up.


Object-Position

The object-position property works in conjunction with object-fit to specify the alignment of the element's content within its container. This is particularly useful when using object-fit: cover or object-fit: contain to control which part of the image is visible.


Example 1: object-position with keywords

.container {
  width: 300px;
  height: 200px;
  border: 2px solid #ccc;
}

.container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: top right; /* Aligns the image to the top right corner of the container */
}

Explanation Using keywords like top, bottom, left, right, and center, you can control the alignment of the image. Here, the image is positioned so that its top right corner is visible within the container.


Example 2: object-position with percentages

.container {
  width: 300px;
  height: 200px;
  border: 2px solid #ccc;
}

.container img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: 25% 75%; /* Positions the image with 25% from the left and 75% from the top */
}

Explanation Percentage values for object-position allow for precise control. A value of 25% 75% aligns the point at 25% horizontally and 75% vertically within the image with the corresponding point in the container.