Logo
Services
Industries
Technologies
About
Case Study
Blog

Let's Build Your Tech Vision

120+ expert engineers & designers in AI, Web, Mobile.

General Inquiries

hr@mediusware.com+880 1897-661850-51

Discuss Services

sales@mediusware.com+880 1750-020408

Mediusware 2015 - 2026. All Rights Reserved

Quick Links

Our StoriesLeadershipBlogsContactCareerCSR Terms & ConditionsPrivacy Policy

Services

Software DevelopmentWeb DevelopmentMobile App DevelopmentE-commerce DevelopmentQA & DevOpsDigital MarketingArtificial IntelligenceBranding & Visualization

Technology

React.jsVue.jsAngular jsLaravelDjangoFlutterRustReact Native

Industries

RetailE-learningTravelHealthcareLogisticsEntertainmentFintechReal Estate
Blog/
  1. Understanding the Basics
  2. What is Flexbox?
  3. Setting Up a Simple Layout
ChatGPT
ChatGPT
Google AI Studio
Google AI Studio
Claude
Claude
Grok
Grok
Perplexity
Perplexity

Mastering CSS Grid and Flexbox for Complex Layouts

Learn the essential skills and steps to become a full stack developer. Start your journey today with this comprehensive guide for beginners!
Mastering CSS Grid and Flexbox for Complex Layouts image
Navigate
  1. Understanding the Basics
  2. What is Flexbox?
  3. Setting Up a Simple Layout

Understanding the Basics

What is CSS Grid?

CSS Grid is a two-dimensional layout system that allows you to control both rows and columns in a grid container. You can create responsive layouts with flexible grid items that adjust according to screen size.

.grid-container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: auto;
  gap: 20px;
}

.grid-item {
  background-color: #f0f0f0;
  padding: 20px;
  text-align: center;
}

What is Flexbox?

Flexbox is a one-dimensional layout system that helps control the alignment, distribution, and spacing of items inside a container. It's perfect for handling rows or columns individually.

.flex-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.flex-item {
  background-color: #ccc;
  padding: 10px;
}

Setting Up a Simple Layout

Let’s start by setting up a simple layout that uses both Grid and Flexbox for a responsive page structure.

Explanation:

  • The grid-template-areas define different regions of the layout.
  • grid-template-columns: 1fr 3fr 1fr sets up the layout with a flexible sidebar, main content, and navigation.
  • Flexbox is used within the header, footer, nav, and sidebar to align and space out the content properly.
HTML Structure:

<div class="grid-container">
  <header class="grid-item header">Header</header>
  <nav class="grid-item nav">Navigation</nav>
  <main class="grid-item main">Main Content</main>
  <aside class="grid-item sidebar">Sidebar</aside>
  <footer class="grid-item footer">Footer</footer>
</div>

CSS (Grid for Layout, Flexbox for Content Alignment):

/* Grid Layout */
.grid-container {
  display: grid;
  grid-template-areas: 
    "header header header"
    "nav main sidebar"
    "footer footer footer";
  grid-template-columns: 1fr 3fr 1fr;
  grid-template-rows: auto 1fr auto;
  gap: 20px;
  height: 100vh;
}

.header { grid-area: header; }
.nav { grid-area: nav; }
.main { grid-area: main; }
.sidebar { grid-area: sidebar; }
.footer { grid-area: footer; }

/* Flexbox for Alignment Inside Grid Items */
.header, .footer {
  display: flex;
  justify-content: center;
  align-items: center;
}

.nav, .sidebar {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
}

Frequently Asked Questions

Use CSS Grid when you need to create a two-dimensional layout, meaning you are managing both rows and columns. Grid is ideal for building overall page structures like layouts that involve a header, sidebar, main content, and footer. On the other hand, Flexbox is best for one-dimensional layouts where you only need to manage the items in a row or column, like centering elements, aligning buttons, or organizing items within a component.

Author
By Mediusware Editorial Team

Content Team at Mediusware

We are the Mediusware Editorial Team, passionate about crafting insightful content on technology, software development, and industry trends. Our mission is to inform, inspire, and engage our audience with well-researched articles and thought leadership pieces. With a deep understanding of the tech landscape, we aim to be a trusted source of knowledge for professionals and enthusiasts alike.

Get the best of our content straight to your inbox!

By submitting, you agree to our privacy policy.

Understanding the Basics
What is Flexbox?
Setting Up a Simple Layout
Navigate
Understanding the BasicsWhat is Flexbox?Setting Up a Simple Layout

Get the best of our content straight to your inbox!

By submitting, you agree to our privacy policy.

Featureblogs

01QA Outsourcing: Boost Software Quality and Speed02The Future of NLP: Key Trends03Computer-Assisted Learning: The Future of Education04Is Golang Still the Right Choice in 2025?05Large Language Models in 2025: How They’re Changing the Way We Work and Build

Authorblogs

01Application Software: Types, Benefits, and Future Trends02What Is Wireframing? A Practical Guide03ChatGPT vs. Gemini vs. Claude vs. Copilot vs. Perplexity: What Matters for Real Work042025 Global Recognition Award: Mediusware’s Leadership in Innovation and Impact05React Vulnerability: Critical Security Flaw in React Server Components