Zoltan King
Zoltan King

Reputation: 2292

CSS Grid with Breaking Out Layout. How to set different sizes for content on different pages?

I have a grid layout. I built it using the mobile-first approach which means that everything by default (outside the media query) is for mobile.

The desktop layout looks like so:

enter image description here

I have a "main" content area which is set to be 80em. Let's pretend that's the Home page of the website.

However, what can I do if I need that "main" area to be a little bit narrower on certain pages like the contact page, maybe 70em instead of 80em but still centered.

In the grid I have this:

.grid-layout {
    grid-template-columns:
      minmax(0, 1fr)
      minmax(0, 80em)
      minmax(0, 1fr);
    grid-template-rows: repeat(2, auto) 200px 1fr;
  }

And here is the full code so you can see exactly how it works:

:root {
  box-sizing: border-box;
}

*,
*::before,
*::after {
  box-sizing: inherit;
}

body {
  margin: 0;
  padding: 0;
}

.grid-layout {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: repeat(2, auto) 1fr 1fr;
  min-height: 100vh;
}

.header {
  background: lightsalmon;
}

.main {
  background: lightseagreen
}

.testimonials {
  background: lightgreen;
}

.footer {
  align-self: end;
  background: lightsalmon;
}

@media (min-width: 52em) {
  .grid-layout {
    grid-template-columns:
      minmax(0, 1fr)
      minmax(0, 80em)
      minmax(0, 1fr);
    grid-template-rows: repeat(2, auto) 200px 1fr;
  }

  .grid-layout > * {
    grid-column: 1/-1;
  }

  .main {
    grid-column: 2;
  }
}
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="grid-layout.css">
  <title>Grid Layout</title>
</head>
<body>
  <div class="grid-layout">
    <header class="header">
      Header
    </header>

    <main class="main">
      <h2>Main</h2>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
      <p>Content</p>
    </main>

    <section class="testimonials">
      Testimonials
    </section>

    <footer class="footer">
      Footer
    </footer>
  </div>
</body>
</html>

Basically the layout works fine as it is, I just want to know how can I vary that main area to be different width on different pages. What approach would you use?

Any suggestion appreciated. Thank you!

Upvotes: 0

Views: 272

Answers (1)

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123397

A possibility is to define a CSS variable you may override in some specific pages, but using 80em as the default value:

HTML

<div class="grid-layout" style="--w: 70em">
</div>

CSS

@media (min-width: 52em) {
  .grid-layout {
    grid-template-columns:
      minmax(0, 1fr)
      minmax(0, var(--w, 80em))
      minmax(0, 1fr);
      grid-template-rows: repeat(2, auto) 200px 1fr;
  }
  ...
}

otherwise you could create a set of pre-defined classes if your variants are in a finite small number, e.g.

@media (min-width: 52em) {
  .grid-layout {
    --w : 80em
    grid-template-columns:
      minmax(0, 1fr)
      minmax(0, var(--w))
      minmax(0, 1fr);
      grid-template-rows: repeat(2, auto) 200px 1fr;
  }
  ...

  .grid-layout.variantA { --w: 70em }
  .grid-layout.variantB { --w: 90em }

}

Upvotes: 1

Related Questions