MAIS AMOR POR FAVOR

SvelteKit Next-Gen Background Image

SvelteKit Next-Gen Background Image

SvelteKit Next-Gen Background Image

SHARE:

πŸ”₯ SvelteKit WebP Background Image with Fallback

A SvelteKit Next-Gen background image is not too difficult to generate. In this video we clarify what we are taking about by Next-Gen background images. Then we see how you can use the vite-imagetools plugin automatically to generate WebP background images for you. These have the advantage of being smaller than less cutting-edge formats such as JPEG and PNG. The upshot is the page loads faster, users get a better experience and Google becomes more comfortable with ranking your site higher.

We are talking about an image that you set as the background of a DOM element using CSS. The trickiest part here is not creating the modern format image, but making sure a fallback JPEG is shown in browsers which don't yet support Next-Gen formats.

If you are looking for how to add responsive, Next-Gen images as img or picture elements, then you should certainly check out the recent article which covers exactly that.

πŸ“Ή SvelteKit Next-Gen Background Image: Video

SvelteKit Next-Gen Background Image

πŸ–₯ Code

Configure vite-imagetools

svelte.config.js β€” click to expand code.
svelte.config.js
javascript
1import adapter from '@sveltejs/adapter-static';
2import preprocess from 'svelte-preprocess';
3import { imagetools } from 'vite-imagetools';
4
5const config = {
6 preprocess: [
7 preprocess({
8 scss: {
9 prependData: "@import 'src/lib/styles/variables.scss';",
10 },
11 }),
12 ],
13 kit: {
14 adapter: adapter(),
15 // hydrate the <div id="svelte"> element in src/app.html
16 target: '#svelte',
17 vite: {
18 plugins: [imagetools({ force: true })],
19 },
20 },
21};
22
23export default config;
Configuring vite-imagetools

SvelteKit Next-Gen Background Image Svelte Code

src/routes/lazyload.svelte β€” click to expand code.
src/routes/lazyload.svelte
svelte
1<script>
2 import { browser } from '$app/env';
3 import backgroundImage from '$lib/assets/sunflower.jpg';
4 import backgroundImageWebp from '$lib/assets/sunflower.jpg?format=webp';
5
6 function serveWebp() {
7 return browser && document.documentElement.classList.contains('webp');
8 }
9
10 const backgroundImageUsed = serveWebp() ? backgroundImageWebp : backgroundImage;
11</script>
12
13<svelte:head>
14 <title>Sunflower Poem</title>
15 <meta name="description" content="A poem about sunflowers" />
16 <meta name="theme-color" content="#ffdf00" />
17 <link rel="apple-touch-icon" href="/icons/icon-192x192.png" />
18 <script
19 async
20 integrity="sha384-xms8Nhw0czGEIWf0BZpCsaCY9PPeFf6bJ6cG0CNYoVvDK3M0146DNIywperKRSNI"
21 src="/modernizr-webp.js"
22 type="text/javascript"></script>
23</svelte:head>
24
25<div class="container" style={`background-image: url(${backgroundImageUsed})`}>
26 <div class="panel">
27 <div class="content">
28 <h1>The Soul of the Sunflower</h1>
29 <h2>by Sara Jewett</h2>
30 <blockquote>
31 <p>The warm sun kissed the earth</p>
32 <p>To consecrate thy birth,</p>
33 <p>And from his close embrace</p>
34 <p>Thy radiant face</p>
35 <p>Sprang into sight,</p>
36 <p>A blossoming delight.</p>
37 </blockquote>
38 </div>
39 </div>
40</div>
41
42<style lang="scss">
43 .container {
44 display: flex;
45 background-color: $color-theme-2;
46 background-position: center center;
47 background-size: cover;
48 width: 100%;
49 min-height: 100vh;
50 padding: $spacing-12;
51 }
52
53 .panel {
54 background: rgba($color-theme-3, $alpha: 0.81);
55 box-shadow: $spacing-0 $spacing-0 $spacing-1 $spacing-0 $color-theme-4;
56 border-radius: $spacing-4;
57 color: #502419;
58 margin: auto;
59 padding: $spacing-6 $spacing-24;
60 height: 100%;
61 }
62
63 .content {
64 padding: $spacing-4;
65 }
66
67 h1,
68 h2,
69 p {
70 font-family: Playfair Display, Times New Roman, Times, serif;
71 }
72 p {
73 font-weight: $font-weight-bold;
74 font-family: Lato, Playfair Display, Times New Roman, Times, serif;
75 font-size: $font-size-2;
76 }
77</style>
SvelteKit Next-Gen Background Image: Svelte code.

You can see the full code for creating a SvelteKit Next-Gen background image on the Rodney Lab Git Hub repo .

πŸ—³οΈ Poll

I try to keep these videos short and to the point, though this one ended up a little longer. Do you mind longer videos?
Voting reveals latest results.

πŸ™πŸ½ SvelteKit Next-Gen Background Image: Feedback

If you have found this video useful, see links below for further related content on this site. I do hope you learned one new thing from the video. Let me know if there are any ways I can improve on it. I hope you will use the code or starter in your own projects. Be sure to share your work on Twitter, giving me a mention so I can see what you did. Finally be sure to let me know ideas for other short videos you would like to see. Read on to find ways to get in touch, further below. If you have found this post useful, even though you can only afford even a tiny contribution, please consider supporting me through Buy me a Coffee.

Finally, feel free to share the post on your social media accounts for all your followers who will find it useful. As well as leaving a comment below, you can get in touch via @askRodney on Twitter and also askRodney on Telegram . Also, see further ways to get in touch with Rodney Lab. I post regularly on SvelteKit as well as Search Engine Optimisation among other topics. Also subscribe to the newsletter to keep up-to-date with our latest projects.

Thanks for reading this post. I hope you found it valuable. Please get in touch with your feedback and suggestions for posts you would like to see. Read more about me…

Rodney from Rodney Lab

Leave a comment …

Your information will be handled in line with our Privacy Policy.