Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

CSS Topics

CSS grid-template-areas Property

Posted in CSS Tutorial
Updated on Oct 03, 2024
By Mari Selvan
👁️ 22 - Views
⏳ 4 mins
💬 1 Comment
CSS grid-template-areas Property

Photo Credit to CodeToFun

🙋 Introduction

The grid-template-areas property in CSS is part of the CSS Grid Layout module. It allows you to define named grid areas within a grid container, making it easier to design complex layouts with a clear, readable syntax.

This property helps in positioning and aligning grid items within the grid container by assigning them to predefined areas.

💡 Syntax

The syntax for the grid-template-areas property uses a string to represent the grid layout. Each string defines a row in the grid, with named areas enclosed in quotes and separated by spaces.

Syntax
Copied
Copy To Clipboard
container {
  display: grid;
  grid-template-areas: 
    "header header header"
    "sidebar main main"
    "footer footer footer";
}

🎛️ Default Value

The default value of the grid-template-areas property is none, meaning no named grid areas are defined.

🏠 Property Values

ValueDescription
noneNo grid areas are defined.
stringA sequence of strings, each representing a row in the grid and containing named areas separated by spaces.

📄 Example

In this example, we'll create a simple layout with a header, sidebar, main content area, and footer.

index.html
Copied
Copy To Clipboard
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS grid-template-areas Example</title>
  <style>
    .container {
      display: grid;
      grid-template-areas: 
        "header header header"
        "sidebar main main"
        "footer footer footer";
      grid-gap: 10px;
    }

    .header {
      grid-area: header;
      background-color: lightblue;
    }

    .sidebar {
      grid-area: sidebar;
      background-color: lightgreen;
    }

    .main {
      grid-area: main;
      background-color: lightcoral;
    }

    .footer {
      grid-area: footer;
      background-color: lightgoldenrodyellow;
    }

    .container div {
      padding: 20px;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="header">Header</div>
    <div class="sidebar">Sidebar</div>
    <div class="main">Main Content</div>
    <div class="footer">Footer</div>
  </div>
</body>
</html>

🖥️ Browser Compatibility

The grid-template-areas property is well-supported in modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. However, it is always a good practice to test your website across different browsers to ensure compatibility.

🎉 Conclusion

The grid-template-areas property is a powerful feature of CSS Grid Layout, making it easier to create complex and responsive layouts with clean and readable code.

By defining named grid areas, you can efficiently manage the placement of content and maintain a clear structure in your stylesheets. Experiment with different grid layouts and see how this property can enhance your web design projects.

👨‍💻 Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy