CSS Named grid fragmented, same named areas not joined

Viewed 30

I have read MDN docs, and SO answers. They stress that named areas in CSS grids must be continguous, and rectangular. My grid below follows these requirements, however the icon named area does not fill 2 cells, only 1: HTML:

<label for="js-icon" class="OfficeIconField">
  <input id="js-icon" class="OfficeFloatInput" value="" type="text" required="" placeholder="Placeholder text">
  <label for="js-icon" class="OfficeFloatLabel js-css-blank">Text placeholder</label>
  <span class="OfficeHelp">Some help text</span>
  <svg class="OfficeIcon" xmlns="http://www.w3.org/2000/svg" width="32px" height="32px" fill="black" version="1.1" viewBox="0 0 24 24" ><path d="M0 0h24v24H0z" fill="none"></path><path d="m12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v3h16v-3c0-2.66-5.33-4-8-4z"></path>
</svg>
</label>

CSS:

.OfficeIconField {
    display: grid;
    gap: 8px;
    align-items: end;
    grid-template-columns: 64px 1fr;
    grid-template-rows: auto;
    grid-template-areas: 
      "icon label"
      "icon input"
      "blank help";
    & .OfficeFloatLabel {
        grid-area: label;
    }
    & .OfficeFloatInput {
        grid-area: input;
    }
    & .OfficeHelp {
        grid-area: help;
    }
    & .OfficeIcon {
        grid-area: icon;
        background: red !important;
        justify-self: center;
    }
}

Result (you can see .OfficeIcon only spans the top left cell, instead of being merged with the cell below it too:

enter image description here

Desired result:

enter image description here

How to fix the named area to span 2 cells?

And position it at the bottom center of its cell like this: enter image description here

Im guessing I am going to have to place the SVG within a div, and then position the SVG inside the div. I would prefer to do it without the extra div if possible.

1 Answers

You make grid columns with grid-template-columns: 64px 1fr; , so try to just adjust svg size to 64px, or you can use align-self: end; and justify-self: center; on .OfficeIcon

.OfficeIconField {
    display: grid;
    gap: 8px;
    grid-template-columns: 64px 1fr;
    grid-template-rows: auto;
    grid-template-areas: 
      "icon label"
      "icon input"
      "blank help";
    border: 1px solid black;
    }
.OfficeFloatLabel {
        grid-area: label;
        border: 1px solid black;
    }
.OfficeFloatInput {
        grid-area: input;
        border: 1px solid black;
    }
.OfficeHelp {
        grid-area: help;
        border: 1px solid black;
    }
.OfficeIcon {
        grid-area: icon;
        background: red !important;
        border: 1px solid black;
        align-self: end;
        justify-self: center;
        /*align-self: stretch; /* desperate attempt */*/
    }
}
<label for="js-icon" class="OfficeIconField">
  <input id="js-icon" class="OfficeFloatInput" value="" type="text" required="" placeholder="Placeholder text">
  <label for="js-icon" class="OfficeFloatLabel js-css-blank">Text placeholder</label>
  <span class="OfficeHelp">Some help text</span>
  <svg class="OfficeIcon" xmlns="http://www.w3.org/2000/svg" width="32px" height="32px" fill="black" version="1.1" viewBox="0 0 24 24" >
    <path d="M0 0h24v24H0z" fill="none"></path>
    <path d="m12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v3h16v-3c0-2.66-5.33-4-8-4z"></path>
  </svg>
</label>

Related