Create your first Astro page
Now that you know that .astro files are responsible for pages on your website, it’s time to create one!
Get ready to…
- Create two new pages on your website: About and Blog
- Add navigation links to your pages
- Deploy an updated version of your website to the web
Create a new .astro file
Section titled “Create a new .astro file”-
In the files pane of your code editor, navigate to the folder
src/pages/where you will see the existing fileindex.astro -
In that same folder, create a new file named
about.astro. -
Copy, or retype the contents of
index.astrointo your newabout.astrofile.Your editor might show a solid white circle on the tab label for this file. This means that the file is not yet saved. Under the File menu in VS Code, enable “Auto Save” and you should no longer need to save any files manually.
-
Add
/aboutto the end of your website preview’s URL in the address bar and check that you can see a page load there. (e.g.http://localhost:4321/about)
Right now, your “About” page should look exactly the same as the first page, but we’re going to change that!
Edit your page
Section titled “Edit your page”Edit the HTML content to make this page about you.
To change or add more content to your About page, add more HTML element tags containing content. You can copy and paste the HTML code below between the existing <body></body> tags, or create your own.
<body> <h1>My Astro Site</h1> <h1>About Me</h1> <h2>... and my new Astro site!</h2>
<p>I am working through Astro's introductory tutorial. This is the second page on my website, and it's the first one I built myself!</p>
<p>This site will update as I complete more of the tutorial, so keep checking back and see how my journey is going!</p></body>Now, visit your /about page in your browser tab again, and you should see your updated content.
Add navigation links
Section titled “Add navigation links”To make it easier to preview all your pages, add HTML page navigation links before your <h1> at the top of both of your pages (index.astro and about.astro):
<a href="/">Home</a><a href="/about/">About</a>
<h1>About Me</h1><h2>... and my new Astro site!</h2>Check that you can click these links to move back and forth between pages on your site.
Unlike many frameworks, Astro uses standard HTML <a> elements to navigate between pages (also called routes), with traditional page refreshes.
Try it yourself - Add a Blog page
Section titled “Try it yourself - Add a Blog page”Add a third page blog.astro to your site, following the same steps as above.
(Don’t forget to add a third navigation link to every page.)
Show me the steps.
- Create a new file at
src/pages/blog.astro. - Copy the entire contents of
index.astroand paste them intoblog.astro. - Add a third navigation link to the top of every page:
<body> <a href="/">Home</a> <a href="/about/">About</a> <a href="/blog/">Blog</a>
<h1>My Astro Site</h1></body>