The Wayback Machine - https://web.archive.org/web/20241123005834/https://www.geeksforgeeks.org/how-to-fill-an-image-size-without-stretching-in-css/
Open In App

How to fill a Box with an Image Without Stretching in CSS?

Last Updated : 17 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The CSS object-fit property is used to fill an image in a container without stretching preserving its aspect ratio.

CSS object-fit Property

Create an image container and specify the width and height. Set the object-fit: cover property and overflow: hidden property to maintain the image's aspect ratio and fill the container without stretching the image.

Syntax

object-fit: cover;
overflow: hidden;
HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <title>Image Without Stretch</title>
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
        }

        h2 {
            text-align: center;
            margin: 5px;
        }

        .image-container {
            display: grid;
            gap: 20px;
        }

        .original-image,
        .stretched-image {
            border: 2px solid rgb(255, 0, 221);
            overflow: hidden;
        }


        .filled-image img {
            object-fit: cover;
            border: 2px solid blue;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="image-container">
            <h2>Original Image</h2>
            <div class="original-image">
                <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231027155426/Best-Front-End-Development-Courses.png"
                    alt="Original Image" />
            </div>
            <h2>Filled Image without Stretch</h2>
            <div class="filled-image">
                <img src=
"https://media.geeksforgeeks.org/wp-content/uploads/20231027155426/Best-Front-End-Development-Courses.png"
                    alt="Stretched Image" />
            </div>
        </div>
    </div>
</body>

</html>

Output

output
fill a box withan image without stretching

Previous Article
Next Article

Similar Reads

How to Control the Size of an Image without Stretching in CSS ?
Controlling the size of an image while maintaining its aspect ratio is a common challenge in web design. Directly setting an image's width and height can lead to stretching or squashing, distorting the original aspect ratio. Fortunately, CSS offers several techniques to resize images without distortion. This article explores various approaches to a
2 min read
How to Fill a Box with an Image Without Distorting in CSS ?
Filling a box with an image without distorting it can be a common requirement in web design. The goal is to ensure that the image covers the entire box while maintaining its aspect ratio. In this article, we will explore two methods to achieve this using CSS. Example 1: Using background-size: cover PropertyThe background-size: cover property is a s
2 min read
How to Prevent Flex Items from Stretching in CSS?
The Flexbox is a powerful tool for creating flexible and responsive web layouts. By default, flex items are stretched to the fill the container. However, there are scenarios where we might want to prevent this stretching behavior. These are the following approaches to prevent flex items from stretching in CSS: Table of Content Using align-items Pro
4 min read
How to Modify the Fill Color of an SVG Image when being Served as Background Image ?
In this article, we will see how to modify the fill color of an SVG image when being served as a background image. SVGs also known as scalable vector images, While being served as a background image we can change their background color easily. It has a number of approaches like using HTML only or using CSS or JavaScript. For starters let's look at
5 min read
How to Get divs to Fill up 100% of the Container Width without Wrapping in CSS Flexbox?
Creating a layout where &lt;<div>&gt; elements fill the full width of their container without wrapping can be a little complex in web design. CSS flexbox makes this easier by allowing us to control the alignment and sizing of elements. In this article, we are going to discuss how to use Flexbox to achieve this layout. Flexbox is a pow
3 min read
Difference between -webkit-box-shadow & box-shadow in CSS
In this article, we will find difference between 'webkit-box-shadow' & 'box-shadow' in CSS. Webkit-box-shadow: The webkit-box-shadow property in CSS is used to apply a box-shadow. The webkit-box-shadow is a browser-specific implementation for WebKit browsers like Google Chrome and Safari. Syntax: webkit-box-shadow: h-offset v-offset blur; Advan
1 min read
How to Drop fill color to change the image color using HTML and CSS ?
In this article, we are going to create a Drop-fill color effect using HTML & CSS in which the image gets colored when we hover over it. It appears when you hover over the image a drop of color falls over the image which changes the color of the image from gray to colored. Approach: The following approach will be implemented to Drop fill color
4 min read
How to change Input box borders after filling the box using JavaScript ?
In this article, we will change the input border after filling in the text on the input text field. The onchange event attribute works when the value of the element changes and selects the new value from the list. Approach: Whenever the input value is changed with some value by the user, the onchange event attribute is triggered. After the event is
2 min read
Difference between alert box and prompt box in JavaScript
1. Alert box : An alert box is one type of popup boxes in JavaScript which is often used to make sure that information have come through the user. So, the user will have to click "OK" to proceed when an alert box pops up on the window. Syntax : window.alert("message"); Another syntax to use alert box is : alert("message"); 2. Prompt box : It is als
2 min read
How is border-box different from content-box ?
CSS (Cascading Style Sheets) is a stylesheet language used to design a webpage to make it attractive. The reason for using this is to simplify the process of making web pages presentable. It allows you to apply styles on web pages. More importantly, it enables you to do this independent of the HTML that makes up each web page. In this article, we w
3 min read
Which property specifies the distance between nearest border edges of marker box and principal box ?
In this article, we will see the property that specifies the distance between the nearest border edges of the marker box and the principal axis. The marker-offset is the property that specifies the distance between the nearest border edges of the marker box and the principal axis. Syntax: style=" marker-offset:em/cm"; Example 1: In this example, we
2 min read
How Create a Text Box Where User Can't Type Inside the Text Box ?
To create a text box where the user can't type inside the text box, we have different approaches. In this article, we are going to learn how to create a text box where the user can't type inside the text box in JavaScript. Below are the approaches to creating a text box where the user can't type inside the text box: Table of Content Using Readonly
3 min read
Difference between alert box and confirmation box in JavaScript
In JavaScript, alert and confirm are two built-in functions used to interact with users via dialogue boxes. Both serve different purposes and provide distinct functionalities. Understanding the difference between an alert box and a confirmation box is crucial for effective user interface design and user interaction management. Table of Content Aler
3 min read
How to put images in a box without space using Bootstrap?
Bootstrap 4 is a CSS a library for frontend web development developed by Twitter which helps the developer to beautify the way the webpage looks. The bootstrap library has pre-defined CSS classes that are easy to use and it helps the developer to provide an artistic look to the website with minimal effort. The following line shows the syntax for in
3 min read
How to display error without alert box using JavaScript ?
JavaScript errors are displayed using alert boxes, which can interrupt user experience and feel outdated. But Now, there are many alternatives to alert boxes for displaying error messages in a more user-friendly way. This article covers different methods to display errors dynamically without using alert boxes, focusing on textContent and innerHTML
3 min read
CSS fill-opacity Property
The fill-opacity property is used to set the opacity of the paint server that is applied to the shape. basically is used to set the opacity level of the fill color of an SVG shape. It determines the transparency of the fill, with a value between 0 and 1, where 0 represents completely transparent and 1 represents completely opaque. Syntax: fill-opac
3 min read
How to Make Empty grid-template-row to Fill Remaining Space in CSS ?
In CSS grid, sometimes we want to create a grid layout where certain rows or columns take up a fixed amount of space and the remaining space is divided among the other rows or columns. In this article, we will see how to create the empty grid-template-row in order to fill up the remaining space using CSS. We can achieve this by using the CSS grid-t
2 min read
Tailwind CSS Fill
This class accepts lots of value in tailwind CSS in which all the properties are covered in class form. This class is a presentation attribute used to set the color of an SVG shape. In CSS, we have done that by using the CSS fill property. FIll class: fill-current: This class is used to set the fill color of an SVG to the current text color. This m
1 min read
What are CSS columns and how to fill it ?
CSS columns: We can break the content of an element into columns, called CSS columns. We can fill it using the column-fill property. The column-fill property control how the content of the element will break into columns. It takes the values auto, balance, inherit, and initial. Syntax: column-fill: balance|auto|initial|inherit; This example shows h
3 min read
How to Work with Fill & Stroke Color in CSS ?
Fill color is the interior color of an element and the Stroke Color is the color that is applied to the outline of the element. we will work with this fill and stroke color in CSS on various SVG shapes. These are the following approaches: Table of Content Using Different Fill and Stroke Colors on SVG ShapesUsing Dynamic fill and stroke colorsUsing
3 min read
Auto-Fit vs Auto-Fill Property in CSS Grid
CSS Grid is a powerful layout system that allows you to create complex, responsive layouts with ease. One of its key features is the ability to create a responsive layout without using media queries. Instead, you can use the grid-template-columns property, the repeat() function, and the auto-fit and auto-fill keywords to adjust the layout based on
4 min read
CSS animation-fill-mode Property
The animation-fill-mode property in CSS is used to define the styles that an animation applies to an element before and after it executes. By default, CSS animations do not affect an element until the first keyframe is played or after the last keyframe is played. This property can override that behavior, allowing for more control over the animation
3 min read
CSS column-fill Property
The column-fill property in CSS is used to specify whether the columns will be filled in a balanced manner or not. Syntax: column-fill: auto|balance|balance-all|initial|inherit;Default Value: balance Property Values: balance: This is the default value for the property. This value fills each column with the same amount of content, but will not allow
3 min read
How to make div fill full height of parent in Tailwind CSS ?
Making a div fill the full height of its parent in Tailwind CSS involves using the utility class h-full. Applying h-full to the div ensures it stretches to match the parent element’s height, providing a responsive and consistent layout within the container. Table of Content Using the "h-full" classUsing FlexboxApproach 1: Using the "h-full" classTh
3 min read
How to fill up the rest of screen height using Tailwind CSS ?
In Tailwind CSS, filling the rest of the screen height is a common task for creating responsive layouts. Tailwind offers utility classes that make it easy to control element sizing and layout, allowing developers to expand elements dynamically to fit the available space. Approach: Using the Flex Class and Height Class In Tailwind CSS, the flex clas
2 min read
CSS fill Property
The CSS fill property is used to define the color inside the graphical elements of an SVG (Scalable Vector Graphics). It specifies the fill color for shapes such as circles, paths, and rectangles. Syntaxfill: <paint>Property Valuespaint: It is used to set the color of the fill property. This paint be defined using color names, hex, rgb or hsl
3 min read
How to create text-fill animation using CSS ?
A text-fill animation using CSS is a visual effect where the content of text gradually fills with a color, gradient, or pattern over time. This animation can create dynamic, eye-catching effects for headings or titles by adjusting properties like background-size, clip-path, or keyframe animations. Approach: Position Text: Use absolute positioning t
2 min read
How to Fill the Height of the Viewport with Tailwind CSS ?
Filling the height of the viewport with Tailwind CSS refers to creating a layout where an element occupies the full vertical space of the browser window. This approach is essential for building responsive designs that adapt to various screen sizes, enhancing user experience. Here we have some common approaches to fill the height of the viewport wit
2 min read
How To Make A Div Fill The Remaining Horizontal Space Using CSS?
To make a div fill the remaining horizontal space in a container using CSS, you can use different techniques to achieve a flexible and responsive layout. This is useful when designing layouts where some elements occupy a fixed space, while others expand to fill the rest of the available space. 1. Using FlexboxFlexbox is a layout model that makes it
3 min read
How to Create an Image Overlay Fade in Box Effect on Hover ?
The image overlay fade in box effect on hover is a visual effect where a box fades in over an image when the user hovers their mouse over the image. It can be used to display additional information about the image or simply add a stylish touch to your website. ApproachCreate an HTML document and create the image container element with the class ima
2 min read
three90RightbarBannerImg