You’re building a React app for a client who wants a button that changes color when hovered over. You decide to use Tailwind CSS for rapid styling. Here’s the button code:
<button className="bg-blue-500 text-white px-4 py-2 rounded hover:bg-blue-700">Submit</button>
What does the hover:bg-blue-700 class do, and why might Tailwind be a better choice here than inline styles?
hover:bg-blue-700
It changes the button’s background color to a darker blue when clicked, and Tailwind avoids JavaScript for hover effects.
It changes the button’s background color to a darker blue when hovered over, and Tailwind supports hover states while inline styles don’t.
It changes the text color to blue on hover, and Tailwind ensures unique class names like CSS Modules.
It adds a blue border on hover, and Tailwind is faster to write than a separate CSS file.
It increases padding on hover, and Tailwind is more responsive than inline styles.
Imagine you’re working on a team project where two developers accidentally use a .header class in separate CSS files:
.header
/* styles1.css */ .header { color: red; } /* styles2.css */ .header { color: blue; }
You import both files into your React app with <link> tags in index.html. Your component looks like this:
<link>
index.html
function Title() { return <h1 className="header">Welcome</h1>; }
What problem arises, and how could CSS Modules fix it?
The text color will be unpredictable due to a naming collision, and CSS Modules create unique class names to avoid conflicts.
The hover effect won’t apply, and CSS Modules add automatic hover support.
The className won’t work because class is reserved, and CSS Modules fix this by using styleName.
className
class
styleName
The text will be red because styles1.css overrides styles2.css, and CSS Modules prevent this by scoping styles to components.
styles1.css
styles2.css
You’re tasked with styling a reusable card component in React. Here’s an attempt using inline styles:
const cardStyle = { backgroundColor: '#f0f0f0', padding: '16px', borderRadius: '8px' }; function Card({ title }) { return <div style={cardStyle}><h2>{title}</h2></div>; }
A teammate suggests switching to Tailwind CSS with this:
function Card({ title }) { return <div className="bg-gray-100 p-4 rounded-lg"><h2>{title}</h2></div>; }
What’s a key limitation of inline styles here, and why might Tailwind be more practical?
Inline styles can’t reuse styles across components, while Tailwind’s classes are globally available.
Inline styles can’t handle hover effects, while Tailwind can with hover:bg-gray-200.
hover:bg-gray-200
Inline styles require camelCase, making them harder to read than Tailwind’s hyphenated classes.
Inline styles don’t support responsive design, while Tailwind offers breakpoints like md:p-4.
md:p-4
You’re debugging a React app where a developer used a CSS file:
/* styles.css */ .code { font-family: monospace; color: #333; }
Imported in app.tsx like this:
app.tsx
import "./styles.css"; function CodeBlock() { return <p className="code">console.log("Hello")</p>; }
Meanwhile, a third-party library also defines a .code class with color: green. What happens, and how could CSS Modules prevent this?
.code
color: green
The font-family won’t apply due to a conflict, and CSS Modules fix this by renaming className.
font-family
Nothing happens because className is invalid, and CSS Modules use styleName instead.
The text will be green because third-party styles override yours, and CSS Modules scope styles locally with unique names.
The text will be #333 because your import is closer, and CSS Modules add hover support.
#333
You’re designing a responsive landing page in React with Tailwind CSS. Here’s a section:
<div className="w-full sm:w-3/4 md:w-1/2 lg:w-1/3 bg-blue-200 p-6"> <h1 className="text-xl md:text-2xl font-bold">Welcome</h1> </div>
A client asks why the design adjusts across screen sizes. What’s happening in the code, and why is this useful?
p-6 adds padding that scales, and Tailwind avoids inline styles for better control.
p-6
md:text-2xl changes the font size on medium screens, and responsive design improves usability on different devices.
md:text-2xl
lg:w-1/3 shrinks the div on large screens, and Tailwind’s breakpoints make styling faster than media queries.
lg:w-1/3
w-full sm:w-3/4 sets a fixed width, and Tailwind ensures consistency across browsers.
w-full sm:w-3/4
This content is only available to registered users.Please register at the the home page.