
Background Color Image Position in TailwindCSS
? Background Color, Image, and Position in Tailwind CSS
Tailwind provides utilities to style the background of elements including color, image, and positioning.
1. Background Color (bg-{color}
)
Set background colors from Tailwind’s palette.
Class | CSS Equivalent | Description |
---|---|---|
bg-white | background-color: #fff; | White background |
bg-black | background-color: #000; | Black background |
bg-gray-500 | background-color: #6b7280; | Gray shade background |
bg-red-600 | background-color: #dc2626; | Red background |
bg-blue-400 | background-color: #60a5fa; | Blue background |
... | ... | Many colors available |
You can also use arbitrary colors:
<div class="bg-[#ff5733] p-4"> Custom orange background</div>
2. Background Image (bg-[url('...')]
or bg-none
)
Set custom background image:
Use arbitrary values to set any image URL:
<div class="bg-[url('https://example.com/image.jpg')] bg-no-repeat bg-center bg-cover h-64 w-full"> <!-- Content here --></div>
bg-no-repeat
— Prevents image repetition.bg-repeat
— Default, repeats background.bg-repeat-x
orbg-repeat-y
— Repeat horizontally or vertically.
Remove background image:
<div class="bg-none"> No background image here.</div>
3. Background Position (bg-{position}
)
Controls the starting position of the background image.
Class | CSS Equivalent | Description |
---|---|---|
bg-bottom | background-position: bottom; | Position at bottom |
bg-center | background-position: center; | Center background |
bg-left | background-position: left; | Position at left |
bg-left-bottom | background-position: left bottom; | Bottom-left position |
bg-left-top | background-position: left top; | Top-left position |
bg-right | background-position: right; | Position at right |
bg-right-bottom | background-position: right bottom; | Bottom-right position |
bg-right-top | background-position: right top; | Top-right position |
bg-top | background-position: top; | Position at top |
Bonus: Background Size Utilities
Class | CSS Equivalent |
---|---|
bg-auto | background-size: auto; |
bg-cover | background-size: cover; (fills container) |
bg-contain | background-size: contain; (fit inside container) |
Example:
<div class="bg-blue-500 bg-center bg-cover h-64 w-full" style="background-image: url('https://example.com/bg.jpg');"> <!-- Content --></div><!-- Or using Tailwind arbitrary URL syntax --><div class="bg-[url('https://example.com/bg.jpg')] bg-no-repeat bg-center bg-cover h-64 w-full"> <!-- Content --></div>
Want me to show you how to combine these or add responsive background utilities?