In this article we are going to learn about how to use tailwind with svelte
- Creating a Svelte App
- Installing and Configuring Tailwind in Svelte App
- Creating re-usable button with Svelte and tailwind
- Different customization options with tailwind CSS and Svelte
- Component design with tailwind css and Svelte
- State and event management in Svelte components with Tailwind
- Integrating Tailwind CSS utility classes with Svelte animations
- Conclusion
In this article we are going to learn about how to use tailwind css with svelte
Creating a Svelte app
Let us create a Svelte app and then add tailwind to it.
Type the below code to initialize a new project
It is going to ask you some questions and you can select the options based on your preferences.
Once you have installed the app run
npm install
to install all the dependencies
then run the below command
npm run dev
to start the application
this will start your application in localhost 5173
Installing and Configuring Tailwind in Svelte App
We have our Svelte app running, let's add tailwind to the project
Step 1: to install the tailwind, type this code in your terminal
Step 2: then open the file svelte.config.js
there import the vitePreprocess and add the preprocess in the config like so
Step 3: After this open the tailwind.config.js
file to configure the template paths like
This article is brought to you by DeadSimpleChat, Chat API and SDK for your website and app
Step 4: After this create a new file named app.css
in the ./src
folder
Step 5: Create a svelte file named +layout.svelte
in the ./src/routes/+layout.svelte
and import the app.css
file
Step 6: Run the application
npm run start
Step 7: Start using tailwind in your application
When using Tailwind remember to add lang="postcss"
for any <style>
blocks that needs to be processed by tailwind
<h1 class="text-3xl font-bold underline">
We are running tailwind in Svelte!
</h1>
<style lang="postcss">
:global(html) {
background-color: theme(colors.indigo.100);
}
</style>
Example: Creating a Reusable button with tailwind and Svelte
Let us create a reusable button with tailwind and Svelte Kit
Step 1: Creating a button component file
In our Svelte application create in new Svelte component file and name it Button.svelte
.
Create this file inside src/lib
folder. You can do this through UI in VS Code or use the terminal
touch src/lib/Button.svelte
Step 2: Creating the component
In the file Button.svelte
write the below code
This article is brought to you by DeadSimpleChat, Chat API and SDK for your website and app
What we are doing here?
We are exporting three props here
- buttonText : for the text that is displayed on the button.
- buttonStyle: To define button style
- textSize: The size of the text inside the button
In the style section we have the tailwind utility classes for the button
We are using the @ directive here to apply the tailwind utility classes
Lastly, we are defining the button with the tailwind classes and the buttonText
Step 3 : Using the Button component in our Svelte page
We are going to use the button component that we just created in our svelte page
open the +page.svelte
and import the Button.svelte
in it
like
In this example we have created the svelte app and created three button with tailwind css
Different customization options with tailwind CSS and Svelte
- Changing button Colors
Easily customize the button color by using built in tailwind CSS color classes, you can also create custom colors using the configuration file
2. Adding icons to the buttons
You can easily added icons to the buttons as well. To do this install the heroicons npm package
Now, let us modify our Button.svelte
component to accept the new icon
prompt and include the icon in the markup
In the above example we have imported the heroicons package and added the arrow icons to our buttons
Component design with tailwind css and Svelte kit
In this section we will explore component design with tailwind and Svelte Kit
We are going to learn component design with the help of an example
Step 1: Card Component
In our project, let us create an new file Card.svelte
in the directory src/lib
touch src/lib/Card.svelte
Step 2: Creating the component into small reusable parts
To improve component design we will design our component into smaller reusable parts
Let us create the parts of out card component
CardHeader
CardBody
CardFooter
Let us create separate files for these components in the src/lib
directory
touch src/lib/CardHeader.svelte
touch src/lib/CardBody.svelte
touch src/lib/CardFooter.svelte
Step 3: Creating the Sub Component
We will create sub components using slots, that will help us creating customizable components and with content injection as well
CardHeader.svelte
CardBody.svelte
CardFooter.svelte
Step 4: Creating the main Card Component
Next we will combine the subcomponents to create the main.svelte
component file
We will be using Svelte slots to be able to inject the sub components into the main component
main.svelte
Now, we will use the main card component on a svelte page
In the above example we added the card component into 2 separate configurations
Thus we are able to re use and maintain the single card component on multiple places and different configurations
Thus creating smaller re usable components and later joining them to create larger components leads to more flexible design.
Integrating Tailwind utility Classes with Svelte Animations
We have built in animation and transition support in Svelte. Let us see how we can integrate tailwind utility classes with svelte animations
Step 1: Importing Svelte animation and easing
To add animations to Svelte we have import the animations and easing functions from svelte/animate
and svelte/easing
modules
Step 2: Adding Tailwind utility classes to Svelte animations
We have to define the animation config using the animate
directive in our component markup and apply the utility classes as we want to
We are appling the tailwind CSS bg-blue-200
, text-red
px-4
and other tailwind utility classes for styling at the same time we are using Svelte fly
animation for in
and out
transitions.
Step 3: Animate multiple elements at the same time
We can create concurrent animations using svelte and tailwind. To do this we need to use each
blocks along with the animate
directive
Here we created a list of notifications and used each
to render the elements
We are notifications has the fly
animations while enteringand leaving
We are integrating Tailwind CSS utility with Svelte. Using this you can create awesome animations and do performance optimization as well
Benefits and advantages of using Svelte with Tailwind
Lastly, let us consider some of the benefits and advantages of using svelte with tailwind
- Having a utility first approach
- Scoped styles and ability to make components
- Easy to structure responsive design
- Small CSS bundle size
- Fully integrated developer experience
- Having a utility first approach: Tailwind has a utility first approach. This gives you control over styling and makes for easy styling and better consistency through the project.
<!-- Styling a button with tailwind -->
<button class="bg-blue-500 hover:bg-blue-200 text-white font-bold py-2 px-4 rounded">
Super Buy Now
</button>
2. Scoped Styles and ability to make components: Using tailwind with Svelte you can directly apply utility classes to Svelte components, giving you the ability to encapsulate the styles for a perticular component. Thus creating individually styled and scoped components
3. Create Responsive designs: Svelte has responsive and reactive components built in and tailwind also supports responsive design with easy to use breakpoints.
4. Minimized CSS bundle size: In tailwind 3 only the classes that are being used are included in the bundle thus reducing the size of the CSS bundle also in Tailwind 2 with purge css the size of the css bundle is significantly reduced
5. Integrated developer experience: With Svelte and tailwind can be easily integrated into a single development file thus avoids switching between CSS files and Svelte files
module.exports = {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
],
};
State and event management in Svelte components with tailwind
In this section we will look at state and event management in Svelte components that are using tailwind for styling
step 1: Create a Svelte component with tailwind
We are creating a new component called Counter.svelte
and we have a <script>
tag inside it. We have also declared a variable called count
Then we are adding Tailwind utility classes to it to add styling
Step 2: Adding buttons to update the state
Next, we will create button to change the state and add tailwind utility classes for styling.
We are also adding event listners to the click
event using the Svelte on:
directive and we will update the count on the click of the button
<script>
let count = 0;
function increment() {
count++;
}
function decrement() {
count--;
}
</script>
<!-- Add buttons to change the state -->
<div class="bg-white shadow-md rounded p-4 w-full max-w-xs mx-auto">
<p class="text-xl font-semibold text-gray-800">Counter: {count}</p>
<div class="flex space-x-2 mt-4">
<button
class="bg-blue-500 text-white py-1 px-3 rounded"
on:click="{add}"
>
Increment
</button>
<button
class="bg-red-500 text-white py-1 px-3 rounded"
on:click="{subtract}"
>
Decrement
</button>
</div>
</div>
Step 3: Handle events with Svelte and tailwind
Based on the changes in a child component we need to update the parent component
To do this we are going to import the createEventDispatcher
function and use it to dispatch custom events
In the Counter.svelte
file we will dispatch a new countUpadted
event each time a count is updated
<script>
import { createEventDispatcher } from "svelte";
const dispatch = createEventDispatcher();
let count = 0;
function increment() {
count++;
dispatch("countUpdated", { count });
}
function decrement() {
count--;
dispatch("countUpdated", { count });
}
</script>
<!-- code -->
In the parent component listen to the countUpdated event and update the parent component as required
<script>
import Counter from "./Counter.svelte";
let totalCount = 0;
function handleCountChanged(event) {
totalCount = event.detail.count;
}
</script>
<Counter on:countChanged="{handleCountChanged}" />
<p>Total count: {totalCount}</p>
Need Chat API for your website or app
DeadSimpleChat is an Chat API provider
- Add Scalable Chat to your app in minutes
- 10 Million Online Concurrent users
- 99.999% Uptime
- Moderation features
- 1-1 Chat
- Group Chat
- Fully Customizable
- Chat API and SDK
- Pre-Built Chat
Conclusion
In this tutorial we learnt about creating a Svelte app and how to integrate Tailwind CSS with svlete app
Thank you for reading the article. I hope you liked the article