Background size in CSS

AKASH E

 


Background size in CSS

Introduction

The background-size property allows us to control the size of a background image. We can adjust the image to align, stretch, or fit within the available space. There are various ways to define the background-size property using different syntaxes. This property can be set using values and unit measurements.

For unit values, we can specify the size in percentages or pixels. Additionally, we can use global values to define the size. Here's an example of how to apply it using a global value.

.card-hero {
  background-sizecover, 30%, 300px 250px, inherit;
}

Keyword Values

We can use the keyword values cover and contain to adjust the background size. These keywords allow us to change the size of the background image effectively.

Cover

We can use the cover keyword to set the background size. When the background size is defined as cover, the image will fully cover the container without leaving any empty space. This ensures the image scales to fit the entire screen.

Here’s an example to illustrate this.

Example 1


Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
  body {
    margin0;
    padding0;
    font-familyArial, sans-serif;
    background-color#f0f0f0;
}
  .background-container {
     positionrelative;
     width100vw;
     height100vh;
     background-imageurl("beautiful.jpg");
     background-sizecover;
     background-positioncenter;
     text-aligncenter;
     colorwhite;
}
  .background-container h1 {
     positionabsolute;
     top50%;
     left50%;
     transformtranslate(-50%, -50%);
     font-size2.5rem;
     text-shadow2px 2px 4px rgba(0, 0, 0, 0.7);
}
</style>
</head>
<body>
<div class="background-container">
<h1>Welcome to Nature's Beauty</h1>
</div>
</body>
</html>

Output



Explanation

In the code above, the background-size property is set to cover, ensuring that the image scales appropriately and fills the entire container without leaving any empty spaces.

Contain

We can set the background size using the contain keyword. If the background-size is set to contain, the image will resize completely, ensuring that it remains fully visible. This scaling ensures the image fits within the container without being cropped.

Let's explore this with the example below.

Example 2


Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
  body {
    margin0;
    padding0;
    font-familyArial, sans-serif;
    background-color#f0f0f0;
}
  .background-container {
    positionrelative;
    width100vw;
    height100vh;
    background-imageurl("sunrise.jpg");
     background-sizecontain;
     background-repeatno-repeat;
     background-positioncenter;
     text-aligncenter;
     colorwhite;
}
  .background-container h1 {
   positionabsolute;
   top50%;
   left50%;
   transformtranslate(-50%, -50%);
   font-size2.5rem;
   text-shadow2px 2px 4px rgba(0, 0, 0, 0.7);
}
</style>
</head>
<body>
<div class="background-container">
<h1>Welcome to Tranquil Forest</h1>
</div>
</body>
</html>

Output



Explanation

In the code above, we set the background-size to contain, ensuring the image fits inside the .background-container while keeping its aspect ratio intact. The background-repeat: no-repeat property prevents the image from repeating.

Unit Values

We can also assign a unit value to the background-size property. These unit values can be defined in pixels or percentages. Below are some examples demonstrating how to implement unit values for this property.

.card-hero {  
   background-size15%;  
}  

.card-hero {  
  background-size400px;  
}  

.card-hero {  
  background-size30vw;  
}  

Example 3


Code

<!DOCTYPE html>  
<html lang="en">  
<head>  
<meta charset="UTF-8">  
<meta name="viewport" content="width=device-width, initial-scale=1.0">  
<title>Document</title>  
<style>  
 body {  
   margin0;  
   padding0;  
   font-familyArial, sans-serif;  
   background-color#f0f0f0;  
}  
 .background-container {  
   positionrelative;  
   width100vw;  
   height100vh;  
   background-imageurl("land.jpg");  
   background-sizecontain;  
   background-repeatno-repeat;  
   background-positioncenter;  
   text-aligncenter;  
   colorwhite;  
}  
 .background-container h1 {  
   positionabsolute;  
   top50%;  
   left50%;  
   transformtranslate(-50%, -50%);  
   font-size2.5rem;  
   text-shadow2px 2px 4px rgba(0, 0, 0, 0.7);  
}  
</style>  
</head>  
<body>  
<div class="background-container">  
<h1>Exploring Vast Horizons</h1>  
</div>  
</body>  
</html>

Output



Explanation

In the code above, we can modify the percentage values in the background-size property to manage the image's width and height relative to the container.

Global Values

We can also use some global values in the background-size property, which include the following.

Inherit

Using this keyword, we can change the property of an element to match that of its parent element. In other words, this situation demonstrates the concept of inheritance.

Example 4


Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
  body {
   margin0;
   displayflex;
   justify-contentcenter;
   align-itemscenter;
   min-height100vh;
   background-color#f0f0f0;
}
  .container {
     width300px;
     height200px;
     background-imageurl("img2.jpg");
     background-sizeinheri; /* Inherits the background size from the parent */
     displayflex;
     justify-contentcenter;
     align-itemscenter;
 }
    .box {
     width150px;
      height100px;
      background-colorrgba(255, 0, 0, 0.5);
 }
</style>
</head>
<body>
<div class="container">
<div class="box"></div>
</div>
</body>
</html>

Output



Explanation

In the code above, we've placed a <div> tag inside another <div> tag. The background-size: inherit; property in the .container class makes the background image of the container inherit the size from its parent, which is the body element. The body element has a light gray background color.

Initial

Using this keyword, we can reset the element to its initial value by specifying that value. It also includes some default settings. To clarify, let's look at an example.

Example 5


Code

