2026 Community Survey results are here! See how the Craft CMS community works. results are live!

What are Astro components?

During this video we'll learn about Astro components and how they work.

Astro com­po­nents are the basic build­ing blocks of Astro. Writ­ing com­po­nents should feel famil­iar to writ­ing HTML (or Twig) and the tran­si­tion to Astro com­po­nents will feel familiar.

There are two parts to an Astro component:

  1. Com­po­nent Script — con­tained between the dashed lines of frontmatter.
  2. Com­po­nent Tem­plate — the main part of the com­po­nent with your markup or any JavaScript expres­sions need­ed to build the page.

In the Com­po­nent Script, you can do any­thing you’d nor­mal­ly do in JavaScript or JavaScript frame­work code: import oth­er code, fetch data, import data, set vari­ables. It’s the place where you get the busi­ness of the com­po­nent done.

The Com­po­nent Tem­plate is where you write the markup that will ren­der your com­po­nent in the brows­er. You can write plain HTML and noth­ing else, and Astro will duti­ful­ly build your page using that com­po­nent. You can also use JavaScript expres­sions (like map­ping data you fetched in the Com­po­nent Script sec­tion). Or, you might need to use Astro direc­tives, like set:html to han­dle data in a spe­cial way. 

Craft Version
Craft 4
Instructor
Ryan Irelan
Level
Beginner
Date Published
March 14, 2023
Ryan Irelan

I am the creator of CraftQuest, a web developer, and former software team manager. I spend most of my time improving CraftQuest with code and courses. When I'm not in front of the computer, I spend my time with my family, and running on the roads and trails of Austin, TX.