- 入门
- 核心概念
- 定制
- Base Styles
- 布局
- Flexbox & Grid
- Flex Basis
- Flex Direction
- Flex Wrap
- Flex
- Flex Grow
- Flex Shrink
- Order
- Grid Template Columns
- Grid Column Start / End
- Grid Template Rows
- Grid Row Start / End
- Grid Auto Flow
- Grid Auto Columns
- Grid Auto Rows
- Gap
- Justify Content
- Justify Items
- Justify Self
- Align Content
- Align Items
- Align Self
- Place Content
- Place Items
- Place Self
- 间隔
- 尺寸
- 排版
- Font Family
- Font Size
- Font Smoothing
- Font Style
- Font Weight
- Font Variant Numeric
- Letter Spacing
- Line Clamp
- Line Height
- List Style Image
- List Style Position
- List Style Type
- Text Align
- Text Color
- Text Decoration
- Text Decoration Color
- Text Decoration Style
- Text Decoration Thickness
- Text Underline Offset
- Text Transform
- Text Overflow
- Text Wrap
- Text Indent
- Vertical Align
- Whitespace
- Word Break
- Hyphens
- Content
- 背景
- 边框
- Effects
- Filters
- 表格 Tables
- Transitions & Animation
- Transforms
- Interactivity
- SVG
- Accessibility
- 其他
- Install Tailwind CSS using PostCSS
- Framework Guides
- Try Tailwind CSS using the Play CDN
- Install Tailwind CSS with Next.js
- Install Tailwind CSS with Laravel
- Install Tailwind CSS with Vite
- Install Tailwind CSS with Nuxt
- Install Tailwind CSS with Gatsby
- Install Tailwind CSS with SolidJS
- Install Tailwind CSS with SvelteKit
- Install Tailwind CSS with Angular
- Install Tailwind CSS with Ruby on Rails
- Install Tailwind CSS with Remix
- Install Tailwind CSS with Phoenix
- Install Tailwind CSS with Parcel
- Install Tailwind CSS with Symfony
- Install Tailwind CSS with Meteor
- Install Tailwind CSS with Create React App
- Install Tailwind CSS with Adonis
- Install Tailwind CSS With Ember.js
- Install Tailwind CSS with Astro
- Install Tailwind CSS with Qwik
- Install Tailwind CSS with Rspack
Responsive Design
Using responsive utility variants to build adaptive user interfaces.
Overview
Every utility class in Tailwind can be applied conditionally at different breakpoints, which makes it a piece of cake to build complex responsive interfaces without ever leaving your HTML.
First, make sure you’ve added the viewport meta tag to the <head>
of your document:
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Then to add a utility but only have it take effect at a certain breakpoint, all you need to do is prefix the utility with the breakpoint name, followed by the :
character:
<!-- Width of 16 by default, 32 on medium screens, and 48 on large screens -->
<img class="w-16 md:w-32 lg:w-48" src="...">
There are five breakpoints by default, inspired by common device resolutions:
Breakpoint prefix | Minimum width | CSS |
---|---|---|
sm | 640px | @media (min-width: 640px) { ... } |
md | 768px | @media (min-width: 768px) { ... } |
lg | 1024px | @media (min-width: 1024px) { ... } |
xl | 1280px | @media (min-width: 1280px) { ... } |
2xl | 1536px | @media (min-width: 1536px) { ... } |
This works for every utility class in the framework , which means you can change literally anything at a given breakpoint — even things like letter spacing or cursor styles.
Here’s a simple example of a marketing page component that uses a stacked layout on small screens, and a side-by-side layout on larger screens (resize your browser to see it in action):
<div class="max-w-md mx-auto bg-white rounded-xl shadow-md overflow-hidden md:max-w-2xl">
<div class="md:flex">
<div class="md:shrink-0">
<img class="h-48 w-full object-cover md:h-full md:w-48" src="/img/building.jpg" alt="Modern building architecture">
</div>
<div class="p-8">
<div class="uppercase tracking-wide text-sm text-indigo-500 font-semibold">Company retreats</div>
<a href="#" class="block mt-1 text-lg leading-tight font-medium text-black hover:underline">Incredible accommodation for your team</a>
<p class="mt-2 text-slate-500">Looking to take your team away on a retreat to enjoy awesome food and take in some sunshine? We have a list of places to do just that.</p>
</div>
</div>
</div>
Here’s how the example above works:
- By default, the outer
div
isdisplay: block
, but by adding themd:flex
utility, it becomesdisplay: flex
on medium screens and larger. - When the parent is a flex container, we want to make sure the image never shrinks, so we’ve added
md:shrink-0
to prevent shrinking on medium screens and larger. Technically we could have just usedshrink-0
since it would do nothing on smaller screens, but since it only matters onmd
screens, it’s a good idea to make that clear in the class name. - On small screens the image is automatically full width by default. On medium screens and up, we’ve constrained the width to a fixed size and ensured the image is full height using
md:h-full md:w-48
.
We’ve only used one breakpoint in this example, but you could easily customize this component at other sizes using the sm
, lg
, xl
, or 2xl
responsive prefixes as well.
Working mobile-first
By default, Tailwind uses a mobile-first breakpoint system, similar to what you might be used to in other frameworks like Bootstrap.
What this means is that unprefixed utilities (like uppercase
) take effect on all screen sizes, while prefixed utilities (like md:uppercase
) only take effect at the specified breakpoint and above.
Targeting mobile screens
Where this approach surprises people most often is that to style something for mobile, you need to use the unprefixed version of a utility, not the sm:
prefixed version. Don’t think of sm:
as meaning “on small screens”, think of it as “at the small breakpoint“.
Don’t use sm:
to target mobile devices
<!-- This will only center text on screens 640px and wider, not on small screens -->
<div class="sm:text-center"></div>
Use unprefixed utilities to target mobile, and override them at larger breakpoints
<!-- This will center text on mobile, and left align it on screens 640px and wider -->
<div class="text-center sm:text-left"></div>
For this reason, it’s often a good idea to implement the mobile layout for a design first, then layer on any changes that make sense for sm
screens, followed by md
screens, etc.
Targeting a breakpoint range
By default, styles applied by rules like md:flex
will apply at that breakpoint and stay applied at larger breakpoints.
If you’d like to apply a utility only when a specific breakpoint range is active, stack a responsive modifier like md
with a max-*
modifier to limit that style to a specific range:
<div class="md:max-xl:flex">
<!-- ... -->
</div>
Tailwind generates a corresponding max-*
modifier for each breakpoint, so out of the box the following modifiers are available:
Modifier | Media query |
---|---|
max-sm | @media not all and (min-width: 640px) { ... } |
max-md | @media not all and (min-width: 768px) { ... } |
max-lg | @media not all and (min-width: 1024px) { ... } |
max-xl | @media not all and (min-width: 1280px) { ... } |
max-2xl | @media not all and (min-width: 1536px) { ... } |
Targeting a single breakpoint
To target a single breakpoint, target the range for that breakpoint by stacking a responsive modifier like md
with the max-*
modifier for the next breakpoint:
<div class="md:max-lg:flex">
<!-- ... -->
</div>
Read about targeting breakpoint ranges to learn more.
Using custom breakpoints
Customizing your theme
You can completely customize your breakpoints in your tailwind.config.js
file:
tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
theme: {
screens: {
'tablet': '640px',
// => @media (min-width: 640px) { ... }
'laptop': '1024px',
// => @media (min-width: 1024px) { ... }
'desktop': '1280px',
// => @media (min-width: 1280px) { ... }
},
}
}
Learn more in the customizing breakpoints documentation .
Arbitrary values
If you need to use a one-off breakpoint that doesn’t make sense to include in your theme, use the min
or max
modifiers to generate a custom breakpoint on the fly using any arbitrary value.
<div class="min-[320px]:text-center max-[600px]:bg-sky-300">
<!-- ... -->
</div>
Learn more about arbitrary value support in the arbitrary values documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论