a first Sveltekit + TailWindCSS project from scratch
Captioned Video Walkthrough of this exercise:
NOTE: If your workbench/IDE creates a new project folder for you (such as Celbridge), then you can skip the first 2 steps...
Create directory for project
>>> mkdir svelte01
cd into directory
>>> cd svelte01
Initialise directory as a SvelteKit project:
we'll use npx sv the Svelte command line tool:
>>> npx sv create
(hit Enter to use './')● SvelteKit minimal (barebones scaffolding for your new app) ● Yes, using JavaScript with JSDoc comments ◼ tailwindcss (css framework - https://tailwindcss.com)◼ typography (@tailwindcss/typography)◼ forms (@tailwindcss/forms)● npmNOTE:
- You'll see that all the above options could be written in a single line, to save time in the future:
```bash
npx sv create --template minimal --types jsdoc --add tailwindcss="plugins:typography,forms" --install npm ./
```
Here's the full output from these interactions at the terminal:
>>> npx sv create
┌ Welcome to the Svelte CLI! (v0.11.3)
│
◇ Where would you like your project to be created?
│ ./
│
◇ Which template would you like?
│ SvelteKit minimal
│
◇ Add type checking with TypeScript?
│ Yes, using JavaScript with JSDoc comments
│
◇ What would you like to add to your project? (use arrow keys / space bar)
│ tailwindcss
│
◇ Which plugins would you like to add?
│ typography, forms
│
◆ Project created
│
◆ Successfully setup add-ons: tailwindcss
│
◇ Which package manager do you want to install dependencies with?
│ npm
│
● Re-run without prompts:
│ npx sv create --template minimal --types jsdoc --add tailwindcss="plugins:typography,forms" --install npm ./
│
│
◆ Successfully installed dependencies with npm
│
◇ What's next? ───────────────────────────────╮
│ 📁 Project steps │
│ │
│ 1: npm run dev -- --open │
├──────────────────────────────────────────────╯
│
└ You're all set!
Let's do what it says! Run the web server
npm run dev -- --open
--open option should automatically open your system default Web Browser to http://localhost:5173/.You also may with to look inside the README.md
You should see something like the following:
This content comes from /src/routes/+page.svelte
Let's replace the content of /src/routes/+page.svelte with our own content
first a <script> to delcare a module name variable
<script>
const moduleName = 'FEDev (Front End Development)';
</script>
next some HTML elements ```html
hello world
about page
<style>, which is local to this page
```html
- here it is altogether:
```html
<script>
const moduleName = 'FEDev (Front End Development)';
</script>
<h1 class="bg-amber-100 p-5 text-2xl">
Welcome to {moduleName}
</h1>
<p>
hello world
<br>
<a href="/about">about page</a>
</p>
<style>
p {
margin: 1rem;
padding: 2rem;
border: solid red 1px;
}
a {
color: green;
}
</style>
The page should automatically refresh within a few seconds, and look as follows:
You'll have noticed we added a link to /about.
But if we click it, we'll get a 404 Not Found Error page, since we've not created that page yet...
Let's create this about page.
SvelteKit routing can be as simple as creating a subdirectory inside /src/routes for each page
create folder /src/routes/about
create file /src/routes/about/+page.svelte to contain the following:
<div class="m-2 p-2 bg-blue-200 rounded-lg">
<a href="/">(home)</a>
<hr class="m-3">
<h1 class="text-2xl">
I'm the About page
</h1>
</div>
Now when you visit the about page link from the home page you should see the following:
Start learning more about Svelte at:
Above we've seen standard CSS in our .svelte pages, and also TailWindCSS styling in the class="" attributes of some HTML elements. If you put a little time into learning TailWindCSS, it can make element styling much faster, and there are nice examples you can copy and paste