Getting Started with Astro Layouts


Get the latest articles delivered to your inbox. No spam, unsubscribe anytime.
One of the key tools in a web framework, is its ability to create many different variations of page templates, which can arrange the elements in a page in any number of different ways, this is commonly known as pay layouts.
The Astro web framework provides great flexibility when it comes to creating diverse page layouts, while also keeping it simple and easy to maintain.
In this article, I'll be covering the essential aspects of Astro layouts, including:
Astro layouts are essentially a way to compose a full HTML page template with the ability to insert different content while also reusing the same component arrangements.
Whatever the case might be, the purpose of any layout is to render a complete HTML document that's ready for a browser to render.
If your collaborating with a UX designer, you may have some design requirements for a wide variety of layouts. For example:
The list goes on and you're most likely going to need a way to standardize and manage these different types of page and component layouts.
Since Astro is a content-driven framework, we'll need a way to insert the Content somewhere on the page. Here are some common use cases for layouts.
In this layout the content might be centered with varying margins on the side, while still showing a header and footer.

In this layout the content would take the full width but still show a header and footer.

In this layout the main content would have a secured role based section like a sidebar navigation, while still showing a header and footer.

In this layout the content would float in the center space and may or may not show a header and footer. This is a very common design strategy for login, forgot password and registration pages.

In this layout the content would take up the full screen and not show a header and footer.

