Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align grid items to the corners of the container

I want to use CSS Grid to align the items to stick to all 4 corners of the container box.

How would I do this? Does it make sense to use CSS grid or is it better to use flex box?

I have the following HTML:

 <div class="container">
    <div class="box1">Box1</div>
    <div class="box2">Box2</div>
    <div class="box3">Box3</div>
    <div class="box4">Box4</div>
 </div>
like image 593
Benedikt Avatar asked Aug 28 '17 19:08

Benedikt


1 Answers

.container {
  display: grid;
  grid-template-columns: auto auto;  /* grid has two columns; content defines width */
  justify-content: space-between;    /* horizontal alignment of grid tracks */
  align-content: space-between;      /* vertical alignment of grid tracks */
  height: 300px;
  background-color: lightgray;
  border: 1px solid black;
}

.container > div {
  background-color: yellow;
}
<div class="container">
  <div class="box1">Box1</div>
  <div class="box2">Box2</div>
  <div class="box3">Box3</div>
  <div class="box4">Box4</div>
</div>

jsFiddle

like image 137
Michael Benjamin Avatar answered Nov 12 '22 09:11

Michael Benjamin