Skip to content

Tailwind CSS - Object Position

# Modern Guide to Tailwind CSS: Object Position

Section titled “# Modern Guide to Tailwind CSS: Object Position”

Welcome to your updated guide on Tailwind CSS’s object-position utilities. These classes control how a replaced element’s content, like an <img> or <video>, is positioned within its container. This is especially powerful when combined with object-fit for creating visually compelling, responsive layouts.

## Core Concept: Art Direction with Object Position

Section titled “## Core Concept: Art Direction with Object Position”

Imagine you have a wide banner image with a person on the far left. On a wide desktop screen, this looks great. On a narrow mobile screen, the person might get cropped out if the image is centered. object-position utilities let you solve this by telling the browser to focus on a specific part of the image, a technique known as art direction.

While you can use a CDN for quick demos, a professional setup involves installing Tailwind CSS as a PostCSS plugin. This unlocks the full power of Tailwind, including customization and performance optimizations.

Terminal window
# 1. Install Tailwind CSS and its peer dependencies
npm install -D tailwindcss postcss autoprefixer
# 2. Generate your config files
npx tailwindcss init -p

This creates tailwind.config.js and postcss.config.js, which are essential for any real-world project. For the examples below, we’ll stick to a simple CDN setup for clarity, but we encourage you to use the standard installation.

Here is a summary of the available object-position utility classes. They are highly intuitive and correspond directly to the CSS object-position property.

ClassCSS Properties
object-bottomobject-position: bottom;
object-centerobject-position: center;
object-leftobject-position: left;
object-left-bottomobject-position: left bottom;
object-left-topobject-position: left top;
object-rightobject-position: right;
object-right-bottomobject-position: right bottom;
object-right-topobject-position: right top;
object-topobject-position: top;

## Comprehensive Example: Responsive Art Direction

Section titled “## Comprehensive Example: Responsive Art Direction”

Let’s see these utilities in action. In this example, we have a container for a profile image. We’ll use object-cover to make the image fill the container, and then use responsive object-position classes to keep the subject in focus across different screen sizes.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Modern Object Position Example</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-100 dark:bg-slate-900 p-8 flex flex-col items-center font-sans">
<h1 class="text-3xl font-bold text-slate-900 dark:text-white mb-6">Responsive Art Direction</h1>
<div class="w-full max-w-md mx-auto bg-white dark:bg-slate-800 rounded-lg shadow-lg p-6">
<h2 class="text-xl font-semibold text-slate-800 dark:text-slate-200 mb-4">Profile Card</h2>
<p class="text-slate-600 dark:text-slate-400 mb-4">Resize your browser to see the image focus change. On small screens, it focuses on the right; on larger screens, it centers.</p>
<!-- The key is here: object-cover makes the image fill the box,
and object-right keeps the subject in view on small screens.
On medium screens and up (md:), we switch to object-center. -->
<img
class="w-full h-64 bg-gray-300 rounded-md object-cover object-right md:object-center"
src="https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?q=80&w=1887&auto=format&fit=crop"
alt="A portrait of a smiling man, used for demonstrating object-position."
/>
</div>
<div class="mt-8 text-center text-slate-500">
<p>Photo by <a href="https://unsplash.com/@shahin_khalaji" class="underline hover:text-indigo-500">Shahin Khalaji</a> on Unsplash</p>
</div>
</body>
</html>
  • object-position has no effect? These utilities only work on replaced elements (like <img>, <video>, <object>, <svg>) and only when an object-fit utility (e.g., object-cover or object-contain) is also applied. Without object-fit, the image will simply have its default dimensions and position, and there will be nothing to reposition within the container’s bounds.
  • Check your dimensions: Ensure the container element has a defined width and height (w-*, h-*). If the container collapses to the size of the image, there’s no extra space for the positioning to become apparent.
  • Mobile First: Design your layout for mobile screens first. Use default utilities like object-right for the mobile view, then use responsive prefixes like md:object-center to adjust for larger screens.
  • Accessibility: Always provide a descriptive alt attribute for images. This is crucial for users who rely on screen readers and is independent of visual styling.
  • Combine with Hover Effects: You can change the object position on hover for subtle interactive effects, e.g., hover:object-bottom.