These are just some of most common layouts to get an idea but Astro is capabable of meeting any of these needs.
From years of experience in building web layouts, it's going to be essential to decide on a CSS framework to use. Since web pages can vary from Mobile to Desktop, having a CSS framework is key.
Here's some top frameworks and strategies to name a few.
As JavaScript applications start to trend towardsserver side renderingand evenstatic site generation, it's best to pick a CSS framework that doesn't have runtime rendering requirements, or what's now calledzero runtime. This is optional but something to consider.
For this article I'm going to use Tailwind CSS since Astro provides great support for it and it provides support for a responsive design.
I highly recommend using an Astro extension or plugin for your IDE when editing .astro file types so it properly formats the syntax and provides some code intellisense.
If you don't already have Astro installed, let's start there first.
To install Astro simply use the generator in a terminal. Choose the following options in the terminal prompts:
astro-layouts1npm create astro@latest
Once Astro is installed, switch into your new project directory.
1cd astro-layouts23npm install
To add Tailwind CSS, simply use the add command.
1npx astro add tailwind
In order to activate Tailwind CSS utilities on page we'll also need to create a style.css page also.
1@tailwind base;2@tailwind components;3@tailwind utilities;45/*6 Any custom CSS here, Tailwind can also be7 configured for nested Sass syntax or even8 be used to together with Sass9*/
Astro has the following folder structures to get things started. Inside the /src directory we have the following directories.
/layouts: Add any layouts for the application here./pages: Add any pages here./components: Add any custom components here.Let's create the Standard layout for this tutorial.
Create a new file in the layouts directory and give it the filename StandardLayout.astro.
Add the following content to the new layout file.
1---2import "../styles/styles.css";3import Header from "../components/ui/Header.astro";4import Footer from "../components/ui/Footer.astro";56type Props = {7 title: string;8 description: string;9};1011const { title, description } = Astro.props;12---1314<html lang="en">15 <head>16 <title>{title}</title>17 <meta name="description" content={description} />18 </head>19 <body class="bg-white text-gray-900">20 <main class="flex flex-col">21 <Header />22 <div class="w-[80%] flex sm:justify-center sm:mx-24 p-10">23 <slot />24 </div>25 <Footer />26 </main>27 </body>28</html>
In Astro, the top section of code is surrounded in opening and closing --- delimiters, commonly referred to as fencing.
This is where you can put any JavaScript ESM code which Astro will use to build a static page.
This is where all import statements should be located. As you can see it's importing the following:
styles.css<Header><Footer>Also, it uses TypeScript to declare the Props type which includes:
titledescriptionThese props will be passed into the StandardLayout when the layout is used by pages. We'll define the page in the next section and show how the props are passed.
The most important thing to note is that Astro uses a <slot /> tag which is where the page content will be inserted.
Here's the rest of the components we'll need.
The <Header> component just creates a simple navbar with responsive support and collapsable menu.
Notice how the JavaScript to toggle the menu is added inside standard <script> tags and not inside the fencing code block ---.
Any JavaScript code that needs access to the document or window object must be added in standard <script> tags since the browser objects are not available in the Astro fencing block since it runs at build time and not browser runtime.
1<nav id="navbar" class="relative flex py-3 border-b">2 <div class="w-full flex justify-between items-center flex-wrap mx-3">3 <div class="flex items-center shrink-0">4 <a href="#" class="flex items-center space-x-3 rtl:space-x-reverse">5 <span6 class="self-center text-3xl font-bold whitespace-nowrap dark:text-white bg-clip-text text-transparent bg-gradient-to-r from-red-600 to-purple-800"7 >Astro Layouts</span8 >9 </a>10 </div>11 <button12 id="navbar-toggler"13 aria-controls="nav-toggle"14 aria-expanded="false"15 class="lg:hidden cursor-pointer rounded-md p-2"16 ><span class="sr-only">Menu</span>17 <svg18 class="w-6 h-6 fill-current text-gray-900"19 viewBox="0 0 24 24"20 xmlns="http://www.w3.org/2000/svg"21 ><rect y="4" width="24" height="2"></rect><rect22 y="11"23 width="24"24 height="2"></rect><rect y="18" width="24" height="2"></rect></svg25 >26 </button27 ><div28 id="navbar-collapse"29 class="basis-full grow items-center lg:font-semibold lg:mx-6 lg:flex lg:justify-between lg:basis-auto hidden"30 >31 <ul32 id="navbar-nav"33 class="flex flex-col overflow-y-auto border lg:border-none shadow-md lg:shadow-none bg-white [&>li]:p-2 lg:flex-row [&>li]:lg:mx-3 [&>li]:lg:border-none"34 >35 <li>36 <a37 href="#"38 class="no-underline hover:underline"39 style="text-decoration: none;">Home</a40 >41 </li><li>42 <a43 href="#"44 class="no-underline hover:underline"45 style="text-decoration: none;">About</a46 >47 </li><li>48 <a49 href="#"50 class="no-underline hover:underline"51 style="text-decoration: none;">Blog</a52 >53 </li>54 </ul>55 </div>56 </div>57</nav>5859<script>6061 const navbarElement = document.querySelector("#navbar-toggler");62 const navbarCollapse = document.querySelector("#navbar-collapse");6364 navbarElement?.addEventListener("click", function (e) {65 if (navbarCollapse?.classList.contains("hidden")) {66 navbarCollapse?.classList.remove("hidden");67 } else {68 navbarCollapse?.classList.add("hidden");69 }70 });71</script>
Here's the footer which is just static HTML with Tailwind CSS classes.
1<footer>2 <div class="bg-gray-600 text-white p-10 border-t fixed w-full bottom-0">3 <ul id="navbar-nav" class="flex space-x-5">4 <li>5 <a href="#">Home</a>6 </li><li>7 <a href="#">About</a>8 </li><li>9 <a href="#">Blog</a>10 </li>11 </ul>12 </div>13</footer>
Now that we have the StandardLayout and all the components defined, we can create a page that uses the layout.
All that's needed is to simply import the layout and use it.
Since it's the default export I'll give it a generic name Layout.
Add all of the page content inside the <Layout> tag so it can be inserted into the <Layout> using the <slot/> tag defined in the layout.
Notice how the title and description are being passed as attribute props on the <Layout>.
1---2import Layout from "../layouts/StandardLayout.astro";3---45<Layout title="Home" description="Astro Layouts Demo">6 <div class="h-96">7 <div class="text-4xl font-bold mb-5">Building Layouts in Astro</div>8 <ul class="text-xl list-disc list-inside">9 <li>Layout with a header and footer</li>10 <li>Centered content</li>11 <li>Responsive navigation</li>12 </ul>13 </div>14</Layout>
Now we can start Astro using npm run dev and open a browser and go to localhost:4321/home and see the page in the layout.
Astro definitely makes it easy to create layouts and it's definitely one of my go to frameworks for building static websites.
One of the other key benefits is that you can use standard HTML syntax and inline JavaScript in a more traditional development approach, while also enjoying strong TypeScript support.
Astro is also completely neutral when it comes to UI technologies and CMS integrations, making it a flexible framework to build powerful statically optimized websites using your favorite tools.


