Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating overlay in CSS-Grid

Tags:

html

css

css-grid

I am trying to create an overlay in CSS3 grid but I can't seem to figure out how I can go about achieving it. I have searched online but haven't found anything of help. I want to achieve something like below:

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>

Here is a link to codepen: https://codepen.io/anon/pen/PROVeY

Edited:

How to place div on left and right side overlay against the larger div behind it in CSS3 grid-layout 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>
like image 669
Jackie Avatar asked Mar 27 '18 14:03

Jackie


People also ask

How do you overlay something in CSS?

In short, CSS overlay effects are achieved by using the following: background-image and background CSS properties to add image and linear-gradient overlay effect. position:absolute , top , bottom , right , left CSS properties to control the position of overlay image or text.


2 Answers

Like this:

Grid-template areas aren't the most useful things here. Better to define the column / rows independently and then assign the elements to them indvidiually. Then align them as required.

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>
like image 147
Paulie_D Avatar answered Sep 29 '22 15:09

Paulie_D


Update Answer for your updated question:

Try the below code snippet:

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>
like image 39
NullPointer Avatar answered Sep 29 '22 13:09

NullPointer