Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS background-size Property

Posted in CSS Tutorial
Updated on Oct 01, 2024
By Mari Selvan
👁️ 20 - Views
⏳ 4 mins
💬 1 Comment
CSS background-size Property

Photo Credit to CodeToFun

🙋 Introduction

The background-size property in CSS allows you to control the size of background images.

This property is essential for creating responsive designs, ensuring that background images look good on all devices and screen sizes. By adjusting the size of the background image, you can make sure it fits the desired area without stretching, tiling, or being cut off.

💡 Syntax

The syntax for the background-size property is straightforward. You can specify one or two values, keywords, or the auto keyword.

Syntax
Copied
Copy To Clipboard
element {
  background-size: value;
}

🎛️ Default Value

The default value for the background-size property is auto, which means the background image will retain its original size.

🏠 Property Values

ValueDescription
autoThe background image retains its original size.
lengthSpecifies the width and height using units like px, em, etc. (e.g., 100px 200px).
percentageSpecifies the width and height as a percentage of the element's dimensions (e.g., 50% 50%).
coverScales the background image to cover the entire container, potentially cropping the image to maintain its aspect ratio.
containScales the background image to be fully visible within the container, ensuring that the entire image fits without cropping.

📄 Example

In this example, we'll demonstrate different values of the background-size property.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS background-size Example</title>
  <style>
    .container {
      width: 300px;
      height: 200px;
      background-image: url('https://via.placeholder.com/600x400');
      background-repeat: no-repeat;
      border: 1px solid #000;
      margin-bottom: 20px;
    }

    .auto {
      background-size: auto;
    }

    .cover {
      background-size: cover;
    }

    .contain {
      background-size: contain;
    }
  </style>
</head>
<body>
  <h1>Background-Size Examples</h1>
  <div class="container auto">Background Size: Auto</div>
  <div class="container cover">Background Size: Cover</div>
  <div class="container contain">Background Size: Contain</div>
</body>
</html>

🖥️ Browser Compatibility

The background-size property is supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is also supported in older versions of Internet Explorer starting from IE9. It's recommended to test your site across different browsers to ensure consistent behavior.

🎉 Conclusion

The background-size property is a versatile tool for managing background images in CSS. Whether you're aiming for a fully responsive design or just want to control how an image displays within a specific element, this property provides the flexibility you need. Experiment with different values to see how they can enhance the visual appeal of your web projects.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy