Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@astrojs/tailwind
Advanced tools
@astrojs/tailwind is an integration package for Astro that allows you to easily set up and use Tailwind CSS in your Astro projects. It simplifies the process of configuring Tailwind CSS with Astro, enabling you to quickly start styling your components with Tailwind's utility-first CSS framework.
Setup Tailwind CSS
This command installs the @astrojs/tailwind package along with Tailwind CSS, setting up the necessary dependencies for using Tailwind in your Astro project.
npm install @astrojs/tailwind tailwindcss
Add Tailwind Integration
This code snippet shows how to add the Tailwind integration to your Astro configuration file. By including `tailwind()` in the `integrations` array, you enable Tailwind CSS in your Astro project.
import { defineConfig } from 'astro/config';
import tailwind from '@astrojs/tailwind';
export default defineConfig({
integrations: [tailwind()]
});
Configure Tailwind
This is a basic Tailwind CSS configuration file (`tailwind.config.js`). It specifies the paths to all of your template files so Tailwind can tree-shake unused styles in production.
module.exports = {
content: ['./src/**/*.{astro,html,js,jsx,ts,tsx}'],
theme: {
extend: {},
},
plugins: [],
};
The core Tailwind CSS package. It provides the utility-first CSS framework that you can use in any project, not just Astro. You need to manually configure it with your build tools.
PostCSS is a tool for transforming CSS with JavaScript plugins. Tailwind CSS itself is a PostCSS plugin. You can use PostCSS to add other plugins for additional functionality, but it requires more manual setup compared to @astrojs/tailwind.
A Vite plugin for integrating Windi CSS, which is a utility-first CSS framework similar to Tailwind CSS. It offers faster build times and a similar feature set, but is not specifically tailored for Astro.
This Astro integration brings Tailwind's utility CSS classes to every .astro
file and framework component in your project, along with support for the Tailwind configuration file.
Tailwind lets you use utility classes instead of writing CSS. These utility classes are mostly one-to-one with a certain CSS property setting: for example, adding the text-lg
to an element is equivalent to setting font-size: 1.125rem
in CSS. You might find it easier to write and maintain your styles using these predefined utility classes!
If you don't like those predefined settings, you can customize the Tailwind configuration file to your project's design requirements. For example, if the "large text" in your design is actually 2rem
, you can change the lg
fontSize setting to 2rem
.
Tailwind is also a great choice to add styles to React, Preact, or Solid components, which don't support a <style>
tag in the component file.
Note: it's generally discouraged to use both Tailwind and another styling method (e.g. Styled Components) in the same file.
https://user-images.githubusercontent.com/4033662/197398760-8fd30eff-4d13-449d-a598-00a6a1ac4644.mp4
The astro add
command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren't sure which package manager you're using, run the first command.) Then, follow the prompts, and type "y" in the terminal (meaning "yes") for each one.
# Using NPM
npx astro add tailwind
# Using Yarn
yarn astro add tailwind
# Using PNPM
pnpm astro add tailwind
If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.
First, install the @astrojs/tailwind
and tailwindcss
packages using your package manager. If you're using npm or aren't sure, run this in the terminal:
npm install @astrojs/tailwind tailwindcss
Then, apply this integration to your astro.config.*
file using the integrations
property:
astro.config.mjs
import { defineConfig } from 'astro/config';
import tailwind from '@astrojs/tailwind';
export default defineConfig({
// ...
integrations: [tailwind()],
});
When you install the integration, Tailwind's utility classes should be ready to go right away. Head to the Tailwind docs to learn how to use Tailwind, and if you see a utility class you want to try, add it to any HTML element to your project!
Autoprefixer is also set up automatically when working in dev mode, and for production builds, so Tailwind classes will work in older browsers.
https://user-images.githubusercontent.com/4033662/169918388-8ed153b2-0ba0-4b24-b861-d6e1cc800b6c.mp4
If you used the Quick Install instructions and said yes to each prompt, you'll see a tailwind.config.cjs
file in your project's root directory. Use this file for your Tailwind configuration changes. You can learn how to customize Tailwind using this file in the Tailwind docs.
If it isn't there, you add your own tailwind.config.(js|cjs|mjs)
file to the root directory and the integration will use its configurations. This can be great if you already have Tailwind configured in another project and want to bring those settings over to this one.
The Astro Tailwind integration handles the communication between Astro and Tailwind and it has its own options. Change these in the astro.config.mjs
file (not the Tailwind configuration file) which is where your project's integration settings live.
If you want to use a different Tailwind configuration file instead of the default tailwind.config.(js|cjs|mjs)
, specify that file's location using this integration's config.path
option. If config.path
is relative, it will be resolved relative to the root.
Warning Changing this isn't recommended since it can cause problems with other tools that integrate with Tailwind, like the official Tailwind VSCode extension.
astro.config.mjs
import { defineConfig } from 'astro/config';
import tailwind from '@astrojs/tailwind';
export default defineConfig({
integrations: [tailwind({
// Example: Provide a custom path to a Tailwind config file
config: { path: './custom-config.cjs' },
})],
});
By default, the integration imports a basic base.css
file on every page of your project. This basic CSS file includes the three main @tailwind
directives:
/* The integration's default injected base.css file */
@tailwind base;
@tailwind components;
@tailwind utilities;
To disable this default behavior, set config.applyBaseStyles
to false
. This can be useful if you need to define your own base.css
file (to include a @layer
directive, for example). This can also be useful if you do not want base.css
to be imported on every page of your project.
astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
integrations: [tailwind({
// Example: Disable injecting a basic `base.css` import on every page.
// Useful if you need to define and/or import your own custom `base.css`.
config: { applyBaseStyles: false },
})],
});
You can now import your own base.css
as a local stylesheet.
@apply
directivesWhen using the @apply
directive in an Astro, Vue, Svelte, or another component integration's <style>
tag, it may generate errors about your custom Tailwind class not existing and cause your build to fail.
error The `text-special` class does not exist. If `text-special` is a custom class, make sure it is defined within a `@layer` directive.
Instead of using @layer
directives in a global stylesheet, define your custom styles by adding a plugin to your Tailwind config to fix it:
// tailwind.config.cjs
module.exports = {
// ...
plugins: [
function ({ addComponents, theme }) {
addComponents({
'.btn': {
padding: theme('spacing.4'),
margin: 'auto'
}
})
}
]
}
@apply
directivesCertain Tailwind classes with modifiers rely on combining classes across multiple elements. For example, group-hover:text-gray
compiles to .group:hover .text-gray
. When this is used with the @apply
directive in Astro <style>
tags, the compiled styles are removed from the build output because they do not match any markup in the .astro
file. The same issue may also happen in framework components that support scoped styles like Vue and Svelte.
To fix this, you can use inline classes instead:
<p class="text-black group-hover:text-gray">Astro</p>
For help, check out the #support
channel on Discord. Our friendly Support Squad members are here to help!
You can also check our Astro Integration Documentation for more on integrations.
This package is maintained by Astro's Core team. You're welcome to submit an issue or PR!
See CHANGELOG.md for a history of changes to this integration.
FAQs
Use Tailwind CSS to style your Astro site
The npm package @astrojs/tailwind receives a total of 63,446 weekly downloads. As such, @astrojs/tailwind popularity was classified as popular.
We found that @astrojs/tailwind demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.