Kick off your project with this bare-bones Tailwind CSS + Emotion starter for Gatsby. This starter ships with the packages and configuration files you need to get hit the ground running on your next Tailwind CSS project.
Andrew Welch wrote a terrific article about this setup over on his blog, "Using Tailwind CSS with Gatsby, React & Emotion Styled Components". The only difference between this starter and Andrew's writeup is I have not included stylelint as of this release.
If you prefer to use a Gatsby Theme instead of a Gatsby Starter, Jordi Talens has an excellent theme that you can layer on with other Gatsby themes.
You can see a demo of the frontend over yonder.
-
Create a Gatsby site.
Use the Gatsby CLI to create a new site, specifying the
gatsby-tailwind-emotion-starter
starter.# create a new Gatsby site using the gatsby-tailwind-emotion-starter gatsby new my-gatsby-tailwind-emotion-starter https://github.com/pauloelias/gatsby-tailwind-emotion-starter
-
Start developing.
Navigate into your new siteβs directory and start it up.
cd my-gatsby-tailwind-emotion-starter/ gatsby develop
-
Open the source code and start editing!
Your site is now running at
http://localhost:8000
!Open the
my-gatsby-tailwind-emotion-starter
directory in your code editor of choice and editsrc/pages/index.js
. Save your changes and the browser will update in real time!
This starter contains has the following features enabled by default:
- Tailwind CSS: The full power of Tailwind is at your fingertips. Style your components using twin.macro to add Tailwind classes to your project.
- Emotion: Best-in-class CSS-in-JS support with Emotion. Write your own custom styled components with Emotion or use
twin.macro
inside your styled components to add Tailwind CSS classes alongside your custom styling. - PostCSS: Use the flexibility of PostCSS to extend Tailwind's CSS or write your own CSS. Postcss-Preset-Env is enabled out-of-the box allowing you to write tomorrow's CSS today!
To use Tailwind CSS classes inside of your components you use the twin.macro
package. You can also create richer styled components using a combination of both Tailwind's classes and your own custom CSS with Emotion. Laslty, if needed, you can use PostCSS to write your own custom CSS as well.
import tw from "twin.macro"
import React from "react"
const Heading = tw.h1`
text-2xl text-gray-500 uppercase
`
export default () => (
<div>
<Heading>Hello, world!</Heading>
</div>
)
import tw, { styled } from "twin.macro"
import React from "react"
import pattern from "../images/pattern.png"
const Container = styled.div`
${tw`bg-gray-100 w-full`}
background-image: url(${background});
padding: 15px;
`
export default () => (
<Container>
<h1>Hello, world!</h1>
</Container>
)
import tw, { styled } from "twin.macro"
import React from "react"
import pattern from "../images/pattern.png"
const Container = styled.div`
${tw`bg-gray-100 w-full`}
background-image: url(${background});
padding: 15px;
`
const Heading = tw.h1`
text-2xl text-gray-500 uppercase
`
export default () => (
<Container>
<Heading>Hello, world!</Heading>
</Container>
)
import tw, { css } from "twin.macro"
import React from "react"
export default () => (
<div
css={css`
${tw`flex items-center justify-between px-4 py-3`}
`}
>
<h1>Hello, world!</h1>
<h2>I'm a flex item too!</h2>
</div>
)
A quick look at the top-level files and directories you'll see in a Gatsby project.
.
βββ src
βββ .babelrc
βββ .eslintignore
βββ .gitignore
βββ .nvmrc
βββ .prettierignore
βββ .prettierrc
βββ .stylelint.config.js
βββ babel-plugin-macros.config.js
βββ gatsby-browser.js
βββ gatsby-config.js
βββ gatsby-node.js
βββ gatsby-ssr.js
βββ LICENSE
βββ package-lock.json
βββ package.json
βββ postcss.config.js
βββ README.md
βββ tailwind.config.js
-
/src
: This directory will contain all of the code related to what you will see on the front-end of your site (what you see in the browser) such as your site header or a page template.src
is a convention for βsource codeβ. -
.babelrc
: This configuration file allows us to fine-tune Babel's configuration settings. In this starter we are adding thebabel-preset-gatsby
preset to allow us to customize Babel as needed. -
.eslintignore
: This file specifies files and folders that we want to exclude from linting with Eslint. -
.gitignore
: This file tells git which files it should not track / not maintain a version history for. -
.nvmrc
: The.nvmrc
allows you to lock down a project's node version when using nvm. -
.prettierignore
: This file allows us to specifiy files that we want to exclude from formatting with Prettier. -
.prettierrc
: This is a configuration file for Prettier. Prettier is a tool to help keep the formatting of your code consistent. -
.stylelint.config.js
: Stylelint configuration file to customize stylelint rules in the project. Styleline is enabled to tame CSS errors displayed in some editors due to TailwindCSS's syntaxt inside CSS files. -
babel-plugin-macros.config.js
: This file helps us configure Tailwind CSS macros to be used with Emotion, our CSS-in-JS tool of choice. -
gatsby-browser.js
: This file is where Gatsby expects to find any usage of the Gatsby browser APIs (if any). These allow customization/extension of default Gatsby settings affecting the browser. By default we are injecting Tailwind's base styles into the browser. -
gatsby-config.js
: This is the main configuration file for a Gatsby site. This is where you can specify information about your site (metadata) like the site title and description, which Gatsby plugins youβd like to include, etc. (Check out the config docs for more detail). -
gatsby-node.js
: This file is where Gatsby expects to find any usage of the Gatsby Node APIs (if any). These allow customization/extension of default Gatsby settings affecting pieces of the site build process. -
gatsby-ssr.js
: This file is where Gatsby expects to find any usage of the Gatsby server-side rendering APIs (if any). These allow customization of default Gatsby settings affecting server-side rendering. -
LICENSE
: Gatsby is licensed under the MIT license. -
package-lock.json
: (Seepackage.json
below, first). This is an automatically generated file based on the exact versions of your npm dependencies that were installed for your project. (You wonβt change this file directly). -
package.json
: A manifest file for Node.js projects, which includes things like metadata (the projectβs name, author, etc). This manifest is how npm knows which packages to install for your project. -
postcss.config.js
: This configuration file allows us to customize our PostCSS settings. PostCSS is used to compile the custom css we write outside of Emotion. -
README.md
: A text file containing useful reference information about your project. -
tailwind.config.js
: This is the default Tailwind CSS configuration file.
To learn more about Gatsby, use the following resources:
- Gatsby Documentation - learn about Gatsby's features and API.
- Gatsby Tutorial - a lovely Gatsby tutorial.
You can check out the Gatsby GitHub repository - your feedback and contributions are welcome!