Skip to content

Tailwind CSS - Background Position

Tailwind’s background-position utilities, prefixed with bg-*, allow you to control the position of an element’s background image. This is fundamental for creating well-composed hero sections, cards, and other components where the framing of an image matters.

These classes set the background-position CSS property. Combine them with background sizing utilities like bg-cover or bg-contain for the best results.

ClassCSS PropertiesEffect
bg-bottombackground-position: bottom;Aligns the image to the bottom edge.
bg-centerbackground-position: center;Centers the image horizontally and vertically.
bg-leftbackground-position: left;Aligns the image to the left edge.
bg-left-bottombackground-position: left bottom;Aligns the image to the bottom-left corner.
bg-left-topbackground-position: left top;Aligns the image to the top-left corner.
bg-rightbackground-position: right;Aligns the image to the right edge.
bg-right-bottombackground-position: right bottom;Aligns the image to the bottom-right corner.
bg-right-topbackground-position: right top;Aligns the image to the top-right corner.
bg-topbackground-position: top;Aligns the image to the top edge.

Practical Application: Responsive Hero Component

Section titled “Practical Application: Responsive Hero Component”

Imagine a hero image with a person on the right side. On a wide desktop screen, you might want to center the image. On a narrow mobile screen, you’d want to shift the focus to the right to keep the person in the frame. This is a perfect use case for responsive background position utilities.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Background Position</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Using a CSS variable for the background image for clarity */
.hero-bg {
background-image: url('https://images.unsplash.com/photo-1557862921-37829c790f19?q=80&w=2071&auto=format&fit=crop');
}
</style>
</head>
<body class="bg-gray-100 p-8">
<h2 class="text-2xl font-bold mb-4 text-center">Responsive Hero Section</h2>
<p class="text-center mb-6 text-gray-600">Resize your browser to see the background position change.</p>
<div
class="hero-bg h-96 w-full rounded-lg shadow-xl
bg-cover bg-no-repeat
bg-center
sm:bg-right-top"
>
<div class="h-full w-full bg-black bg-opacity-30 flex items-end p-8 rounded-lg">
<h1 class="text-4xl font-bold text-white">Modern Web Development</h1>
</div>
</div>
<div class="mt-4 p-4 bg-blue-100 border-l-4 border-blue-500 text-blue-700">
<p class="font-bold">Explanation</p>
<p>On small screens (mobile-first), `bg-center` is applied. On screens `sm` (640px) and wider, `sm:bg-right-top` overrides it, keeping the subject of the image in view.</p>
</div>
</body>
</html>

Modern Tailwind’s JIT (Just-In-Time) compiler lets you use arbitrary values for ultimate control. If the standard utilities aren’t precise enough, you can specify the exact position in square brackets.

<!-- Position the background 50% from the left and 2rem from the top -->
<div class="bg-[50%_2rem] ..."></div>
<!-- Use keywords and values -->
<div class="bg-[center_top_1rem] ..."></div>
  • Combine with Sizing: background-position is most effective when used with bg-cover or bg-contain to manage how the image scales within its container.
  • Mobile-First Approach: Define your base background position for mobile and use responsive prefixes (sm:, md:, lg:) to adjust it for larger screens.
  • Consider the Subject: Choose a position that keeps the most important part of your image visible across different device sizes.