Starting from scratch: Building a modern responsive webpage using Tailwind CSS

3-minute read
2026-03-14
2,695
I earn commissions when you shop through the links below, at no additional cost to you.

In the web industry, where efficient development is highly valued today, CSS frameworks that prioritize practicality are gradually becoming the mainstream choice. Among them,Tailwind CSS It stands out thanks to its unique “atomization” philosophy. Instead of providing pre-made components, it offers a comprehensive set of finely granular, freely combinable utility classes that allow developers to quickly build any desired design directly in HTML. Say goodbye to the era of spending hours pondering class names!Tailwind CSS Encouraging the use of combinations to create styles greatly enhances the flexibility and consistency of UI development, making it a powerful tool in modern responsive web design.

Understanding the core advantages of Tailwind CSS

Traditional CSS frameworks such as Bootstrap provide features like… .btn.card Such pre-built components, although ready to use out of the box, often require a large amount of styling adjustments when used in highly customized designs. This leads to bloated code and an excessive number of specific style rules (what is commonly referred to as a “war of specificity” in software development).Tailwind CSS A completely different philosophy was adopted: the creation of atomic classes with a single responsibility.

Practical and prioritized atomic classes

Tailwind CSS The class names directly correspond to a single CSS property. For example,.mt-4 Corresponding to margin-top: 1rem;.text-blue-500 The corresponding blue color value in hexadecimal format. This design approach means that you don’t need to switch back and forth between HTML and CSS files, and you hardly have to write any custom CSS code at all. All style decisions are made at the template level, which makes the prototyping and iteration process extremely fast.

Recommended Reading From Beginner to Expert: The Ultimate Guide to Improving Front-End Development Efficiency with Tailwind CSS

High degree of customization and design consistency

Through the files located in the project's root directory. tailwind.config.js With the configuration file, you have complete control. Tailwind The design system allows you to define your color palette, font sizes, spacing ratios, breakpoints, and more. This ensures that the entire project adheres to a set of strict design guidelines. Any developer can use the same “design language” to build the application, which helps maintain a high level of consistency in the user interface (UI) throughout the team’s collaboration.

WordPress.com Website Builder Assistant
WordPress.com Website Builder Assistant
99.999% Availability + Cross-zone Disaster Recovery, 24/7 Support, Free AI Build Site with Blog Package Purchase
Free domain name for one year
Visit WordPress.com Website Builder Helper →
UltaHost Website Builder Assistant
UltaHost Website Builder Assistant
900+ Free, Customizable Templates to Get the SEO Power You Need to Optimize Your Site for Search Exposure

Simplified production files generated on demand

Many people, when first encountering this technology, worry that the large number of available libraries might cause CSS files to become excessively large in size.Tailwind CSS By using PurgeCSS (which is integrated into the updated version) @tailwindcss/jit This issue can be resolved using the latest engines available. They automatically analyze your project files (such as HTML, JS, Vue, React components) and only pack the CSS classes that are actually used into the final production CSS file. As a result, the resulting style file is extremely compact, typically weighing only a few KB.

Start your first Tailwind CSS project

Without the need for complex scaffolding, you can integrate these components quickly in various ways. Tailwind CSS

Quick experience through CDN

For learning purposes or simple prototype design, you can directly use CDN links to incorporate the necessary resources. Although this approach doesn’t allow for the use of some advanced features (such as commands, plugins, or production optimizations), it’s perfect for getting a quick start and gaining an initial understanding of the tool.

<!doctype html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100">
  <h1 class="text-3xl font-bold text-center text-blue-600 mt-8">
    Hello, Tailwind!
  </h1>
  <div class="max-w-md mx-auto bg-white rounded-xl shadow-md p-6 mt-4">
    This is a card that was quickly built using Tailwind CSS.
  </div>
</body>
</html>

Using PostCSS for building formal projects

For production projects, it is recommended to use Node.js and PostCSS for installation in order to unlock all available features.

