【问题标题】:CSS Named grid fragmented, same named areas not joinedCSS 命名网格碎片化,同名区域未连接
【发布时间】:2021-08-17 09:39:35
【问题描述】:

我已阅读 MDN 文档和 SO 答案。他们强调 CSS 网格中的命名区域必须是连续的和矩形的。我下面的网格遵循这些要求,但是名为 area 的图标没有填充 2 个单元格,只有 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;
    }
}

结果(您可以看到.OfficeIcon 仅跨越左上角的单元格,而不是与其下方的单元格合并:

想要的结果:

如何将命名区域固定为跨越 2 个单元格?

并将其放置在其单元格的底部中心,如下所示:

我猜我必须将 SVG 放在 div 中,然后将 SVG 放在 div 中。如果可能的话,我宁愿不用额外的div

【问题讨论】:

  • 尝试高度:100% 和宽度:100%
  • 我也会从 SVG width="32px" height="32px" 中删除它
  • height:100% and width:100% on .OfficeIcon 使它也填满了blank 区域,太奇怪了。
  • @Paulie_D 你是对的,它的 SVG 尺寸搞砸了。这是我第一次使用 CSS 命名网格,所以我认为还有别的东西在起作用,但是将它替换为正常的 diff 工作。它比 Chrome 在合并的单元格中画一个排水沟更奇怪,错误地使它看起来像它的两个单元格。
  • @TemaniAfif 感谢您发现这一点。我删除了一些伪选择器案例以明确问题,但错过了,现在更正。

标签: html css css-grid


【解决方案1】:

您使用 grid-template-columns: 64px 1fr; 创建网格列,因此尝试将 svg 大小调整为 64px,或者您可以在 .OfficeIcon 上使用 align-self: end;justify-self: center;

.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>

【讨论】:

  • 感谢您的回答,为什么我希望合并单元格的一个大问题是图标可以位于底部。抱歉,含糊不清,我更新了问题以使其更清楚。
  • 您的代码笔运行良好,谢谢。我正在尝试将其应用于我的代码以使其工作,但我遇到了一些麻烦,但我认为这将是公认的答案,只需对其进行更多测试,它应该可以工作。
猜你喜欢
  • 1970-01-01
  • 2020-05-18
  • 1970-01-01
  • 1970-01-01
  • 2021-01-06
  • 1970-01-01
  • 2014-08-16
  • 2017-08-29
相关资源
最近更新 更多