Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Practical Astro
Lesson Plan
  1. Content Systems
    1. Introduction
    2. An import alias
    3. Using Markdown
    4. Markdown extended
    5. Markdown component
    6. Markdown component in frameworks
    7. Content collections made easy
    8. Basic blogs
    9. Blogs made easy
    10. Prev/Next navigation
    11. Pagination made easy
    12. Better tag pages
    13. Paginated tag pages
    14. Markdown layouts
    15. Partials
    16. JS over JSON Data
    17. Dynamic 404 pages
    18. Putting everything together
    19. Other useful systems
  2. Image Systems
    1. Introduction
    2. Images are complicated because of this
    3. Astro's Image component
    4. Astro's Picture component
    5. Improvements over Astro's Image and Picture Components
    6. Images in Markdown and MDX
    7. Images in framework components
    8. Background images
    9. Art Direction with Astro
    10. Videos without layout shifts
    11. GIF Replacements and background video
    12. SVGs and icons
    13. SVG backgrounds
    14. Adding noise to images
    15. Asset caching on GitHub Actions
    16. Putting everything together
    17. Other useful systems

Background images

2m:40s

Background images are usually set up in CSS with the background-image property. They aren’t automatically optimized because they don’t pass through Astro’s image optimization pipeline.

.selector {
background-image: url('...');
}

But there are two ways to optimize these background images:

  1. Use an inline background-image property
  2. Use a real image with object-fit: cover

We’ll go through both methods here.

Using an inline background-image property

The trick here is to optimize the image via Astro’s getImage function before passing it into the CSS background-image property.

In this case, we don’t need to insert widths because background-image does not support any width-based srcset/sizes mechanism.

---
import { getImage } from 'astro:assets'
import myBackground from '../assets/background.png'
const background = await getImage({ src: myBackground, format: 'avif' })
---
<div
style={`background-image: url(${background.src}); background-size: cover;`}
>
</div>

I also made a component called BackgroundImage to simplify the process.

---
import { BackgroundImage } from '@splendidlabz/astro'
---
<BackgroundImage src="/src/assets/hero.jpg">
<!-- Your content goes here -->
</BackgroundImage>

Using a real image with object-fit: cover

I prefer this method because we can actually deliver the best experience for users — by using an img with srcset.

To do this, we need to put both the image and the content inside a stack — which is basically a CSS Grid with children overlaid onto each other.

Here’s what the HTML looks like:

<div class="stack">
<!-- image goes here -->
<div><!-- content goes here --></div>
</div>

And here’s what stack looks like:

/* Simple version of stack */
.stack {
display: grid;
position: relative;
grid-template-columns: minmax(0, 1fr);
> * {
grid-column: 1 / span 1;
grid-row: 1 / span 1;
}
}

We can use Astro’s Picture component to build the image. We’ll include widths here to let browsers download an image of the appropriate size, and formats to produce modern formats like avif and webp.

---
import { Picture } from 'astro:assets'
import image1 from '../assets/image1.png'
---
<div class="stack">
<Picture
src={image1}
alt=""
widths={[400, 800, 1200]}
formats={['avif', 'webp']}
/>
<div><!-- content goes here --></div>
</div>

The important part here is to add a class to the image, along with the following CSS.

This overrides Astro Picture’s tendency to preserve the image’s intrinsic sizing — which allows the image to be a background image that conforms to the size of the content.

---
import { Picture } from 'astro:assets'
import image1 from '../assets/image1.png'
---
<div class="stack">
<Picture class="object-background" src={image1} alt="" ... />
<div><!-- content goes here --></div>
</div>
.object-background {
position: absolute;
z-index: -1;
inset: 0;
width: 100%;
height: 100%;
object-fit: cover;
}

To make things simpler, Splendid Astro’s Image component has a backgroundMode property that does this for you automatically.

---
import { Image } from '@splendidlabz/astro'
---
<div class="stack">
<Image src="/src/assets/image1.png" backgroundMode />
<div><!-- content goes here --></div>
</div>

Code for you to copy

Using background-image

---
import { BackgroundImage } from '@splendidlabz/astro'
---
<BackgroundImage src="/src/assets/hero.jpg">
<!-- Your content goes here -->
</BackgroundImage>

Using a real image

---
import { Image } from '@splendidlabz/astro'
---
<div class="stack">
<Image src="/src/assets/hero.jpg" backgroundMode />
<div><!-- content goes here --></div>
</div>