Recommended Reading Core Advantages and Design Philosophy of Tailwind CSS

First, initialize the project using npm or yarn and install the required dependencies:

npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init

This will generate a default one. tailwind.config.js File. Next, create a CSS file (for example)... src/input.css), and use @tailwind Instructions for including all layers of the framework.

/* src/input.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

Then, configure the build tools (such as Vite or Webpack) to handle this CSS file, or use it directly. tailwindcss Building using the CLI:

Bluehost Website Builder
Offers AI website creation tool, 24/7 live chat & phone support, free domain name for 1 year, free CDN, 99.99% uptime SLA
npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch

Finally, incorporate the generated content into your HTML. ./dist/output.css The document.

Master responsive design and interactive states.

When building modern web pages, responsiveness and interactive feedback are of utmost importance.Tailwind CSS An extremely elegant solution has been provided for this purpose.

Mobile-first responsive breakpoints

Tailwind Adopt a mobile-first breakpoint system. The default utility classes (such as…) .text-lg.ml-2When no prefix is used, the style applies to all screen sizes. You can specify styles for larger screen sizes by adding a specific prefix.

Recommended Reading The Ultimate Tailwind CSS Guide: Building Modern, Responsive Webpages from Scratch

Its default breakpoints include:
sm (640px), md (768px), lg (1024px), xl (1280px), 2xl (1536px).

For example, the following code creates a layout that stacks elements on mobile devices and displays them side by side on medium-sized screens:

hosting.com
Free SSL, Cloudflare CDN, WAF, 40+ global server rooms to choose from, lower latency near you, 24/7/365 service support, you can now save up to 67%, support for AI builds and SEO optimization!
<div class="flex flex-col md:flex-row">
  <div class="p-4 bg-blue-200 md:w-1/2">The content on the left side</div>
  <div class="p-4 bg-green-200 md:w-1/2">The content on the right side</div>
</div>

Convenient status variants

By adding a status prefix to your utility classes, you can easily define the styles of elements when they are in states such as hovering, being focused, or being activated.

<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded transition duration-300">
  点击我
</button>

In the above code,hover:bg-blue-700 Indicates that the background color changes to dark blue when the mouse is hovered over the element.transition and duration-300 Then, a smooth color transition animation was added to it. Similarly, you can also use… focus:active:disabled: Use prefixes to define other states.

Dark mode is supported.

Tailwind CSS Built-in dark mode support is available. First of all, tailwind.config.js Enable in:

module.exports = {
  darkMode: 'class', // 或 'media'
  // ... 其他配置
}

Set it to ‘class’ In that case, you need to manually modify the HTML root element (such as…) <html>Switch between them. class="dark"Set to ‘media’ When enabled, the system will follow the user's selected color scheme. Once activated, you can then use the corresponding colors accordingly. dark: Prefixes are used to define the styles in dark mode.

<div class="bg-white text-gray-900 dark:bg-gray-800 dark:text-gray-100">
  Switch the background and text colors based on a specific pattern.
</div>

Advanced Techniques and Ecosystems

Once you have mastered the basic usage, the following tips and tools can take your development experience to the next level.

Extract components and use @apply

Despite Tailwind It is encouraged to use practical classes directly in HTML. However, for complex style combinations that appear repeatedly in a project, you can use… @apply The instruction in CSS is to extract the “component class”.

/* 在你的 CSS 文件中 */
.btn-primary {
  @apply py-2 px-4 bg-blue-500 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400 focus:ring-opacity-75;
}

Then, use it directly in HTML. class=“btn-primary” That’s it. This approach balances the flexibility of practical applications with the maintainability of component-based systems.

A wealth of official and community plugins available.

