Align grid items to the corners of the container

5.6k views Asked by At

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>
3

There are 3 answers

1
Michael Benjamin On BEST ANSWER

.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

4
Dalin Huang On

Two solutios:

(1) use position: absolute;

  • set your parent container to position: relative;

  • using position: absolute; to those 4 div boxes then control the position with left, top, right, bottom

example below:

.container {
  width: 500px;
  height: 300px;
  background: lightgreen;
  position: relative;
  padding: 50px;
}

.container div {
  background: aqua;
  height: 50px;
  width: 50px;
  border: 2px solid black;
  box-sizing: border-box;
  position: absolute;
}

.box1 {
  top: 0;
  left: 0;
}

.box2 {
  top: 0;
  right: 0;
}

.box3 {
  bottom: 0;
  left: 0;
}

.box4 {
  bottom: 0;
  right: 0;
}
<div class="container">
  <div class="box1">Box1</div>
  <div class="box2">Box2</div>
  <div class="box3">Box3</div>
  <div class="box4">Box4</div>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has
  survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing
  software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a
  galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets
  containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>

(2) use CSS grid

.container {
  width: 500px;
  height: 300px;
  background: lightgreen;
  display: grid;
  grid-template-columns: 40px auto 40px;
  grid-template-rows: 40px auto 40px;
  padding: 0;
}

.container div {
  box-sizing: border-box;
  border: 1px solid red;
}

.box1 {
  grid-column-start: 1;
  grid-row-start: 1;
}

.box2 {
  grid-column-start: 3;
  grid-row-start: 1;
}

.box3 {
  grid-column-start: 1;
  grid-row-start: 3;
}

.box4 {
  grid-column-start: 3;
  grid-row-start: 3;
}
<div class="container">
  <div class="box1">Box1</div>
  <div class="box2">Box2</div>
  <div class="box3">Box3</div>
  <div class="box4">Box4</div>
</div>

7
Asons On

One can also do this using Flexbox, using a pseudo to push the 3rd and 4th item to a new line.

.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-content: space-between;
  align-items: flex-start;
  height: 200px;
  background-color: lightgray;
  border: 1px solid black;
}

.container > div {
  background-color: yellow;
}

.container > div:nth-child(n+3) {
  order: 2;
  align-self: flex-end;
}

.container::before {
  content: '';
  order: 1;
  flex-basis: 100%;
}
<div class="container">
  <div class="box1">Box1</div>
  <div class="box2">Box2</div>
  <div class="box3">Box3</div>
  <div class="box4">Box4</div>
</div>


Update based on a comment.

For IE11 on Windows 7, it appears it has issues with the pseudo as a flex item, so the fix for that will be a spacer element.

.container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-content: space-between;
  align-items: flex-start;
  height: 200px;
  background-color: lightgray;
  border: 1px solid black;
}

.container > div {
  background-color: yellow;
}

.container > div:nth-child(n+3) {
  order: 2;
  align-self: flex-end;
}

.container .spacer {
  flex-basis: 100%;
}
<div class="container">
  <div class="box1">Box1</div>
  <div class="box2">Box2</div>
  <div class="spacer"></div>
  <div class="box3">Box3</div>
  <div class="box4">Box4</div>
</div>