Skip to content

Tailwind CSS - Resize

Modernizing Element Resizing with Tailwind CSS

Section titled “Modernizing Element Resizing with Tailwind CSS”

In modern web development, providing users with control over their interface is key to a great user experience. The resize utility in Tailwind CSS allows you to control whether an element, like a <textarea>, can be resized by the user. This tutorial will guide you through using Tailwind’s resize classes, integrating them into a modern development workflow, and applying best practices for responsive and accessible design.

Tailwind provides four primary classes for controlling the resize CSS property. These utilities are intuitive and cover all common use cases.

ClassCSS Properties
resizeresize: both;
resize-xresize: horizontal;
resize-yresize: vertical;
resize-noneresize: none;

Setting Up a Modern Development Environment

Section titled “Setting Up a Modern Development Environment”

While you can use Tailwind via a CDN for quick prototyping, a professional setup involves installing it as a PostCSS plugin. This unlocks Tailwind’s full potential, including customization, and optimizes your production build by purging unused styles. Here’s a quick setup using Vite:

# 1. Create a new project with Vite
$ npm create vite@latest my-tailwind-project -- --template vanilla
$ cd my-tailwind-project
# 2. Install Tailwind CSS dependencies
$ npm install -D tailwindcss postcss autoprefixer
$ npx tailwindcss init -p
# 3. Configure your tailwind.config.js
# Add paths to all of your template files
module.exports = {
content: [
'./index.html',
'./src/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {},
},
plugins: [],
}
# 4. Add the Tailwind directives to your main CSS file (e.g., ./src/style.css)
@tailwind base;
@tailwind components;
@tailwind utilities;
# 5. Start the dev server
$ npm run dev

Let’s apply these utilities in a single, comprehensive example. This form demonstrates all four resize behaviors and includes modern styling for states like focus and hover, along with responsive adjustments.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tailwind CSS Resize Demo</title>
<!-- Assuming a build process links your compiled CSS -->
<link href="/dist/output.css" rel="stylesheet">
</head>
<body class="bg-slate-50 font-sans p-4 md:p-8">
<div class="max-w-4xl mx-auto grid grid-cols-1 md:grid-cols-2 gap-8">
<!-- Card 1: Vertical Resizing -->
<div class="bg-white p-6 rounded-lg shadow-md">
<label for="bio" class="block text-sm font-medium text-slate-700 mb-2">Biography (Vertical Resize)</label>
<textarea
id="bio"
class="resize-y w-full h-24 p-3 border border-slate-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition"
placeholder="You can only resize this field vertically..."></textarea>
<p class="text-xs text-slate-500 mt-2">Useful for long-form text inputs.</p>
</div>
<!-- Card 2: Horizontal Resizing -->
<div class="bg-white p-6 rounded-lg shadow-md">
<label for="tags" class="block text-sm font-medium text-slate-700 mb-2">Tags (Horizontal Resize)</label>
<textarea
id="tags"
class="resize-x w-1/2 h-24 p-3 border border-slate-300 rounded-md focus:ring-2 focus:ring-indigo-500 focus:border-indigo-500 transition"
placeholder="You can only resize this horizontally..."></textarea>
<p class="text-xs text-slate-500 mt-2">Ideal for tag inputs or code snippets where width matters.</p>
</div>
<!-- Card 3: Bidirectional Resizing -->
<div class="bg-white p-6 rounded-lg shadow-md">
<label for="notes" class="block text-sm font-medium text-slate-700 mb-2">Scratchpad (Resizable)</label>
<textarea
id="notes"
class="resize w-full h-24 p-3 border border-slate-300 rounded-md focus:ring-2 focus:ring-emerald-500 focus:border-emerald-500 transition"
placeholder="You can resize this field in any direction..."></textarea>
<p class="text-xs text-slate-500 mt-2">Provides maximum flexibility for user notes.</p>
</div>
<!-- Card 4: No Resizing (Responsive) -->
<div class="bg-white p-6 rounded-lg shadow-md">
<label for="feedback" class="block text-sm font-medium text-slate-700 mb-2">Feedback (Resize Disabled on Mobile)</label>
<!-- On small screens, it's resizable. On medium screens and up, it's not. -->
<textarea
id="feedback"
class="resize md:resize-none w-full h-24 p-3 border border-slate-300 rounded-md focus:ring-2 focus:ring-rose-500 focus:border-rose-500 transition"
placeholder="Resizable on small screens only..."></textarea>
<p class="text-xs text-slate-500 mt-2">Use responsive prefixes like `md:resize-none` for adaptive UIs.</p>
</div>
</div>
</body>
</html>

A common issue developers face is resize not working on an element. Remember these key points:

  • Overflow Property: The resize property only works on elements with an overflow value other than visible. This is why it works out-of-the-box on <textarea> elements, which have overflow: auto by default.
  • Block-level Elements: It is typically applied to block-level elements. Applying it to an inline element may not produce the desired effect without changing its display property.
  • Browser Support: While widely supported, always check caniuse.com for specific browser compatibility if you are targeting older platforms.

Accessibility (a11y) and UX Considerations

Section titled “Accessibility (a11y) and UX Considerations”

While resizable elements enhance user experience, ensure your design is accessible:

  • Provide Adequate Default Size: The initial size of a resizable element should be large enough to be functional without requiring immediate resizing.
  • Use Labels: Always pair form elements like <textarea> with a <label> for screen reader users. The for attribute on the label must match the id of the textarea.
  • Responsive Resizing: As shown in the example, consider disabling resizing on smaller screens where it might be cumbersome or break the layout. The md:resize-none utility is perfect for this.