<!DOCTYPE html>  
<html lang="en">  
<head>  
<meta charset="UTF-8">  
<meta name="viewport" content="width=device-width, initial-scale=1.0">  
<style>  
  body {  
    margin0;  
    padding0;  
    font-familyArial, sans-serif;  
}  
  .hero {  
    positionrelative;  
    height100vh;  
    background-imageurl("lake.jpg");  
    background-sizeinitial; /* Uses the initial size of the image */  
    background-positioncenter;  
    background-repeatno-repeat;  
    colorwhite;  
    text-aligncenter;  
    displayflex;  
    flex-directioncolumn;  
    justify-contentcenter;  
    align-itemscenter;  
  .hero-content {  
     padding20px;  
     background-colorrgba(0, 0, 0, 0.5);  
     border-radius10px;  
}  
    .hero h1 {  
      font-size3rem;  
      margin-bottom10px;  
}  
    .hero p {  
      font-size1.2rem;  
}  
</style>  
<title>Initial Background Size Example</title>  
</head>  
<body>  
<div class="hero">  
<div class="hero-content">  
<h1>Welcome to gocourse</h1>  
<p>Discover Amazing Designs</p>  
</div>  
</div>  
</body>  
</html>  

Output



Explanation

In the code above, we have defined a hero class where the background size is set to initial. This setting keeps the background image at its original size. The content is centered both vertically and horizontally. Additionally, a semi-transparent background and rounded corners are used to enhance the design's elegance.

Unset

The unset property can be used to remove both initial and inherited values. If the property is set to inherit, the unset property will act as inherit. If it isn't set to inherit, then the unset property will act as initial. Let's clarify this with the example below.

Example 6


Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
   body {
     margin0;
     padding0;
     font-familyArial, sans-serif;
     displayflex;
     justify-contentcenter;
     align-itemscenter;
     height100vh;
     background-color: #f0f0f0;
}
   .container {
     width400px;
      height300px;
      background-imageurl("lake.jpg");
            background-sizeunset; /* Reset the background size */
       border-radius10px;
        box-shadow0 4px 8px rgba(0, 0, 0, 0.1);
        overflowhidden;
        positionrelative;
}
    .overlay {
        positionabsolute;
        top0;
        left0;
        width100%;
        height100%;
        background-colorrgba(0, 0, 0, 0.5);
        opacity0.8;
}
    .content {
        positionabsolute;
        top50%;
        left50%;
        transformtranslate(-50%, -50%);
        text-aligncenter;
        color#fff;
}
     .content h1 {
         margin0;
         font-size24px;
}
     .content p {
         margin10px 0;
         font-size16px;
}
</style>
<title>Background Size Example</title>
</head>
<body>
<div class="container">
<div class="overlay"></div>
<div class="content">
<h1>Welcome to gocourse</h1>
<p>Discover a world of creativity and inspiration.</p>
</div>
</div>
</body>
</html>

Output



Explanation

In the code above, the background-size property is set to unset, which means the background image will display at its original size. The design features a centered container that includes an overlay and content. The overlay adds a semi-transparent background to enhance text readability, while the content is positioned at the center of the container.

One value

When only one value is needed in the code, you should use a single value. If you specify one value, the other value will automatically be set to auto. Let’s illustrate this with an example.

Example 7


Code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
   body {
     margin0;
     padding0;
     displayflex;
     justify-contentcenter;
     align-itemscenter;
     min-height100vh;
     background-color#f4f4f4;
     background-imageurl("light.jpg");
       background-size70%; /* Sets the background size */
       background-repeatno-repeat;
       background-positioncenter;
       font-familyArial, sans-serif;
}
   .content {
      text-aligncenter;
      padding2rem;
      background-colorrgba(255, 255, 255, 0.9);
      border-radius10px;
      box-shadow0px 0px 10px rgba(0, 0, 0, 0.1);
}
    h1 {
      color#333;
      font-size2.5rem;
      margin-bottom1rem;
}
    p {
      color#666;
      font-size1.2rem;
}
</style>
<title>Background Size Percentage Example</title>
</head>
<body>
<div class="content">
<h1>Welcome to gocourse</h1>
<p>Where imagination meets design.</p>
</div>
</body>
</html>

Output



Explanation

In the code above, the background-size property is set to 70%, meaning the background image will occupy 70% of the width of the entire webpage while keeping its aspect ratio intact.

Two values

If we assign two values to the background-size property, it will control both the width and height of the background image. The first value specifies the width, and the second value determines the height. Here's an example to illustrate this.

Example 8


Code

<!DOCTYPE html>  
<html lang="en">  
<head>  
<meta charset="UTF-8">  
<meta name="viewport" content="width=device-width, initial-scale=1.0">  
<style>  
  body {  
    margin0;  
    padding0;  
    displayflex;  
    justify-contentcenter;  
    align-itemscenter;  
    min-height100vh;  
    background-color#f4f4f4;  
    background-imageurl("bound.jpg");  
    background-size60% 80%; /* Specifies the width as 60% and the height as 80% */  
    background-repeatno-repeat;  
    background-positioncenter;  
    font-familyArial, sans-serif;  
}  
 .content {  
   text-aligncenter;  
   padding2rem;  
   background-colorrgba(255, 255, 255, 0.9);  
   border-radius10px;  
   box-shadow0px 0px 10px rgba(0, 0, 0, 0.1);  
}  
 h1 {  
  color#333;  
  font-size2.5rem;  
  margin-bottom1rem;  
}  
 p {  
   color: #666;  
   font-size1.2rem;  
}  
</style>  
<title>Background Size Two-Value Example</title>  
</head>  
<body>  
<div class="content">  
<h1>Welcome to gocourse</h1>  
<p>Where creativity knows no bounds.</p>  
</div>  
</body>  
</html>

Output


Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send