CSS 网格中的重叠项
Overlapping items in CSS Grid
我正在尝试通过让两个元素彼此半重叠来使用 css 网格进行响应式布局。在宽屏幕上它们排成一排并水平重叠,但在窄屏幕上它们应该排成一列并垂直重叠。
下面是我要实现的目标的说明:
css 网格可以实现这种行为吗?这是我得到的结果,但现在我无法获得垂直重叠。
CSS
.wrapper {
background: white;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(444px, 1fr));
}
.wrapper__red, .wrapper__green {
align-self: center;
}
.wrapper__red {
z-index: 1;
background: red;
}
.wrapper__green {
justify-self: end;
height: 100%;
background: green;
}
HTML
<div class="wrapper">
<h1 class="wrapper__red">Title text goes here</h1>
<img class="wrapper__green" src="/a.jpg" />
</div>
在 CSS 网格中,您可以创建重叠的网格区域。
Grid 通过 line-based placement.
使这变得简单易行
来自规范:
The grid-row-start
, grid-column-start
, grid-row-end
, and
grid-column-end
properties determine a grid item’s size and location
within the grid by contributing a line, a span, or nothing (automatic)
to its grid placement, thereby specifying the inline-start,
block-start, inline-end, and block-end edges of its grid area.
注意:re-size 下面演示从桌面到移动视图的转换
article {
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-auto-rows: 50px;
grid-gap: 5px;
}
section:nth-child(1) { grid-column: 1 / 4; grid-row: 1; z-index: 1; }
section:nth-child(2) { grid-column: 3 / 5; grid-row: 1; }
section:nth-child(3) { grid-column: 5 / 7; grid-row: 1; }
@media ( max-width: 500px ) {
article { grid-template-columns: 100px; justify-content: center; }
section:nth-child(1) { grid-row: 1 / 4; grid-column: 1; }
section:nth-child(2) { grid-row: 3 / 5; grid-column: 1; }
section:nth-child(3) { grid-row: 5 / 7; grid-column: 1; }
}
/* non-essential demo styles */
section:nth-child(1) { background-color: lightgreen; }
section:nth-child(2) { background-color: orange; }
section:nth-child(3) { background-color: aqua; }
section {
display: flex;
justify-content: center;
align-items: center;
font-size: 1.2em;
}
<article>
<section><span>1</span></section>
<section><span>2</span></section>
<section><span>3</span></section>
</article>
jsFiddle demo
就我而言,我添加了:
section {
overflow: hidden;
}
到我的网格块,不管是否有任何包装,我的块都具有相同的高度。
<article>
<section><span>1</span></section>
<section><span>2</span></section>
<section><span>3</span></section>
</article>
我正在尝试通过让两个元素彼此半重叠来使用 css 网格进行响应式布局。在宽屏幕上它们排成一排并水平重叠,但在窄屏幕上它们应该排成一列并垂直重叠。
下面是我要实现的目标的说明:
css 网格可以实现这种行为吗?这是我得到的结果,但现在我无法获得垂直重叠。
CSS
.wrapper {
background: white;
display: grid;
grid-template-columns: repeat(auto-fit, minmax(444px, 1fr));
}
.wrapper__red, .wrapper__green {
align-self: center;
}
.wrapper__red {
z-index: 1;
background: red;
}
.wrapper__green {
justify-self: end;
height: 100%;
background: green;
}
HTML
<div class="wrapper">
<h1 class="wrapper__red">Title text goes here</h1>
<img class="wrapper__green" src="/a.jpg" />
</div>
在 CSS 网格中,您可以创建重叠的网格区域。
Grid 通过 line-based placement.
使这变得简单易行来自规范:
The
grid-row-start
,grid-column-start
,grid-row-end
, andgrid-column-end
properties determine a grid item’s size and location within the grid by contributing a line, a span, or nothing (automatic) to its grid placement, thereby specifying the inline-start, block-start, inline-end, and block-end edges of its grid area.
注意:re-size 下面演示从桌面到移动视图的转换
article {
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-auto-rows: 50px;
grid-gap: 5px;
}
section:nth-child(1) { grid-column: 1 / 4; grid-row: 1; z-index: 1; }
section:nth-child(2) { grid-column: 3 / 5; grid-row: 1; }
section:nth-child(3) { grid-column: 5 / 7; grid-row: 1; }
@media ( max-width: 500px ) {
article { grid-template-columns: 100px; justify-content: center; }
section:nth-child(1) { grid-row: 1 / 4; grid-column: 1; }
section:nth-child(2) { grid-row: 3 / 5; grid-column: 1; }
section:nth-child(3) { grid-row: 5 / 7; grid-column: 1; }
}
/* non-essential demo styles */
section:nth-child(1) { background-color: lightgreen; }
section:nth-child(2) { background-color: orange; }
section:nth-child(3) { background-color: aqua; }
section {
display: flex;
justify-content: center;
align-items: center;
font-size: 1.2em;
}
<article>
<section><span>1</span></section>
<section><span>2</span></section>
<section><span>3</span></section>
</article>
jsFiddle demo
就我而言,我添加了:
section {
overflow: hidden;
}
到我的网格块,不管是否有任何包装,我的块都具有相同的高度。
<article>
<section><span>1</span></section>
<section><span>2</span></section>
<section><span>3</span></section>
</article>