问题描述
我的网格布局为2行,第一行为2列,第二行为3列。它们之间的网格间距为10px。
为每个单独的网格提供背景图像是没有问题的。但是,如果我希望它们都具有相同的背景图像,该背景图像从左上方的网格开始,一直延伸到右下方的网格。所有网格上都有一张大背景图像,只是由白色网格间隙隔开。
I have a grid layout of, let's say 2 rows, 2 columns in the 1st row, 3 columns in the 2nd row. And a grid gap of 10px between them.It is no problem to give every single grid a background-image. But what if I want them all to have the same background image that starts at the top left grid and continues/is spanned until the bottom right grid. One big background-image over all grids, just separated by the white grid gaps.
html,
body {
height: 100%;
}
.grid {
display: grid;
height: 100%;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: repeat(2, 1fr);
grid-gap: 10px;
}
.grid_cell_one {
grid-column: 1 / span 3;
grid-row: 1 / span 1;
background-image: url("https://cdn.pixabay.com/photo/2017/01/08/21/11/wood-1963988__340.jpg");
}
.grid_cell_two {
grid-column: 4 / span 3;
grid-row: 1 / span 1;
background-image: url("https://cdn.pixabay.com/photo/2017/01/08/21/11/wood-1963988__340.jpg");
}
.grid_cell_three {
grid-column: 1 / span 2;
grid-row: 2 / span 1;
background-image: url("https://cdn.pixabay.com/photo/2017/01/08/21/11/wood-1963988__340.jpg");
}
.grid_cell_four {
grid-column: 3 / span 2;
grid-row: 2 / span 1;
background-image: url("https://cdn.pixabay.com/photo/2017/01/08/21/11/wood-1963988__340.jpg");
}
.grid_cell_five {
grid-column: 5 / span 2;
grid-row: 2 / span 1;
background-image: url("https://cdn.pixabay.com/photo/2017/01/08/21/11/wood-1963988__340.jpg");
}
<div class="grid">
<div class="grid_cell_one">
</div>
<div class="grid_cell_two">
</div>
<div class="grid_cell_three">
</div>
<div class="grid_cell_four">
</div>
<div class="grid_cell_five">
</div>
</div>
推荐答案
想法是考虑 background-attachement:fixed
,但是背景将不再跟随滚动:
On idea is to consider background-attachement:fixed
but the background will no more follow the scroll:
html,
body {
height: 100%;
margin:0;
}
.grid {
display: grid;
height: 100%;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: repeat(2, 1fr);
grid-gap: 10px;
}
.grid>* {
background-image: url("https://cdn.pixabay.com/photo/2017/01/08/21/11/wood-1963988__340.jpg");
background-size: cover;
background-repeat: no-repeat;
background-attachment: fixed;
}
.grid_cell_one {
grid-column: 1 / span 3;
grid-row: 1 / span 1;
}
.grid_cell_two {
grid-column: 4 / span 3;
grid-row: 1 / span 1;
}
.grid_cell_three {
grid-column: 1 / span 2;
grid-row: 2 / span 1;
}
.grid_cell_four {
grid-column: 3 / span 2;
grid-row: 2 / span 1;
}
.grid_cell_five {
grid-column: 5 / span 2;
grid-row: 2 / span 1;
}
<div class="grid">
<div class="grid_cell_one">
</div>
<div class="grid_cell_two">
</div>
<div class="grid_cell_three">
</div>
<div class="grid_cell_four">
</div>
<div class="grid_cell_five">
</div>
</div>
另一个想法是考虑在网格容器上有多个背景,您将在其中用白色(或背景中使用的任何颜色)填充空白。
Another idea is to consider multiple background on the grid container where you will fill the gap with white color (or any color used in the background).
为网格项目以更好地说明
html,
body {
height: 100%;
margin:0;
}
.grid {
display: grid;
height: 100%;
grid-template-columns: repeat(6, 1fr);
grid-template-rows: repeat(2, 1fr);
grid-gap: 10px;
background:
/*middle horizontal line*/
linear-gradient(#fff,#fff) center/100% 10px,
/*top vertical line*/
linear-gradient(#fff,#fff) top center/10px 50%,
/*bottom lines*/
linear-gradient(#fff,#fff) calc(1*100%/3 - 3px) 100%/10px 50%,
linear-gradient(#fff,#fff) calc(2*100%/3 + 3px) 100%/10px 50%,
/*main background*/
url("https://cdn.pixabay.com/photo/2017/01/08/21/11/wood-1963988__340.jpg") center/cover;
background-repeat:no-repeat;
}
.grid>* {
background: rgba(255, 255, 0, 0.2);
/*to illustrate*/
}
.grid_cell_one {
grid-column: 1 / span 3;
grid-row: 1 / span 1;
}
.grid_cell_two {
grid-column: 4 / span 3;
grid-row: 1 / span 1;
}
.grid_cell_three {
grid-column: 1 / span 2;
grid-row: 2 / span 1;
}
.grid_cell_four {
grid-column: 3 / span 2;
grid-row: 2 / span 1;
}
.grid_cell_five {
grid-column: 5 / span 2;
grid-row: 2 / span 1;
}
<div class="grid">
<div class="grid_cell_one">
</div>
<div class="grid_cell_two">
</div>
<div class="grid_cell_three">
</div>
<div class="grid_cell_four">
</div>
<div class="grid_cell_five">
</div>
</div>
您可以检查此链接可获取有关所使用的不同值的更多详细信息:
You can check this link for more details about the different value used: Using percentage values with background-position on a linear gradient
这篇关于如何获得背景图像以覆盖网格布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!