Use CSS to scale background images to fit the window
This article will teach you how to scale and stretch background images using CSS. Sometimes, we need to scale a background image based on the viewport or window size of the screen, and that’s what we’ll do in this article.
Use the CSS background-size property to scale the background image to fit the window
Programmers can use the background-size CSS property to manage the size of the background image. If we use cover as the value of the CSS background-size property, we can stretch the background image according to the user's window size.
For example, we created <h1>
the element and added some text to be displayed. Additionally, we used the CSS background property to set a background image to the entire body.
We have passed 4 different values to the background property. The first value represents the URL of the background image.
no-repeat means that the background image should not be repeated and should only be displayed once on the screen. The center value means that the background image should be in the center of the screen, and fixed means that the background image is fixed in size and does not allow scrolling.
Additionally, we used background-size: cover
the property in CSS, which allows us to scale the image based on the size of the body element.
HTML code:
<h1 class="text">This is the demo text.</h1>
CSS code:
body {
background: url(https://pbs.twimg.com/profile_images/959568688654553089/PuZWi9tj_400x400.jpg) no-repeat center fixed;
background-size: cover;
}
Output:
In the above output image, user can see that the image is stretched and a single image serves as the background of the web page.
We can also use 100% as the value of the CSS background-size property to fit the background image according to the viewport of the user's screen, as shown in the following example.
CSS code:
body {
background: url(https://img.test.com/profile_images/959568688654553089/PuZWi9tj_400x400.jpg) no-repeat center fixed;
background-size: 100%;
}
<h1 class="text">This is the demo text.</h1>
Output:
We have learned only two different ways to scale background images using CSS in this article. Users can choose any of the ways based on their comfort.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Overriding Bootstrap CSS
Publish Date:2025/04/19 Views:80 Category:CSS
-
This article introduces the famous CSS UI framework Bootstrap. We will discuss the process of overriding Bootstrap CSS with custom styles. Bootstrap Overview Using Bootstrap CSS makes it quicker and easier to create responsive websites than
Applying multiple transformations in CSS
Publish Date:2025/04/19 Views:143 Category:CSS
-
CSS transforms are a powerful but underutilized way to add animation effects to elements. However, it can be tricky to get the desired results when applying multiple transformations at once; for example, we want elements to transform in syn
Styling input and submit buttons using CSS
Publish Date:2025/04/19 Views:193 Category:CSS
-
A submit button is a button used to submit data into a form. It is usually placed at the end of the form after all other fields are filled in. When you click the submit button, the form is transferred to the server for processing. The synta
Creating background image gradients using CSS
Publish Date:2025/04/19 Views:164 Category:CSS
-
A background image gradient is a gradual transition from one color to another. Background image gradients can be created using multiple colors, but they are usually most effective when using a limited color palette. This article will teach
Stretchable background images using CSS
Publish Date:2025/04/19 Views:82 Category:CSS
-
This short article is about styling HTML pages using CSS, focusing on styling background images of any HTML element. CSS styles on the web page There are a number of ways to use CSS on your web pages: Inline CSS: Inline CSS means that you u
Gradient backgrounds in CSS
Publish Date:2025/04/19 Views:148 Category:CSS
-
This simple guide is about the use of CSS properties that can be used to define a rainbow-like gradient background for an HTML element. CSS Gradients We can use CSS gradients to display a seamless transition between two or more specified co
First Of Class in CSS
Publish Date:2025/04/19 Views:158 Category:CSS
-
CSS :first-of-type selectors are used to select the first element of its type within a group of elements. For example, if you have a group of paragraphs, :first-of-type the selector will select the first paragraph in the group. This selecto
Bringing an element to the front using CSS
Publish Date:2025/04/19 Views:111 Category:CSS
-
If an element is not set to cover the entire display area, you can z-index bring it to the front using the property. z-index The property determines the stacking order of elements on the page. z-index The higher element is stacked z-index o
Changing the color of SVG elements in HTML using CSS
Publish Date:2025/04/19 Views:83 Category:CSS
-
This simple guide is all about using SVG in an HTML document and how we can customize it to change its color using CSS properties. First, we will take a brief look at SVG in HTML. Introduction to SVG Similar to XHTML, SVG is an XML language