【问题标题】:css grid element placement chrome vs. firefoxcss 网格元素放置 chrome 与 firefox
【发布时间】:2018-02-18 01:54:21
【问题描述】:

我不知道这是我的代码有问题,还是 chrome 的网格规范(或者可能是 firefox 的)实现有问题,但是如果我指定相对位置调整,我看到元素在它们的网格中放错了位置.

查看以下内容:

HTML

<div class="sheet-container">
  <div class="sheet-header-block sheet-one"></div>
  <div class="sheet-bottom-left sheet-corner sheet-one"></div>
  <div class="sheet-top-right sheet-corner sheet-one"></div>
  <div class="sheet-header-block sheet-two"></div>
  <div class="sheet-bottom-left sheet-corner sheet-two"></div>
  <div class="sheet-top-right sheet-corner sheet-two"></div>
  <div class="sheet-header-block sheet-three"></div>
  <div class="sheet-bottom-left sheet-corner sheet-three"></div>
  <div class="sheet-top-right sheet-corner sheet-three"></div>
</div>

CSS

.sheet-container{
    display:grid;
    grid-template-columns: 80px 80px 80px;
    grid-template-rows:40px;
    grid-gap:5px;
    grid-template-areas:"one two three";
    background-color:white;
    width:100%;
    height:100%;
}
.sheet-header-block{
  background-color:red;
}
.sheet-one{
  grid-area:one;
}
.sheet-two{
  grid-area:two;
}
.sheet-three{
  grid-area:three;
}
.sheet-corner{
    position:relative;
    transform:rotate(45deg);
    height:2px;
    width:5px;
    background-color:black;
}
.sheet-bottom-left{
    align-self:end;
    top:-2px;
}
.sheet-top-right{
    justify-self:end;
    left:-4px;
}

在 Firefox 中,此代码导致角落 div 位于标题块各自的角落,但分别向上重新定位 2px 和向左 4px。

在 chrome 中,任何位置调整都会否定 justify-self 或 align-self 属性并将它们放在标题块的左上角。有人对此有建议的解决方法吗?

编辑:我只需要支持最新的 chrome 和 firefox 版本。额外的兼容性当然是一个加分项。由于是开发环境,只能使用类。

EDT2:Fiddle 的问题。小提琴使用 Warren Wan 建议的解决方案,尽管它仍然没有解决 chrome 64.0.3282.167 (Official Build) (64-bit) 中的问题。

【问题讨论】:

  • 我不确定应该发生什么,但您不应该看到网格区域与元素相同。定位这些角的最简单(也是合乎逻辑的)方法是将它们放在 .sheet-header-block 元素中并从那里定位它们。
  • 是的,网格区域与 div 之类的元素不同,根据规范,在网格区域内(相对或绝对)定位网格元素应该相对于网格区域定位它(如如果该区域是一个相对定位的元素)。至于我为什么要这样做,实际上以这种方式定位它们(​​至少在可行时)比在 header-block 元素中进行定位更容易,因为需要完成的计算更少。

标签: html css css-grid


【解决方案1】:

你写错了css.sheet-top-right CSS 应该是 left: 0;top: 2px;。由于您对justify-self: end; 的理解,因此意味着内容将在右边缘自动显示。如果你设置了rotate: 45(deg),你需要设置top或者bottomposition

FireFox test result with FireFox version.

Chrome test resultChrome version 63.0.3239.132 (Official Build) (64-bit)

注意:您应该写css prefix 以支持旧的browser

.sheet-top-right{
    justify-self:end;
    left: 0;
    top: 2px;
}

.sheet-container{
    display:grid;
    grid-template-columns: 80px 80px 80px;
    grid-template-rows:40px;
    grid-gap:5px;
    grid-template-areas:"one two three";
    background-color:white;
    width:100%;
    height:100%;
}
.sheet-header-block{
  background-color:red;
}
.sheet-one{
  grid-area:one;
}
.sheet-two{
  grid-area:two;
}
.sheet-three{
  grid-area:three;
}
.sheet-corner{
    position:relative;
    transform:rotate(45deg);
    height:2px;
    width:5px;
    background-color:black;
}
.sheet-bottom-left{
    align-self:end;
    top:-2px;
}
.sheet-top-right{
    justify-self:end;
    left: 0;
    top: 2px;
}
<div class="sheet-container">
  <div class="sheet-header-block sheet-one"></div>
  <div class="sheet-bottom-left sheet-corner sheet-one"></div>
  <div class="sheet-top-right sheet-corner sheet-one"></div>
  <div class="sheet-header-block sheet-two"></div>
  <div class="sheet-bottom-left sheet-corner sheet-two"></div>
  <div class="sheet-top-right sheet-corner sheet-two"></div>
  <div class="sheet-header-block sheet-three"></div>
  <div class="sheet-bottom-left sheet-corner sheet-three"></div>
  <div class="sheet-top-right sheet-corner sheet-three"></div>
</div>

【讨论】:

  • 嗯,很有趣,所以这是最新的 chrome 版本中导致它的原因(64.0.3282.167(官方构建)(64 位))很高兴知道。但是,您的建议并不能解决问题。为了一个简单的问题示例,我只在每个方向上操作了一个方向,但是在实际项目中,角落 div 是相对垂直和水平定位的。
【解决方案2】:

由于 CSS 跨浏览器兼容性,这样做应该没问题:

.sheet-bottom-left{
    -webkit-align-self:end;
    -moz-align-self:end;
    align-self:end;
    top:-2px;
}

.sheet-top-right{
    -webkit-justify-self:end;
    -moz-justify-self:end;
    justify-self:end;
    left:-4px;
}

注意:您可能还需要将 -moz- 和 -webkit- 标志添加到“转换”。

【讨论】:

  • 这似乎也不能解决问题。我已经用一个显示问题的小提琴更新了 OP。
猜你喜欢
  • 2016-12-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多