SvelteKit - Tailwind CSS Setup

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
Search for a command to run...

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
No comments yet. Be the first to comment.
SvelteKit is a powerful framework for building web apps. Follow my series of posts to learn how to use its features and create fast and efficient web apps.
Continuing from last time, we will explore the concepts behind Create and Update functionality and provide a step-by-step guide on how to implement it in your SvelteKit application. Let's get started! Server & Client Code In SvelteKit, the server-sid...
Writing this up so I have somewhere to quickly grab basic Jest setup. This information is copied from the Jest Documentation Site compressed into just the parts I'm interested in. Create folders mkdir -p <project>/src cd <project> Install and init n...

In this blog post, we'll explore the process of setting up and configuring a Postgres database using Docker and seeding it with test data. This approach can be used for a variety of apps and can be easily adapted for your own needs. We'll cover all t...

Continuing from last time, we will explore the concepts behind Create and Update functionality and provide a step-by-step guide on how to implement it in your SvelteKit application. Let's get started! Server & Client Code In SvelteKit, the server-sid...

Introduction In this post, I will explore how to create a REST API using Spring Boot and Kotlin. We will dive into the fundamentals of Spring Boot and Kotlin, explain how to set up the project, and guide you through the process of building the API st...

Introduction Welcome to the latest post in my series of Koan Battles using Kotlin and TypeScript. In this series, we'll complete a Koan in both languages and then discuss the differences between the two solutions. My goal is to help you gain a deeper...

Hello and welcome back to my series of technical blog posts on SvelteKit! In this post, I will be introducing my next topic which is integrating Tailwind CSS with SvelteKit.
Tailwind CSS is a utility-first CSS framework that provides a set of pre-defined CSS classes that can be used to style HTML elements. It allows you to create complex layouts and designs with minimal CSS code, making it a popular choice among developers.
Integrating TailwindCSS with SvelteKit can help streamline your development process by providing pre-defined styles that can be easily applied to your components.
To install Tailwind CSS and its dependencies, navigate to your SvelteKit project directory in your terminal and run the following command:
pnpm install -D tailwindcss postcss autoprefixer @tailwindcss/forms
This command will install the latest version of TailwindCSS, PostCSS, Autoprefixer and Tailwind Forms as dev dependencies in your project.
Next, we need to create a configuration file for TailwindCSS. You can do this by running the following command in the root of our project directory:
pnpx tailwindcss init tailwind.config.cjs -p
This command will generate a default tailwind.config.cjs file in your project root. The -p flag tells Tailwind CSS to generate a basic postcss.config.cjs file.
Now add the file paths to the Svelte template files in tailwind.config.cjs :
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ['./src/**/*.{html,js,svelte,ts}'],
theme: {
extend: {},
},
plugins: [require("@tailwindcss/forms")],
}
Create a src/app.css file and add Tailwind:
@tailwind base;
@tailwind components;
@tailwind utilities;
Then add src/app.css to our layout file src/routes/+layout.svelte:
<script>
import "../app.css";
</script>
<slot />
We are now ready to sprinkle some Tailwind magic, for example on the main page:
<div class="mx-auto max-w-2xl text-center p-16">
<h1 class="text-4xl font-bold text-gray-900">Top 3</h1>
<div class="mt-8">
{#if data.todos.length > 0}
<Todos todos={data.todos} />
{:else}
<NewTop3 />
{/if}
</div>
</div>

Check out all the code in the GitHub Repository to see how I've styled the other Svelte components. Thanks for reading!