DEV Community

Cover image for Chapter 4: Grids, Flexbox, and Responsive Design
Prasun Chakraborty
Prasun Chakraborty

Posted on

Chapter 4: Grids, Flexbox, and Responsive Design

In this chapter, we will transform our portfolio into a fully responsive website. By incorporating CSS Grid and Flexbox, we will ensure your layout adapts smoothly to all screen sizes—desktops, tablets, and smartphones.

Understanding Responsive Design

Responsive design ensures your website looks great and functions seamlessly on any device. It involves using fluid layouts, flexible images, and media queries.

Step 1: CSS Flexbox for Navigation:

  1. Flexbox: A CSS layout model that aligns and distributes items within a container along a single axis (row or column) with flexible sizing.

  2. Flex Container: The parent element that holds flex items and controls their layout using display: flex.

  3. Flex Item: Any direct child of a flex container that can grow, shrink, and align based on Flexbox rules.

nav ul {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
}

nav ul li {
  margin: 0.5rem 1rem;
}
Enter fullscreen mode Exit fullscreen mode
  • display: flex: Turns the <ul> element into a flex container,

  • justify-content: Aligns flex items horizontally with options like center, space-between, or flex-start. Here, It centers the navigation items horizontally.

  • flex-wrap: Ensures navigation links wrap to the next line on smaller screens.

Flexbox = Flexible Box. Justify the main axis, Align the cross axis, and mind the Gap!

Step 2: CSS Grid for Project Layout:

  1. CSS Grid: A 2D layout system that creates rows and columns to precisely position and align elements within a container.

  2. Grid Container: The parent element that defines the grid structure using display: grid.

  3. Grid Item: Any direct child of a grid container placed within the grid cells.

HTML Updates:

<section id="projects">
  <h2>Projects</h2>
  <div class="project-grid">
    <div class="project-item">Project 1</div>
    <div class="project-item">Project 2</div>
    <div class="project-item">Project 3</div>
    <div class="project-item">Project 4</div>
  </div>
</section>
Enter fullscreen mode Exit fullscreen mode

CSS Changes:

.project-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
}

.project-item {
  background-color: #e0f0ff;
  padding: 1rem;
  border-radius: 5px;
  text-align: center;
}
Enter fullscreen mode Exit fullscreen mode
  • display: grid: Activates grid layout.

  • grid-template-columns: Defines responsive columns, automatically fitting and resizing based on available space.

  • gap: Provides consistent spacing between grid items.

Flexbox = 1D (rows or columns), Grid = 2D (rows and columns)

Step 3: Using Media Queries
Media queries enable styling adjustments at specific screen widths.

/* Tablets and smaller screens */
@media (max-width: 768px) {
  header h1 {
    font-size: 2rem;
  }

  nav ul {
    flex-direction: column;
  }

  main {
    padding: 1rem;
  }
}

/* Mobile devices */
@media (max-width: 480px) {
  header {
    padding: 1rem 0;
  }

  header p {
    font-size: 1rem;
  }

  .project-grid {
    grid-template-columns: 1fr;
  }
}
Enter fullscreen mode Exit fullscreen mode
  • Media queries adjust styles depending on screen size.

  • Flexbox directions switch vertically on smaller screens for easier navigation.

  • Grid layouts simplify to single-column formats for readability on smartphones.

Media Queries are like If-Else for CSS.

We now have a portfolio that elegantly adjusts itself to any device. The combination of CSS Grid, Flexbox, and Media Queries provides us with powerful, adaptable layout control.

Here is what we have done so far

Dev Portfolio - Chapter 4 Branch

In the following chapters, we'll enhance your portfolio with interactive elements and advanced styling techniques. Stay tuned and keep exploring!

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.