Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to offset a div in a css grid

Tags:

html

css

css-grid

It's possible to "offset" a div in a css grid as in the image ?image

like image 608
Gianmarco Avatar asked Jul 11 '18 11:07

Gianmarco


People also ask

How do you put a div in the grid?

To center the <div> element horizontally using grid. Use the property of display set to grid i.e. display: grid; Then use property place-items: center; Example: The following example demonstrates to set the <div> to the center using grid property.

What is offset in CSS Grid?

Primer CSS Grid Offset Columns are used to push the div to X columns using the offset classes.

How do you align content in grid?

One of the easiest ways of centering the content of grid items is using Flexbox. Set the display to "grid" for the grid container, and "flex" for grid items. Then, add the align-items and justify-content properties, both with the "center" value, to grid items.

How do you split grid in CSS?

If you have a grid element that should span two columns, you can do grid-column: span 2 , or grid-row: span 2 for rows.


2 Answers

Consider negative margin:

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  width: 500px;
  height: 200px;
  border: 1px solid;
}

.box {
  grid-column: 2/3;
  margin-left: -20px;
  margin-right: -80px;
  background: red;
}
.box-alt {
  grid-column: 2/3;
  grid-row:2;
  background: blue;
}
<div class="container">
  <div class="box"></div>
  <div class="box-alt"></div>
</div>
like image 131
Temani Afif Avatar answered Sep 28 '22 08:09

Temani Afif


<div class="container">
    <div class="area1"></div>
    <div class="area2"></div>
</div>
.container{
    display: grid;
    grid-template: 1fr / repeat(3, 1fr);
    grid-template-areas: "area1 offset area2";
}
.area{
    width: 100%;
    height: 40px;
}
.area1{
    background-color: red;
}
.area2{
    background-color: yellow;
}
like image 24
Cesar Abel Ramirez Avatar answered Sep 28 '22 07:09

Cesar Abel Ramirez