4

I really love the simplicity and ease of manipulation of grid-template-areas, but is there an easier way/shortcut to indicate an area spans multiple columns (say 12) — within the template?

This starts to get pretty gross when you have 12 columns...

.wrapper{
  display: grid;
  grid-template-areas:
    "nav nav cont cont cont cont cont cont cont cont cont cont";

  @media all and (max-width: 839px){
    grid-template-areas:
      "nav nav cont cont cont cont";
  }
}

2 Answers 2

5

Named areas are just shortcuts for named lines, and so you can use either. For example, your 12-column code can instead be written as:

.wrapper{
  display: grid;
  grid-template-columns: [nav-start] repeat(2, auto) [nav-end cont-start] repeat(10, auto) [cont-end];
}

The -start and -end line names define named areas in the same way that grid-template-areas does, and are sometimes more convenient to use.

(That said, I recommend usually doing what @temani-afif said, and instead just having two meaningful columns, using fr units to designate their relative sizes. Literal 12-column grids are an artifact of the old limitations that grid systems had to work in. But on the other hand, using actual multiple columns is sometimes clearer, especially if you're using gaps between the columns. Do whatever works best for you.)

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! this was very helpful too! Yea I'm starting to love css grid but it can get a bit messy once you start having nested grids. Easy to manage, though.
1

You can combine it with grid-template-columns like this:

.wrapper{
  display: grid;
  grid-template-areas:
    "nav cont";
  grid-template-columns:2fr 10fr;

  @media all and (max-width: 839px){
    grid-template-areas:
      "nav cont";
    grid-template-columns:1fr 4fr;
  }
}

Example:

.wrapper {
  display: grid;
  grid-template-areas: "nav cont";
  grid-template-columns: 2fr 10fr;
}

.nav {
  grid-area: nav;
  height: 100px;
  background: red;
}

.cont {
  grid-area: cont;
  height: 100px;
  background: blue;
}

@media all and (max-width: 839px) {
  .wrapper {
    grid-template-areas: "nav cont";
    grid-template-columns: 1fr 4fr;
  }
}
<div class="wrapper">
  <div class="nav">
  </div>
  <div class="cont">
  </div>
</div>

2 Comments

Ah ok. I guess I have to look at column designation differently, rather than rigid column designations like in the Foundation or bootstrap framework. I don't always know the amount of columns certain elements should/will span. Thanks!
it's be awesome if there was a template-area shortcut where you could append something like "nav nav cont---" where the --- indicates the cont should span to the end... or something similar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.