360 Secure SkillHub
360 安全技能中心

tailwind-rails

面向 Ruby on Rails 的 Tailwind CSS 组件参考,涵盖 cn() 辅助方法、徽章/卡片/表格/标签页写法、暗色模式配对、hidden 类与 hidden 属性差异,以及对比度和焦点指示等可访问性注意事项;不包含安装配置、自动扫描或完整项目生成流程。

分类:开发
djc00p
djc00p
热度 37.16
最近更新 2026-11-19 12:10:49

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add djc00p/tailwind-rails
兼容性
通用兼容
联网要求
不联网
数据出网
数据不出本机
Metadata
name
tailwind-rails
description
Tailwind CSS component patterns for Ruby on Rails ERB views. Use when building UI components in Rails, creating shared partials, implementing dark mode, writing badge/button/card/table components, setting up a consistent design system, or checking Tailwind markup for contrast and accessibility problems. Trigger phrases: tailwind rails, rails UI components, ERB tailwind, rails dark mode, tailwind badge, tailwind button rails, shared partials tailwind, tailwind hidden class, tailwind contrast.
metadata
{"clawdbot":{"emoji":"🎨","requires":{"bins":[]},"os":["linux","darwin","win32"],"version":"1.0.4"}}

Tailwind CSS for Rails

Build consistent, dark-mode-ready component patterns in Rails ERB views using Tailwind utilities and reusable partials.

Quick Start

Add a class name helper to collapse conditional logic:

# app/helpers/application_helper.rb
def cn(*classes)
  classes.flatten.compact.join(' ').split.uniq.join(' ')
end

Use it in views to combine base and conditional classes:

<div class="<%= cn('px-4', active ? 'bg-blue-500' : 'bg-gray-200') %>">
  Content
</div>

Standard full-width container with responsive padding:

<div class="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
  <!-- Page content -->
</div>

Badge Pattern

<span class="inline-flex items-center rounded-full bg-green-100 px-3 py-1 text-sm font-medium text-green-800 dark:bg-green-900 dark:text-green-100">
  Label
</span>

Colors: Yellow=warning, Blue=info, Green=success, Red=danger, Gray=neutral
Light: bg-{color}-100 + text-{color}-800 | Dark: dark:bg-{color}-900 + dark:text-{color}-100

Common Components

Card with border and shadow:

<div class="rounded-lg border border-gray-200 bg-white shadow-sm dark:border-gray-800 dark:bg-gray-950">
  <div class="px-6 py-4">
    <!-- content -->
  </div>
</div>

Table wrapper with responsive shadow:

<div class="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
  <table class="w-full divide-y divide-gray-200">
    <thead>...</thead>
    <tbody>...</tbody>
  </table>
</div>

Tab navigation with underline:

<nav class="flex space-x-8 border-b border-gray-300">
  <% tabs.each do |tab| %>
    <% active = current_tab == tab %>
    <%= link_to tab_path(tab),
        class: "py-2 px-1 border-b-2 font-medium text-sm #{active ? 'border-blue-500 text-blue-600' : 'border-transparent text-gray-600 hover:text-gray-700'}" do %>
      <%= tab.humanize %>
    <% end %>
  <% end %>
</nav>

Best Practices

  1. Use the cn() helper for all conditional class logic — it deduplicates and removes blanks.
  2. Pair light and dark classes always — bg-white dark:bg-gray-950, text-gray-900 dark:text-gray-100.
  3. Mobile-first responsive design — use sm:, md:, lg: prefixes for breakpoints.
  4. Extract repeated patterns into partials — _badge.html.erb, _button.html.erb, _card.html.erb.
  5. Use semantic color names — yellow for warnings, green for success, red for errors.
  6. Ensure sufficient contrast in both light and dark modes.
  7. Test accessibility with screen readers and keyboard navigation.

Accessibility

The Tailwind hidden class vs the HTML hidden attribute

These are different mechanisms and they interact in ways that surprise people.

Tailwind class HTML attribute
Syntax class="hidden" <div hidden>
Mechanism display: none via CSS display: none via the UA stylesheet
Overridable by CSS? Yes - any later display: rule wins No - needs [hidden] { display: none !important } to beat
Hidden from screen readers? Yes Yes
Removed from tab order? Yes Yes

The trap: the HTML hidden attribute has a lower specificity than almost any Tailwind utility. A Tailwind class like flex or block on the same element overrides it, and the element stays visible while the author believes it is hidden.

<%# BAD - the `flex` utility overrides the `hidden` attribute, element stays visible %>
<div hidden class="flex">
<%# GOOD - use the Tailwind class so the intent is explicit and wins by specificity %>
<div class="hidden">

Rule: pick one mechanism per element. If you use Tailwind for visibility (hidden, md:hidden, block), do not also add the hidden attribute.

Responsive visibility is CSS-only. hidden md:block hides at small widths and shows at md+. The element is never removed from the DOM, so assistive tech sees it at all widths - it is only visually hidden. For content that must be removed from the accessibility tree entirely (not just visually), remove the element from the DOM server-side.

Contrast is measured against the real background

A color pair that passes on white can fail on the actual container. Contrast is computed between the text color and the nearest painted background behind it - not against the page background.

<%# BAD - text-gray-400 on a gray-100 badge passes against white, fails on gray-100 %>
<span class="bg-gray-100 text-gray-400">
<%# GOOD - compute against the container that actually paints behind the text %>
<span class="bg-gray-100 text-gray-800">

WCAG AA thresholds:

Text size Minimum ratio
Normal (< 18px, or < 14px bold) 4.5:1
Large (>= 18px, or >= 14px bold) 3:1
UI components & focus indicators 3:1

Common traps:

  • Disabled states. text-gray-400 on bg-gray-50 is usually around 2.8:1. Disabled controls are exempt from WCAG contrast requirements - but "disabled" must be conveyed by more than color alone.
  • Placeholder text. Placeholders are still text and need contrast. Browsers default them to low opacity.
  • Dark mode. Every text-* needs a dark:text-* counterpart, and the ratio must hold in both modes. A pair that passes in light mode can fail in dark.
  • Text over images/gradients. The background varies, so the ratio varies. Use a scrim or a solid backing behind the text.
  • Semantic color tokens. oklch() colors with high chroma saturate and fail more often than the equivalent hex. Chroma above roughly 0.04 on light backgrounds is a reliable failure.

Focus indicators must survive

Tailwind's preflight removes the browser's default outline. If you do not replace it, keyboard users lose focus entirely.

<%# BAD - outline removed, nothing replaces it %>
<a class="focus:outline-none">
<%# GOOD - always pair outline-none with a visible ring %>
<a class="focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-600 focus-visible:ring-offset-2">

Use focus-visible rather than focus so mouse clicks do not show the ring, but keyboard navigation always does.

Never hide content from screen readers with display: none

hidden (the class or the attribute) removes content from the accessibility tree and from the tab order. If the content is still meaningful - for example a label that is visually superseded by an icon - use sr-only instead:

<%# GOOD - visible to screen readers, invisible on screen %>
<button class="p-2">
  <%= render "icon", name: "trash" %>
  <span class="sr-only">Delete item</span>
</button>

Icon-only buttons need an accessible name

<%# BAD - no accessible name %>
<button><svg ...></svg></button>

<%# GOOD %>
<button aria-label="Delete item">
  <svg aria-hidden="true" ...></svg>
</button>

Decorative SVGs need aria-hidden="true" so the screen reader does not announce raw path data.

References

  • references/components.md — Reusable badge, button, and card partials with usage examples.
  • references/dark-mode-responsive.md — Dark mode setup, responsive patterns, and animation utilities.