Understanding CSS Modules in Vite

Tutorial 2 of 5

Understanding CSS Modules in Vite

Introduction

Goal of this Tutorial: This tutorial aims to provide a comprehensive understanding of CSS Modules in Vite. CSS Modules are a CSS file type in which all class names and animation names are scoped locally by default, providing a way to use CSS in your Vite projects without worrying about collisions and overwriting.

Learning Outcomes: By the end of this tutorial, you will be able to:
- Explain what CSS Modules are
- Understand how to use CSS Modules in Vite
- Implement CSS Modules in your Vite projects

Prerequisites: Basic knowledge of HTML, CSS, and JavaScript is required. Experience with Vite is beneficial but not necessary.

Step-by-Step Guide

CSS Modules in Vite are very easy to use. You just need to use the .module.css extension instead of the .css extension.

Let's walk through the steps:

Step 1: Creating a CSS Module

Create a new CSS module file with the extension .module.css. For example, you can create a file named style.module.css.

/* style.module.css */

.container {
  background-color: #f5f5f5;
  border-radius: 5px;
  padding: 20px;
}

Step 2: Importing a CSS Module

You can import this CSS module in your JavaScript or Vue component like any other module.

// App.vue

<template>
  <div :class="$style.container">Hello World</div>
</template>

<script>
import styles from './style.module.css'

export default {
  name: 'App',
  data() {
    return {
      $style: styles
    }
  }
}
</script>

Best Practices and Tips

  • Always use clear and descriptive names for your CSS classes and IDs to avoid confusion.
  • Avoid using global CSS in your modules. Keep your styles scoped to prevent unexpected style collisions.

Code Examples

Example 1: Using CSS Modules in a Vue Component

// App.vue

<template>
  <div :class="$style.container">
    <h1 :class="$style.title">Hello Vite!</h1>
  </div>
</template>

<script>
import styles from './style.module.css'

export default {
  name: 'App',
  data() {
    return {
      $style: styles
    }
  }
}
</script>

In this example, we are importing the style.module.css file in our Vue component and using the styles therein.

Example 2: Using CSS Modules in a JavaScript Component

// main.js

import styles from './style.module.css'

const container = document.createElement('div')
container.className = styles.container

const title = document.createElement('h1')
title.className = styles.title
title.textContent = 'Hello Vite!'

container.appendChild(title)
document.body.appendChild(container)

In this JavaScript example, we are importing the style.module.css file and applying the styles to HTML elements created dynamically.

Summary

In this tutorial, we learned about CSS Modules in Vite and how to use them in our projects. With CSS Modules, we can keep our CSS scoped to specific components, preventing style collisions.

For further learning, you can explore more about Vite and how it handles other types of CSS such as SCSS or LESS.

Practice Exercises

  1. Exercise 1: Create a style.module.css file with several CSS styles and import them in a Vue component.

  2. Exercise 2: Create a CSS module and use it in a JavaScript file to style dynamically created HTML elements.

  3. Exercise 3: Use CSS modules in a Vite project with more complex components and styles.

Tips for Further Practice

  • Try to convert an existing CSS file in a Vite project to a CSS module and observe the changes.
  • Experiment with different types of CSS such as SCSS or LESS in your CSS modules.

Remember, practice is key to mastering any concept, and CSS Modules in Vite are no exception. Happy coding!