Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertically align content within CSS Grid [duplicate]

I have a CSS Grid below and I would like the content(letters for now) to be vertically aligned within the cells. What's the best way to accomplish this? vertical-align:middle on the cells doesn't seem to do anything, align-items:center; on the grid container worked, but then the heights were all different.

body {
  margin: 40px;
}

.wrapper {
		display: grid;
    grid-gap: 10px;
		grid-template-columns: [col] 100px [col] 100px [col] 100px;
		grid-template-rows: [row] auto [row] auto [row] ;
		background-color: #fff;
		color: #444;
	}

	.box {
		background-color:#444;
		color:#fff;
		padding:20px;
		font-size:150%;
	}

	.a {
		grid-column: col / span 2;
		grid-row: row 1 / 3;
	}
	.b {
		grid-column: col 3 / span 1;
		grid-row: row ;
	}
	.c {
		grid-column: col 3 / span 1;
		grid-row: row 2 ;
	}
	.d {
		grid-column: col / span 1;
		grid-row: row 3;
	}

	.e {
		grid-column: col 2 / span 1;
		grid-row: row 3;
	}

.f {
  grid-column: col 3 / span 1;
  grid-row: row 3;
}
<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 class="box e">E</div>
  <div class="box f">F</div>
</div>
like image 592
user13286 Avatar asked May 24 '18 19:05

user13286


People also ask

How can I align the content inside of the grid cells?

To center content horizontally in a grid item, you can use the text-align property. Note that for vertical centering, vertical-align: middle will not work. This is because the vertical-align property applies only to inline and table-cell containers.

How do you justify-content in CSS Grid?

justify-content You can use the following values: start , end , center , stretch , space-around , space-between , or space-evenly . The grid content is aligned horizontally to the far edge of the container element. The grid content is spaced evenly horizontally within the container element.


1 Answers

If you just want them vertically centered you can add display:flex; and align-items: center; to the box class:

body {
  margin: 40px;
}

.wrapper {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: [col] 100px [col] 100px [col] 100px;
  grid-template-rows: [row] auto [row] auto [row];
  background-color: #fff;
  color: #444;
}

.box {
  background-color: #444;
  color: #fff;
  padding: 20px;
  font-size: 150%;
  display: flex;
  align-items: center;
}

.a {
  grid-column: col / span 2;
  grid-row: row 1 / 3;
}

.b {
  grid-column: col 3 / span 1;
  grid-row: row;
}

.c {
  grid-column: col 3 / span 1;
  grid-row: row 2;
}

.d {
  grid-column: col / span 1;
  grid-row: row 3;
}

.e {
  grid-column: col 2 / span 1;
  grid-row: row 3;
}

.f {
  grid-column: col 3 / span 1;
  grid-row: row 3;
}
<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 class="box e">E</div>
  <div class="box f">F</div>
</div>
like image 118
Scath Avatar answered Sep 22 '22 02:09

Scath