Tailwind CSS It boasts a vibrant ecosystem. The official resources provide... @tailwindcss/typography(Used to beautify prose-style content)@tailwindcss/forms(Better form styling)@tailwindcss/aspect-ratio Additionally, there are various plugins available. Furthermore, the community has contributed countless plugins for icon integration, animations, and the beautification of scrollbars, which greatly expand the capabilities of the framework.

Deep integration with mainstream front-end frameworks

Regardless of whether you use React, Vue, Svelte, or any other framework,Tailwind CSS All of these can be seamlessly integrated. The scaffolding tools for many frameworks (such as Next.js, Nuxt.js, Vite) come out of the box with ready-to-use solutions. Tailwind Templates. By combining the framework’s component system, you can create a UI component library that is highly reusable, has a consistent style, and is easy to maintain.

summarize

Tailwind CSS It’s not just a CSS framework; it’s also a revolution in the front-end development workflow. By adopting a practical and prioritized approach, it moves style decisions out of the style sheets and into the UI templates you’re building, resulting in incredible development efficiency and design consistency. Whether it’s responsive layouts, interactive states, dark modes, or performance optimizations, it offers elegant and powerful solutions. Although the initial learning curve involves memorizing class names, once you master it, you’ll gain unprecedented freedom and speed when building user interfaces. For any developer looking for modern, efficient, and maintainable front-end styles, this framework is an essential tool.Tailwind CSS All of them are excellent choices that are worth in-depth study and application in a production environment.

FAQ Frequently Asked Questions

Will very long class names in Tailwind CSS make the HTML code become messy?

This is indeed a common concern for beginners. Although in HTML… class The property names may seem long, but this centralizes all the styling logic in one place (the view layer), eliminating the need to frequently switch between HTML and CSS files. In actual development, when combined with well-structured componentization (such as using React/Vue components), these class names are encapsulated within reusable components, which helps to maintain the cleanliness of the code. The benefits in terms of faster development and greater design consistency far outweigh the slight trade-off in readability.

How to customize or extend the default styles of Tailwind CSS?

All custom work has been completed. tailwind.config.js This is done within the file. You can do it by… theme.extend You can use objects to add new values or override default values, such as extending color options or adding additional spacing dimensions. Additionally, you can create entirely new, useful classes by writing plugins. For one-time use cases, you can still write traditional CSS files or utilize other methods as needed. @apply Instructions for combining utility classes.

How does Tailwind CSS compare with CSS-in-JS or style component solutions?

Tailwind CSS Both CSS-in-JS approaches (such as Styled-components) aim to address the maintainability issues associated with CSS, but they take different approaches to achieving this goal.Tailwind It’s a CSS framework that prioritizes practicality; CSS-in-JS, on the other hand, involves writing styles in JavaScript and applying them locally to components. The two can be used together, but usually, you have to choose one or the other. Tailwind Typically, better runtime performance is achieved (since it ultimately consists only of pure CSS), a smaller package size (thanks to Purge), and stricter constraints on design elements (design tokens).

In a production environment, are the CSS files generated by Tailwind really small in size?

Yes, that's exactly what it is. Tailwind CSS One of its core advantages is its Purge function, which is built into the engine in the latest versions. This function performs a static analysis of your source code during the build process, identifies all the class names that are used, and only includes those styles in the final generated CSS file. For a typical project, the size of the resulting CSS file is usually less than 10KB. This is a significant advantage compared to many traditional frameworks, which often produce uncompressed CSS files that can weigh several hundred KB.

Should I migrate from Bootstrap to Tailwind CSS?

It depends on the requirements of your project and the preferences of your team. If your project relies heavily on Bootstrap’s themes and pre-built components with little customization, the benefits of migrating might not be significant. However, if you have invested a lot of time in customizing Bootstrap components, frequently writing custom styles to override the default ones, or if your team values greater design flexibility and development efficiency, then migrating to another framework or technology could be worthwhile. Tailwind CSS It would be very beneficial. It is recommended to start by trying with a new project or an independent module to assess whether its workflow suits your team.