Centering in CSS Grid

Viewed 597212

I'm trying to create a simple page with CSS Grid.

What I'm failing to do is center the text from the HTML to the respective grid cells.

I've tried placing content in separate divs both inside and outside of the left_bg and right_bg selectors and playing with some of the CSS properties to no avail.

How do I do this?

html,
body {
  margin: 0;
  padding: 0;
}

.container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 100vh;
  grid-gap: 0px 0px;
}

.left_bg {
  display: subgrid;
  background-color: #3498db;
  grid-column: 1 / 1;
  grid-row: 1 / 1;
  z-index: 0;
}

.right_bg {
  display: subgrid;
  background-color: #ecf0f1;
  grid-column: 2 / 2;
  grid_row: 1 / 1;
  z-index: 0;
}

.left_text {
  grid-column: 1 / 1;
  grid-row: 1 / 1;
  position: relative;
  z-index: 1;
  justify-self: center;
  font-family: Raleway;
  font-size: large;
}

.right_text {
  grid-column: 2 / 2;
  grid_row: 1 / 1;
  position: relative;
  z-index: 1;
  justify-self: center;
  font-family: Raleway;
  font-size: large;
}
<div class="container">
  <!--everything on the page-->

  <div class="left_bg">
    <!--left background color of the page-->
  </div>
</div>

<div class="right_bg">
  <!--right background color of the page-->
</div>

<div class="left_text">
  <!--left side text content-->
  <p>Review my stuff</p>

  <div class="right_text">
    <!--right side text content-->
    <p>Hire me!</p>
  </div>
</div>

9 Answers

The CSS place-items shorthand property sets the align-items and justify-items properties, respectively. If the second value is not set, the first value is also used for it.

.parent {
  display: grid;
  place-items: center;
}

We can use place-items: center; property in parent element to make child elements text in center.

.parent {
    display:grid;
    grid-template-columns: repeat(2,1fr);
    border: 3px solid yellow;
    grid-gap: 3px;
    place-items: center;
 }
 
 .parent > div {
    background-color: blue;
    text-align: center;
    color: white;
    font-weight: bold;
    font-size: 2rem;
 }
 
 
<div class="parent">
  <div class="child1">child1</div>
  <div class="child2">child2</div>
</div>

Make parent a grid and justify-content: center; and align-content: center;

<div class="parent">
  <div class="child">child</div>
</div>


.parent {
      height: 100px;
      width: 100px;
      border: 1px solid black;
      display: grid;
      justify-content: center;
      align-content: center;
}


.child {
      height: 20px;
      width: 20px;
      background-color: red;
}

This worked for me:

.d-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  border-top: 1px solid black;
  border-left: 1px solid black;
}

.d-grid div {
  height: 50px;
  display: flex;
  border-bottom: 1px solid black;
  border-right: 1px solid black;
  align-items: center;
  justify-content: center;
}
<div class="d-grid">
  <div>text 1</div>
  <div>text 2</div>
  <div>text 3</div>
  <div>text 4</div>
  <div>text 5</div>
  <div>text 6</div>
</div>

Related