Hello! Welcome back to our course on building a professional accounting website with Astro.
In the last lesson, you successfully set up your first Astro project, started the development server, and got a tour of the default folder structure. You now have a running project and a solid understanding of where different files belong.
Today, we will bring that structure to life. The goal of this lesson is to create a basic Astro page using HTML and scoped CSS. You'll learn the fundamental syntax of .astro files, which are the core building blocks of any Astro site. This is where you'll start writing the actual code that your users will see in their browser.
Let's dive in and transform that default welcome page into the first piece of your accounting website.
1. The Anatomy of an Astro Page
First, let's understand the structure of an .astro file. Open your project in your code editor and navigate to src/pages/index.astro.
An Astro file is composed of two main parts:
- The Component Script (Code Fence): This is the section at the top of the file enclosed by
---code fences. You'll write your JavaScript or TypeScript here. This code runs on the server during the build process to fetch data, define variables, and handle any logic needed to render your page. It is never sent to the client's browser. - The Template: This is the section below the code fence. It looks like standard HTML, but with some extra capabilities. This is where you define the structure and content of your page. You can use JavaScript expressions from the component script directly in your template.
This structure allows you to keep your server-side logic and your presentation markup in the same file, a pattern that makes components easy to reason about.
For a concise overview of how Astro handles pages, take a moment to review the official documentation.
The "Pages" documentation explains how files in the src/pages/ directory become pages on your site. This reinforces the concept of file-based routing that we touched on in the last lesson.
Read the sections titled "Supported page files," "File-based routing," and "Astro Pages." This will solidify your understanding of how .astro files in this directory function.
2. Creating Your First Page with HTML and JavaScript
Let's modify the index.astro file to create a simple homepage. The following video gives a great practical demonstration of clearing out the default file and adding new content.
In the "Astro Crash Course" by Traversy Media, you'll see a hands-on walkthrough of creating a basic page, defining variables in the code fence, and injecting them into the HTML template.
Watch the segment from 00:10:18 to 00:12:50. Focus on how the .astro file is cleared, how a JavaScript variable is defined in the code fence, and how it's used in the HTML template with curly braces {}.
Now, let's apply this to our project.
- Open
src/pages/index.astro. - Delete all the existing content in the file.
- Add the following code:
---
// Server-side JavaScript/TypeScript goes here.
// This code runs at build time, not in the browser.
const pageTitle = "Apex Financials";
const tagline = "Your trusted partner in financial clarity and growth.";
---
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>{pageTitle}</title>
</head>
<body>
<h1>{pageTitle}</h1>
<p>{tagline}</p>
</body>
</html>
Save the file. If your development server is still running (npm run dev), your browser should automatically refresh to show a simple page with a heading and a paragraph.

Notice how we defined pageTitle and tagline in the component script and then used them in the template with {}. This syntax should feel familiar from your experience with JSX in React.
3. Styling with Scoped CSS
One of Astro's most powerful features is how it handles CSS. When you add a <style> tag to an .astro file, the styles you write are scoped by default. This means they will only apply to the elements within that specific file and won't "leak" out to affect other pages or components. This is a huge advantage for creating a maintainable and scalable website, a key part of your project goal.
Let's read the official documentation to understand this concept thoroughly.
The "Styles and CSS" documentation provides the definitive explanation of how styling works in Astro, particularly the concept of scoped styles.
Read the sections "Styling in Astro" and "Scoped Styles." Pay close attention to the compiled output example, which shows how Astro adds a unique data- attribute to enforce scoping.
To see this in action, watch this short clip.
Astro makes websites faster & easier to build
Kevin Powell provides an excellent visual explanation of scoped styles, showing how you can write simple CSS selectors without worrying about them affecting other parts of your site.
Watch from 05:55 to 07:31. Notice how he removes class names and styles the ul element directly, and then inspects the browser to see the unique data attribute Astro has added.
Now, let's add some scoped styles to our index.astro page. Add the following <style> tag at the bottom of your file, below the closing </body> tag.
<style>
/* These styles ONLY apply to this page (index.astro) */
body {
font-family: system-ui, sans-serif;
background-color: #f4f6f8;
color: #1a2b48;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
text-align: center;
}
h1 {
font-size: 3rem;
font-weight: 800;
margin-bottom: 0.5rem;
}
p {
font-size: 1.25rem;
color: #5c677d;
}
</style>
Save the file. Your homepage should now have a clean, centered layout with professional-looking fonts and colors.
Test your understanding!
Now it's your turn to apply what you've learned.
- Create a new file in your project:
src/pages/about.astro. - In this new file, create a simple "About Us" page with a heading
<h2>and a paragraph<p>. - Add a
<style>block toabout.astroand give theh2a different color (e.g.,green). - Navigate to
http://localhost:4321/aboutin your browser to see your new page. - Now, navigate back to your homepage (
http://localhost:4321). Does the<h1>on the homepage have the green color you applied to theh2on the about page?
Show answer
No, it should not. The styles you wrote in about.astro are scoped to that file only and do not affect any elements on index.astro. This demonstrates the power of scoped styling in preventing unintended style conflicts across your site.
Your about.astro file might look something like this:
---
const pageTitle = "About Apex Financials";
---
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{pageTitle}</title>
</head>
<body>
<h2>Our Mission</h2>
<p>We are dedicated to empowering businesses with financial insight.</p>
</body>
</html>
<style>
h2 {
color: green;
text-align: center;
}
</style>
4. A Note on Global Styles
While scoped styles are perfect for individual pages and components, you'll also need some styles to be global—like CSS resets, font definitions, and base styles for the body.
The recommended way to handle this in Astro is to create a central CSS file and import it. Let's set that up.
- Inside the
src/folder, create a new folder namedstyles. - Inside
src/styles/, create a new file namedglobal.css. - Add some basic global styles to
global.css:
/* src/styles/global.css */
/* A simple CSS Reset */
html {
box-sizing: border-box;
font-size: 16px;
}
*, *:before, *:after {
box-sizing: inherit;
}
body, h1, h2, h3, h4, h5, h6, p, ol, ul {
margin: 0;
padding: 0;
font-weight: normal;
}
/* More global styles can be added here later */

To apply these styles, you must import the stylesheet into your page. Open src/pages/index.astro and add the import statement at the very top of your component script (the code fence).
---
import '../styles/global.css'; // <-- Add this line
const pageTitle = "Apex Financials";
const tagline = "Your trusted partner in financial clarity and growth.";
---
<!-- Rest of the file remains the same -->
Although you won't see a dramatic visual change, these global styles are now being applied before your scoped styles, creating a consistent base for your entire site. We'll soon move this import into a shared layout component so we don't have to repeat it on every page.
Conclusion
Excellent work! You've taken a major step from project setup to actual page creation. You now have the fundamental skill needed to build any page in Astro.
Here are the key takeaways from this lesson:
- An
.astrofile consists of a component script (---) for server-side logic and a template for HTML-like markup. - You can embed JavaScript expressions from the script into your template using
{}. - The
<style>tag provides CSS scoped by default, which prevents style conflicts and improves maintainability. - Global styles can be managed in a separate
.cssfile and imported into your pages.
Up Next: In the next lesson, we will focus on building reusable UI elements using Astro's component syntax. You'll learn how to break down your pages into smaller, manageable components like headers, buttons, and cards. This is the key to creating a clean, scalable, and maintainable architecture for your accounting website.