Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Properties

CSS background-position Property

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

Photo Credit to CodeToFun

🙋 Introduction

The background-position property in CSS is used to set the initial position of a background image within its containing element.

This property allows you to control where the image is placed, whether it's in the center, top-left, bottom-right, or any other specified position. By using background-position, you can ensure that your background images appear exactly where you want them, enhancing the visual layout of your web page.

💡 Syntax

The syntax for the background-position property allows for various values, including keywords, length, and percentage values.

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

🎛️ Default Value

The default value of the background-position property is 0% 0%, which means the background image is positioned at the top-left corner of the element.

🏠 Property Values

ValueDescription
keywordstop, bottom, left, right, center. These values position the image based on the specified direction.
lengthSpecifies the position using length units like px, em, etc. For example, 50px 100px positions the image 50 pixels from the left and 100 pixels from the top.
percentageSpecifies the position as a percentage of the element's dimensions. For example, 50% 50% centers the image both horizontally and vertically.
combinationYou can combine these values, liketop 50%, which positions the image at the top and centered horizontally.

📄 Example

In this example, we'll position the background image at the center of the element.

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-position Example</title>
  <style>
    .background-example {
      width: 300px;
      height: 200px;
      background-image: url('example.jpg');
      background-position: center;
      background-size: cover;
    }
  </style>
</head>
<body>
  <h1>Background Position Center</h1>
  <div class="background-example"></div>
</body>
</html>

🖥️ Browser Compatibility

The background-position property is widely supported in all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. This ensures that your background images will be positioned correctly across different platforms.

🎉 Conclusion

The background-position property is an essential tool for web developers looking to control the placement of background images.

Whether you're aligning an image to the center or positioning it at specific coordinates, this property provides the flexibility needed to create visually appealing designs. Experiment with different values to see how this property can enhance your web layouts.

👨‍💻 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