我有网格框,item2
在嗨的b
区域的末尾:
.wrapper > div {
background-color: gray;
padding: 20px;
}
.wrapper {
display: grid;
grid-template-columns: 1fr 2fr;
grid-gap: 10px;
grid-template-rows: 150px 150px;
grid-template-areas:
"a b"
"c d"
}
.item1 {
grid-area: a;
}
.item2 {
grid-area: b;
justify-self: end;
position: relative;
right: -20px;
}
.item3 {
grid-area: d;
justify-self: start;
}
<div class="wrapper">
<div class="item1">Item 1</div>
<div class="item2">Item 2</div>
<div class="item3">Item 3</div>
</div>
我需要从他的位置向右移动item2
,所以我使用relative
位置和right: -20px
。它可以在Chrome和Firefox中完美运行,但是在Safari版本11.1.2(13605.3.8)中,justify-self: end
似乎无法正常工作:我的意思是,item2
位于区域{{1}的左侧}。如果没有b
元素,则right: -20px
位于其区域的右侧。我该如何解决?
Codepen:https://codepen.io/mraimon/pen/ZwzxWe
谢谢!
答案 0 :(得分:3)
您可以改用负边距:
.wrapper > div {
background-color: gray;
padding: 20px;
}
.wrapper {
display: grid;
grid-template-columns: 1fr 2fr;
grid-gap: 10px;
grid-template-rows: 150px 150px;
grid-template-areas:
"a b"
"c d"
}
.item1 {
grid-area: a;
}
.item2 {
grid-area: b;
justify-self: end;
margin-right: -20px;
}
.item3 {
grid-area: d;
justify-self: start;
}
<div class="wrapper">
<div class="item1">Item 1</div>
<div class="item2">Item 2</div>
<div class="item3">Item 3</div>
</div>
或翻译:
.wrapper > div {
background-color: gray;
padding: 20px;
}
.wrapper {
display: grid;
grid-template-columns: 1fr 2fr;
grid-gap: 10px;
grid-template-rows: 150px 150px;
grid-template-areas:
"a b"
"c d"
}
.item1 {
grid-area: a;
}
.item2 {
grid-area: b;
justify-self: end;
transform:translateX(20px);
}
.item3 {
grid-area: d;
justify-self: start;
}
<div class="wrapper">
<div class="item1">Item 1</div>
<div class="item2">Item 2</div>
<div class="item3">Item 3</div>
</div>