How can I get a background-image to be spanned over a grid-layout

喜夏-厌秋 提交于 2019-12-11 12:08:57

问题


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>

回答1:


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).

Added a transparent layer to the grid item to better illustrate

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




回答2:


Make your Grid-Background transparent and set the background-image of the parent to the desired one.

LG RT



来源:https://stackoverflow.com/questions/55567319/how-can-i-get-a-background-image-to-be-spanned-over-a-grid-layout

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!