DEV Community

Alex Carpenter
Alex Carpenter

Posted on • Edited on

7 2

Build your own grid with flexbox

There are plenty of ways to layout content on a website. Most revolve around some type of grid setup. In this screencast I walk through a simple and effective way to build your own grid setup using flexbox.

Here is the CSS needed to get you started:

:root {
  --grid-gutter: 20px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.o-grid {
  display: flex;
  flex-wrap: wrap;
  margin-bottom: calc(var(--grid-gutter) * -1);
  margin-right: calc(var(--grid-gutter) * -1);
  margin-left: calc(var(--grid-gutter) * -1);
}

.o-grid__cell {
  width: 100%;
  margin-bottom: var(--grid-gutter);
  padding-right: var(--grid-gutter);
  padding-left: var(--grid-gutter);
}

@media screen and (min-width: 700px) {
  .u-md-width-6of12 {
    width: 50%;
  }
}
Enter fullscreen mode Exit fullscreen mode

and the markup:

<div class="o-grid">
  <div class="o-grid__cell u-md-width-6of12">
    <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nihil deleniti, nesciunt magnam iusto exercitationem fuga at ipsa laborum autem. Assumenda impedit vel nam illum molestiae architecto hic commodi corrupti rem.</p>
  </div>

  <div class="o-grid__cell u-md-width-6of12">
    <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nihil deleniti, nesciunt magnam iusto exercitationem fuga at ipsa laborum autem. Assumenda impedit vel nam illum molestiae architecto hic commodi corrupti rem.</p>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

Helpful links:

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (4)

Collapse
 
equinusocio profile image
Mattia Astorino

Flexbox is not for grid 👍🏻

Collapse
 
hybrid_alex profile image
Alex Carpenter

How so? I use this same grid setup with flexbox everyday at my job.

Collapse
 
equinusocio profile image
Mattia Astorino • Edited

Read about grid-layout and 2 dimensional spaces. Flexbox is content and flow based.

Collapse
 
prol profile image
Danny Prol

Wow, this is so useful.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay