This Vite app is a mock-up so my friends can quickly style SPAs with Tailwind. I found the docs a bit frustrating to navigate and decided to make a shortcut. The important bits are this README, the example src/App.jsx
, and tailwind.config.ts
. Feel free to beg, borrow, or steal any of it 😊.
Gold star to the person who finds a cool way to stack linear-gradients in Tailwind (please @me 🥺).
Tailwind is a CSS framework that uses short-hand for pre-existing CSS stylesheet declarations. These short-hand phrases are called "utility classes", referring to a styling property and its value. They are declared inside the class/className of an html attribute. Many utility classes can be used in one attribute statement like so:
<footer className="sticky p-2 bottom-0 flex flex-row bg-indigo-950"></footer>
Click here to open the repo. Browse, or:
Click the "Fork" button. (This will create a user copy of the repository.)
Open the Terminal application.
In Terminal, use the "cd" command to navigate to where the local repository will live.
In GitHub, click on the green "Code" dropdown. You will see three clone options. Select "SSH" and copy the SSH key.
In Terminal, run "git clone [insert copied SSH key here]", and then use the "cd" command to navigate into the newly created directory.
Open the local repository in VS Code by typing: code .
Type npm run dev
in your Terminal to run the Vite app, and view in browser at http://localhost:5173/.
To install Tailwind in your own project go here. For the Vite install in particular, go here and follow their instructions:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
tailwind.config.js
add in content: []
the following:"./index.html",
"./src/**/*.{js,ts,jsx,tsx}",
@tailwind base;
@tailwind components;
@tailwind utilities;
I'll start out by highlighting this App wrapper:
<div className='min-h-screen flex flex-col bg-blue-100'></div>
and main tag:
<main className='flex flex-1 flex-col'></main>
These should give your app a good starting point by setting the content to fit screen height and having the main content fill available space with flex-1
.
I like to do the classic header, main, footer setup for a quick SPA with semantic HTML.
<div className="min-h-screen flex flex-col bg-blue-100">
<header className="bg-gray-100">header</header>
<main className="flex flex-1 flex-col">main</main>
<footer className="bg-gray-100">footer</footer>
</div>
Don't guess what number to use for sizes. Only some work.
h-px
is height of 1pxh-1
is height: 4px;
Here's an example of applying pixels to padding to show what I mean:
p-0
is 0px padding,p-1
is 4px padding, and afterp-12
some numbers are skipped.
For font sizes you can try text-xs
or -sm/-base/-lg/-xl/-2xl...-9xl.
Tailwind's text-base
is 1rem
, which is 16px in vanilla CSS.
Generally helpful container values:
h-full
to fill container's heighth-screen
or min-h-screen
to fill viewportmin-h-min
, min-h-max
w-screen
w-1
is width: 4px;
p-2
is padding: 8px;
m-4
is margin: 1rem;
Percentages as fractions:
w-1/4
or w-full
is width: 25% or width: 100%You can add x/y for left&right, top&bottom like the x/y (vertical/horizontal) axes: px-1
is padding-left: 4px;
and padding-right: 4px;
, or padding: 0 4px;
t,r,b,l is top, right, bottom, left, so pt-1
is padding-top: 4px;
default breakpoints:
Adding a value at a breakpoint looks like this: sm:text-center
`background-color: #4b5563` is written in the `className` as `bg-gray-600`:
`<div className="bg-gray-600 text-gray-100"></div>`
I included an example of how to add custom colors in the tailwind.config.ts
file. Label hex codes with a name and number to reference in your className elements. I used numbers in-between the pre-existing Tailwind designations.
I've included a few simple examples in App.jsx
, however I would suggest using dev tools to get the flex properties you want and use the search field in the tailwind docs to find their special syntax.
If I wanted a grid display:
.container{
display: grid;
gap: 1.5rem;
grid-template-columns: repeat(3, 1fr);
}
it would be something like this:
<div className="grid gap-5 grid-cols-3"></div>
Or for a flex display:
.profile-form{
align-items: flex-start;
display: flex;
flex-direction: column;
justify-content: space-evenly;
}
it might look like this:
<div className="flex flex-col items-start justify-evenly"></div>