【发布时间】: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 元素中进行定位更容易,因为需要完成的计算更少。