Tailwind CSS Background Position
The Tailwind background position utility (bg-{position}) allows you to set the starting point of a background image inside an HTML element.
Tailwind Background Position
The Tailwind background position utility (bg-{position}
) allows you to set the starting point of a background image inside an HTML element. This is especially useful when you want the most important part of an image (like a subject’s face in a portrait or a focal point in a hero section) to appear in a specific part of the container.
How to apply Tailwind Background Position
To apply a specific tailwind background position to an element, you can use the bg-{position}
utility class, where {position}
represents the desired background position. Here are some common values you can use:
bg-top
: The background image is positioned at the top of the element.bg-center
: The background image is positioned at the center of the element.bg-bottom
: The background image is positioned at the bottom of the element.bg-left
: The background image is positioned at the left side of the element.bg-right
: The background image is positioned at the right side of the element. Here's an example:
<div class="bg-top-left"></div><div class="bg-top"></div><div class="bg-top-right"></div>
Preview
Responsive Tailwind Background Position
Tailwind CSS allows you to apply background position classes responsively at different breakpoints. To use responsive tailwind background position classes, you can append the breakpoint prefix to the utility class. For example, md:bg-right
sets the background position to the right starting from the medium breakpoint and above.
<div class="bg-left md:bg-right"> This element has a background image that is positioned at the left by default, but starting from the medium breakpoint and above, it changes to the right side.</div>
In the above example, the tailwind background position of the <div>
element is set to the left by default (bg-left)
, but starting from the medium breakpoint and above, it changes to the right side (md:bg-right)
.
🎯 When and Why to Use Tailwind Background Position
Tailwind background position is great for:
-
Hero sections where the subject must be centered or offset.
-
Card components with image backgrounds that emphasize a specific area.
-
Responsive headers where image focus needs to adapt to layout changes.
-
Portfolios and galleries to show consistent image framing.
By using Tailwind’s utilities, you save time and avoid writing custom CSS
Tailwind Background Position Class Table
Class | Properties | Description |
---|---|---|
bg-bottom | background-position: bottom; | Aligns image to the top center |
bg-center | background-position: center; | Centers the image both ways |
bg-left | background-position: left; | Aligns image to the center left |
bg-left-bottom | background-position: left bottom; | Aligns image to the bottom center |
bg-left-top | background-position: left top; | Top-left alignment |
bg-right | background-position: right; | Aligns image to the center right |
bg-right-bottom | background-position: right bottom; | Bottom-right alignment |
bg-left-bottom | background-position: left bottom; | Bottom-left alignment |
bg-right-top | background-position: right top; | Top-right alignment |
bg-top | background-position: top; | Top alignment |
These classes can be combined with other background-related utilities like bg-cover
, bg-contain
, bg-no-repeat
, and more for advanced styling.
🎨 Combining Background Position with Other Tailwind Utilities
To create polished designs, you’ll often want to combine bg-{position}
with:
-
bg-cover
orbg-contain
(image sizing) -
bg-no-repeat
(avoid repeating the image) -
h-64
,w-full
, oraspect-[ratio]
(set dimensions)
Flexbox utilities like items-center
, justify-center
(for content alignment)
💡 Example: Full Hero Banner
<div class="flex items-center justify-center h-96 bg-[url('/images/hero.jpg')] bg-center bg-cover bg-no-repeat text-white text-3xl font-bold"> Welcome to Our Site</div>
Preview
Welcome to Our Site
This creates a centered, full-width banner with perfectly positioned background and centered content.
Windframe Tailwind blocks
Windframe is a drag and drop builder for rapidly building tailwind css websites and UIs
Start building stunning tailwind UIs!
