How to Target a Component in Svelte with CSS?
Last Updated :
26 Aug, 2024
Targeting a component in Svelte means applying CSS styles specifically to a particular component to control its appearance and behavior. This can be achieved by using scoped styles within the component or applying global styles that affect the component. Proper targeting ensures that styles are encapsulated or shared as needed, enhancing your Svelte application's visual consistency and maintainability.
In this article, we will explore two different approaches to target a component in svelte with CSS.
Steps To Target a Component In Svelte with CSS
Step 1: Install Node.js
Make sure Node.js is installed on your machine. You can download it from nodejs.org.
Step 2: Install Svelte
Open your terminal and run the following command to set up a new Svelte project:
npx degit sveltejs/template svelte-app
Step 3: Navigate to Your Project Directory
Change to the newly created project directory:
cd svelte-app
Step 4: Install Dependencies
Install the required npm packages:
npm install
Project Structure:
Folder StructureDependencies:
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@rollup/plugin-terser": "^0.4.0",
"rollup": "^3.15.0",
"rollup-plugin-css-only": "^4.3.0",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-svelte": "^7.1.2",
"svelte": "^3.55.0"
},
"dependencies": {
"sirv-cli": "^2.0.0"
}
Scoped CSS Using Component Styles
In this approach, we are using scoped CSS within the <style> block of the Svelte component to target and style specific elements. The CSS defined here applies only to this component, ensuring that styles are encapsulated and do not affect other components. The active and inactive classes are conditionally applied based on the component's state, allowing dynamic styling.
Example: The below example uses Scoped CSS Using Component Styles to Target a Component In Svelte with CSS.
JavaScript
<!-- App.svelte -->
<script>
let isActive = false;
</script>
<main>
<h3>Scoped CSS Using Component Styles</h3>
<button on:click={()=> isActive = !isActive}>
Toggle Component Style
</button>
<div class={isActive ? 'active' : 'inactive' }>
<p>This component has styles scoped to it.</p>
</div>
</main>
<style>
main {
text-align: center;
padding: 1em;
}
h3 {
color: darkblue;
}
.active {
background-color: lightblue;
color: darkblue;
border: 2px solid darkblue;
padding: 1em;
border-radius: 8px;
}
.inactive {
background-color: lightcoral;
color: darkred;
border: 2px solid darkred;
padding: 1em;
border-radius: 8px;
}
</style>
Output:
Global CSS with Component-Specific Classes
In this Approach, targeting a component in Svelte with CSS is done by defining global styles in an external CSS file, such as global.css. The component imports this global CSS file and uses predefined classes, such as component-style, active, and inactive, to apply styles. The component's state determines which class is applied dynamically, allowing the component to be styled based on its current state .
Example: The below example uses Global CSS with Component-Specific Classes to Target a Component In Svelte with CSS.
CSS
/* global.css */
.component-wrapper {
display: flex;
flex-direction: column;
align-items: center;
padding: 1em;
border-radius: 8px;
}
.component-style {
width: 80%;
max-width: 600px;
border: 2px solid #333;
padding: 1em;
border-radius: 8px;
transition: background-color 0.3s, color 0.3s;
}
.component-style.active {
background-color: lightgreen;
color: darkgreen;
}
.component-style.inactive {
background-color: lightcoral;
color: darkred;
}
.component-header {
font-size: 1.5em;
margin-bottom: 1em;
}
.component-content {
font-size: 1.2em;
margin-bottom: 1em;
}
.component-footer {
margin-top: 1em;
}
h3 {
color: darkblue;
text-align: center;
}
button {
display: block;
margin: 1em auto;
padding: 0.5em 1em;
font-size: 1em;
background-color: blue;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: darkblue;
}
JavaScript
<!--App.svelte-->
<script>
import './global.css';
let isActive = false;
</script>
<main class="component-wrapper">
<h3>Global CSS with Component-Specific Classes</h3>
<button on:click={()=> isActive = !isActive}>
Toggle Component Style
</button>
<div class="component-style {isActive ? 'active' : 'inactive'}">
<p>This component uses global CSS for styling.</p>
</div>
</main>
Output:
Similar Reads
How to target components around or within other elements in CSS ?
In this article, we will see how can we target components around or within other elements. This task can be achieved by using the target selector. The target selector in CSS is used to represent a unique element with an id attribute matching the URLâs fragment and It can be used to style the current
2 min read
How to change svg icon colors with Tailwind CSS ?
SVG stands for Scalable Vector Graphics and is an XML-based ( can be edited ) vector image format. SVG is commonly used for icons, animations, interactive charts, graphs, and other dynamic graphics in the browser. As it is XML-based, you can easily edit or change the SVG icon colors with Tailwind. A
3 min read
How to Work with Fill & Stroke Color in CSS ?
Fill color is the interior color of an element and the Stroke Color is the color that is applied to the outline of the element. we will work with this fill and stroke color in CSS on various SVG shapes. These are the following approaches: Table of Content Using Different Fill and Stroke Colors on SV
3 min read
How to use CssBaseLine Component in ReactJS ?
In React JS, A CssBaseline component is a collection of HTML elements and attribute style-normalizations. At the <head> of our document, it is added as CSS reset. This component is used to add some basic styling to our application like box-sizing, background color, etc like CSS reset for our a
3 min read
Onsen UI CSS Component List Item with Subtitle
Onsen UI CSS is used to create beautiful HTML components. It is one of the most efficient ways to create HTML5 hybrid components that are compatible with both mobile and desktop. Onsen UI CSS Component List Item with Subtitle is used to create the subtitle of the list item using the list-item__subti
2 min read
How to Conditionally Set Background Color in React Component with Tailwind CSS?
In React, you might often need to change the background color of a component based on certain conditions, like highlighting a box if a value is "true" or changing it based on user input. With Tailwind CSS, you can handle this easily using its utility classes, which allow you to conditionally apply s
4 min read
Onsen UI CSS Component List Item with Chevron
Onsen UI CSS is used to create beautiful HTML components. It is one of the most efficient ways to create HTML5 hybrid components that are compatible with both mobile and desktop. Onsen UI CSS Component List Item with Chevron is used to create the list items with the chevron using the list-item--chev
2 min read
Onsen UI Card CSS Components
Onsen UI CSS is used to create beautiful HTML components. It is one of the most efficient ways to create HTML5 hybrid components that are compatible with both mobile and desktop. In this article, we are going to implement the Onsen UI Card CSS Components. A Card is an HTML component used to show dat
3 min read
How to Change Selected Text Background Color in CSS?
Sometimes, we need to change the color and background color of the selected text. The ::selection pseudo-element is used to change the background color of the selected text in CSS. This pseudo-element allows you to style the portion of text that has been selected by the user. Using ::selection Pseud
2 min read
Onsen UI CSS Component Toolbar Item
Onsen UI CSS is used to create beautiful HTML components. It is one of the most efficient ways to create HTML5 hybrid components that are compatible with both mobile and desktop. In this article, we will see the Onsen UI CSS Component Toolbar Item. The toolbar is a horizontal bar with some toolbar i
2 min read