Skip to main content

Just like in HTML, you can add a <style> tag to your component. Let's add some styles to the <p> element:

<p>This is a paragraph.</p>

<style>
	p {
		color: purple;
		font-family: 'Comic Sans MS', cursive;
		font-size: 2em;
	}
</style>

Importantly, these rules are scoped to the component. You won't accidentally change the style of <p> elements elsewhere in your app, as we'll see in the next step.

App.svelte
<p>This is a paragraph.</p>

<style>
/* Write your CSS here */
</style>

/* App.svelte generated by Svelte v4.2.19 */
import {
SvelteComponent,
detach,
element,
init,
insert,
noop,
safe_not_equal
} from "svelte/internal";

import "svelte/internal/disclose-version";

function create_fragment(ctx) {
let p;

return {
c() {
p = element("p");
p.textContent = "This is a paragraph.";
},
m(target, anchor) {
insert(target, p, anchor);
},
p: noop,
i: noop,
o: noop,
d(detaching) {
if (detaching) {
detach(p);
}
}
};
}

class App extends SvelteComponent {
constructor(options) {
super();
init(this, options, null, create_fragment, safe_not_equal, {});
}
}

export default App;
result = svelte.compile(source, {
generate:
css:
});
/* Add a <style> tag to see compiled CSS */