In Svelte.js, an application is made of one or more components. A component is a reusable, self-contained block of code that encapsulates HTML, CSS and JavaScript that belong together. The components are the building blocks of Svelte applications and are written into .svelte files using a superset of HTML. The 'Hello World' example in the code editor is a simple component.
The <script> section
The <script> section contains the JavaScript that runs when a component instance is created. The variables declared at the top level are 'visible' from the component's markup. Svelte handles the component state by top-level variables, and they are reactive by default.
- <script>
- export let name;
- </script>
The markup section
In this section you can insert any HTML. You can also insert here valid JavaScript expression inside single curly brackets ({}).
- <main>
- <h1>Hello {name}!</h1>
- <p>Visit the <a href="https://abc.com">Svelte tutorial</a> Here you can learn Svelte.</p>
- </main>
The <style> section
This is used to add CSS in the application.
For example:
- <style>
- main {
- text-align: center;
- padding: 1em;
- max-width: 240px;
- margin: 0 auto;
- }
- h1 {
- color: #ff3e00;
- text-transform: uppercase;
- font-size: 4em;
- font-weight: 100;
- }
- @media (min-width: 640px) {
- main {
- max-width: none;
- }
- }
- </style>