Import CSS in node_modules to Svelte
Last Updated :
10 Sep, 2024
When we require importing of external CSS files where the files are bundled with other packages in node_modules then we need to import these CSS files into your Svelte components. also, if you’re using some libraries such as Bootstrap or Tailwind CSS, for instance. Here, we will discuss the various methods that you can use to import CSS from the node_modules folder into your Svelte project.
These are the following approaches to import the CSS in node_module to svelte:
Steps to Create a Svelte Application
Step 1: Install Svelte
npm create svelte@latest my-svelte-app
cd svelte-app
npm install
npm run dev
Project Structure:
Project structureStep 2: Install the dependencies:
npm install bootstrap
Updated Dependencies:
"dependencies": {
"bootstrap": "^5.2.0",
"svelte": "^3.0.0"
}
Direct Import in the <script> Block
This method enables you to include CSS files to some of the Svelte components. The benefit is that the styles are made specific to the component hence are not seen in other components.
Example: This example shows the importing the bootstrap module directly in the script tag.
JavaScript
<script>
export let name;
import 'bootstrap/dist/css/bootstrap.min.css';
</script>
<main>
<h1>Hello {name}!</h1>
<p>Visit the <a href=
"https://www.geeksforgeeks.org/">GeeksForGeeks Website to learn Svelte</a>
to learn how to build Svelte apps.</p>
</main>
Output:
OutputUsing the Svelte <style> Block with Global Scope
Svelte styles components by default isolate the applied styles, meaning that the styles will not bleed into other components. Although, to define the styles globally within a component, we have to use the global keyword within the <style> block. With this approach, Bootstrap styles will be applied globally within the App.svelte component, and the .container class will have a margin of 20px. This ensures that Bootstrap’s styling is available while allowing you to add additional global styles.
Example: This example shows the use of style tag to add the CSS.
JavaScript
<style global>
@import 'bootstrap/dist/css/bootstrap.min.css';
</style>
<main>
<h1>Hello {name}!</h1>
<p>Visit the
<a href=
"https://www.geeksforgeeks.org/">GeeksForGeeks Website to learn Svelte</a>
to learn how to build Svelte apps.</p>
</main>
Output:
OutputImporting CSS in root file (Global CSS)
In case you have to apply CSS universally throughout the entire Svelte application, it is possible to import the CSS file in the main.js or main.ts file. This makes sure that all the components in the app have the styles and this will make them to be uniform. when we import Bootstrap in main, it becomes available for use in all files that we may create in this project. js, which meant that the styles are applied to all the webpage and components in your Svelte application. This is especially helpful for stylesheets like libraries, for instance Bootstrap or Tailwind CSS that you wish to use across the entire application.
Example: This example shows the use of global CSS in mail file.
JavaScript
// index.js
import App from "./App.svelte";
import "bootstrap/dist/css/bootstrap.min.css";
const app = new App({
target: document.body,
props: {
name: "GeeksForGeeks Articles",
},
});
export default app;
Output:
Output
Similar Reads
How to import a module in Typescript ? Before starting with importing modules, first of all, we need to know the basics of modules in TypeScript. We know that JavaScript has come with the concept of modules from the ES6 version in 2015 and by 2020 had broad support in most web browsers and JavaScript runtimes. TypeScript also shares the
5 min read
How to use EcmaScript Modules in Node.js ? Using ECMAScript Modules (ES Modules or ESM) in Node.js allows you to take advantage of modern JavaScript syntax for organizing and managing your code. ECMAScript Modules provide a more structured and standardized way to work with modules compared to CommonJS, which has been traditionally used in No
2 min read
What is the purpose of module.exports in node.js ? The module.exports is actually a property of the module object in node.js. module. Exports is the object that is returned to the require() call. By module.exports, we can export functions, objects, and their references from one file and can use them in other files by importing them by require() meth
3 min read
How to Access URL Query String in Svelte? URL query strings are a way to pass parameters to a web page through the URL. They appear after the question mark (?) in a URL and are typically used to convey data or state between different parts of a web application.In Svelte, you can access and manipulate these query parameters to dynamically di
3 min read
How to remove all Global Modules in Node.js ? Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside of a browser. You need to remember that Node.js is not a framework and itâs not a programming language. Most of the people are confused and understand itâs a framework or a programming language. We
2 min read
How to Import SVG Icons into a Svelte App? SVG Icons are used in Svelte applications for creating scalable, high-quality graphics that can be easily styled and animated. They are lightweight, making them ideal for enhancing the visual appeal of modern web applications. In this article, we'll explore two different approaches to importing and
3 min read