在CSS-Grid中创建叠加层

时间:2018-03-27 14:39:41

标签: html css css3 css-grid

我正在尝试在CSS3网格中创建叠加层,但我似乎无法弄清楚如何实现它。我在网上搜索过,但没有找到任何帮助。我想实现以下目标:

enter image description here



body {
  margin: 0;
  padding: 0;
}

.wrapper {
		display: grid;
		grid-template-rows: 1f;
    width: 100vw;
    height: 100vh; 
    grid-template-areas:
      "a"
      "b"
      "c";
	}

.box {
  background-color: #444;
  color: #fff;
}

.a {
  grid-area: a;
}

.b {
  grid-area: b;
}

.c {
  grid-area: c;
}

<div class="wrapper">
  <div class="box a">A</div>
  <div class="box b">B</div>
  <div class="box c">C</div>
</div>
&#13;
&#13;
&#13;

以下是codepen的链接:https://codepen.io/anon/pen/PROVeY

编辑:

如何在CSS3网格布局中将div和左侧和右侧的div叠加在它后面的较大div上 enter image description here

 body {
      margin: 0;
      padding: 0;
    }

    .wrapper {
            display: grid;
            grid-template-rows: 1f;
        width: 100vw;
        height: 100vh; 
        grid-template-areas:
          "a"
          "b"
          "c";
        }

    .box {
      background-color: #444;
      color: #fff;
    }

    .a {
      grid-area: a;
    }

    .b {
      grid-area: b;
    }

    .c {
      grid-area: c;
    }
    .D {
      grid-area: D;
    }


    <div class="wrapper">
      <div class="box a">A</div>
      <div class="box b">B</div>
      <div class="box c">C</div>
      <div class="box D">C</div>
    </div>

2 个答案:

答案 0 :(得分:4)

像这样:

网格模板区域不是最有用的东西。最好独立定义列/行,然后单独为它们分配元素。然后根据需要对齐它们。

body {
    margin: 0;
    padding: 0;
}

.wrapper {
  margin:auto;
  width:90vw;
    display: grid;
    height: 100vh; 
  grid-template-columns:1fr; /* only one column */
  grid-template-rows:1fr auto; /* 2 rows */
    }
.a,
.b {
  grid-column:1; /* first-column */
  grid-row:1; /* first row */
}



.b {
  width:3em;
  height:3em;
  align-self: end; /* bottom of column */
  justify-self: end; /* right of row */
  margin:1em;
}

.box {
  border:1px solid green;
}
<div class="wrapper">
  <div class="box a">A</div>
  <div class="box b">B</div>
  <div class="box c">C</div>
</div>

答案 1 :(得分:1)

为您更新的问题更新Answer

尝试以下代码段:

body {
    margin: 0;
    padding: 0;
}

.wrapper {
  margin:auto;
  width:90vw;
    display: grid;
    height: 100vh; 
  grid-template-columns:1fr; /* only one column */
  grid-template-rows:1fr auto; /* 2 rows */
    }
.a,
.b,.c {
  grid-column:1; /* first-column */
  grid-row:1; /* first row */
}

.c {
  width:3em;
  height:3em;
  align-self: end; /* bottom of column */
  justify-self: end; /* right of row */
  margin:1em;
}

.b {
  width:9em;
  height:3em;
  align-self: end; /* bottom of column */
  justify-self: start; /* left of row */
  margin:0.5em;
 
}

.box {
  border:1px solid green;
}
<div class="wrapper">
  <div class="box a">A</div>
  <div class="box b">B</div>
  <div class="box c">C</div>
  <div class="box d">D</div>
</div>