0

It is my first attempt us use responsive design using grid templates, following some example code I found. However my elements just use one grid cell, even if multiple contiguous cells use the same name.

Consider this primitive test (derived from the real code):

<html>
  <head>
    <title>Test</title>
    <style>
@media screen {
    .grid-1 {                           /* main grid */
        display: grid;
        grid-template-areas:
        'main main'
        'opt1 opt2'
        'opt3 opt3'
        'subm ...';
        gap: 10px;
    }

    .grid-1 * {
        /* adjust box sizing */
        margin: 0;
        box-sizing: border-box;
        background-color: #ccc;
    }
}

@media screen and (width < 60rem) {
    .grid-1 {
        grid-template-areas:
        'main main'
        'opt1 opt1'
        'opt2 opt2'
        'opt3 opt3'
        'subm ...';
    }
}
    </style>
  </head>
  <body>
    <div class="grid-1">
      <span grid-area="main">Main</span>
      <span grid-area="opt1">Options 1</span>
      <span grid-area="opt2">Options 2</span>
      <span grid-area="opt3">Options 3</span>
      <span grid-area="subm">Submit</span>
    </div>
  </body>
</html>

Debugging in Firefox the grid is shown like this (I added the background color in CSS for illustration after having made the screenshot):

Grid display in Firefox

(in the original code the <div> is a <form>, and the <span>s are `s)

Also note that adding more text into the elements does not change the assignment to grid cells.

Probably some beginner's mistake, but I don't get it.

2 Answers 2

1

The issue is that you're using grid-area as an HTML attribute on your elements, but that's not valid HTML and doesn't affect layout. grid-area should be set as a CSS property in your stylesheet, not as an HTML attribute.

Update your HTML to use classes instead, like this:

<div class="grid-1">
  <span class="main">Main</span>
  <span class="opt1">Options 1</span>
  <span class="opt2">Options 2</span>
  <span class="opt3">Options 3</span>
  <span class="subm">Submit</span>
</div>

Then, in your CSS, assign each class to a grid area:

.main { grid-area: main; }
.opt1 { grid-area: opt1; }
.opt2 { grid-area: opt2; }
.opt3 { grid-area: opt3; }
.subm { grid-area: subm; }

Now the elements will correctly span across the grid areas defined in your grid-template-areas.

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

1 Comment

It's a bit counter-intuitive that you cannot "style directly", but have to use indirect styling via CSS IMHO.
0

The grid-area is a CSS property, and not an attribute. You can add it to your CSS or inline it (like the example) via the "style" attribute:

@media screen {
  .grid-1 {
    /* main grid */
    display: grid;
    grid-template-areas:
      'main main'
      'opt1 opt2'
      'opt3 opt3'
      'subm ...';
    gap: 10px;
  }

  .grid-1 * {
    /* adjust box sizing */
    margin: 0;
    box-sizing: border-box;
    background-color: #ccc;
  }
}

@media screen and (width < 60rem) {
  .grid-1 {
    grid-template-areas:
      'main main'
      'opt1 opt1'
      'opt2 opt2'
      'opt3 opt3'
      'subm ...';
  }
}
<div class="grid-1">
  <span style="grid-area: main">Main</span>
  <span style="grid-area: opt1">Options 1</span>
  <span style="grid-area: opt2">Options 2</span>
  <span style="grid-area: opt3">Options 3</span>
  <span style="grid-area: subm">Submit</span>
</div>

Comments

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.