Ryan Coupland
Ryan Coupland

Reputation: 21

Struggling to vertically center in CSS grid

I'm trying to vertically center h1 in the header class but I can't figure out how to do it while keeping the background red. I've used margin: auto but it gets rid of the background. I've looked through so many suggestions but I just can't seem to get what I want. I'll leave the code here if anyone can help, thanks.

* {
  margin: 0;
  padding: 0;
}

.wrapper{
  height: 100vh;
  display: grid;
  grid-template-columns: 1fr 5fr 1fr;
  grid-template-rows:  1fr 7fr;
  text-align: center;
}

.header{
  background-color: red;
  grid-row: 1 / 2;
  grid-column: 1 / 2;
}

.header h1

.nav{
  background-color: blue;
  grid-row: 2 / 3;
  grid-column: 1 / 2;
}

.main{
  background-color: green;
  grid-row: 1 / span 2;
  grid-column: 2 / 3;
}

.sidebar{
  background-color: yellow;
  grid-row: 1 / span 2;
  grid-column: 3 / 4;
}
<!DOCTYPE html>
<html lang="en" dir="ltr"`
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="css.css">
  <title>Learning stuff</title>
</head>
<body>
  <div class="wrapper">
    <header class="header">
      <h1>center me</h1>
    </header>
    <nav class="nav">navigation
    </nav>
    <main class="main">
       main content
    </main>
    <section class="sidebar">
        sidebar
    </section>
  </div>
</body>

Upvotes: 2

Views: 47

Answers (2)

ROOT
ROOT

Reputation: 11622

If you mean the to center the <h1>, then you can add this style to the <header>:

.header {
  ...
  display: flex;
  justify-content: center;
  align-items: center;
}

* {
  margin: 0;
  padding: 0;
}

.wrapper{
  height: 100vh;
  display: grid;
  grid-template-columns: 1fr 5fr 1fr;
  grid-template-rows:  1fr 7fr;
  text-align: center;
}

.header{
  background-color: red;
  grid-row: 1 / 2;
  grid-column: 1 / 2;
}

.header h1 {
  display: flex;
  justify-content: center;
  align-items: center;
}

.nav{
  background-color: blue;
  grid-row: 2 / 3;
  grid-column: 1 / 2;
}

.main{
  background-color: green;
  grid-row: 1 / span 2;
  grid-column: 2 / 3;
}

.sidebar{
  background-color: yellow;
  grid-row: 1 / span 2;
  grid-column: 3 / 4;
}
<!DOCTYPE html>
<html lang="en" dir="ltr"`
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="css.css">
  <title>Learning stuff</title>
</head>
<body>
  <div class="wrapper">
    <header class="header">
      <h1>center me</h1>
    </header>
    <nav class="nav">navigation
    </nav>
    <main class="main">
       main content
    </main>
    <section class="sidebar">
        sidebar
    </section>
  </div>
</body>

Upvotes: 1

Hussein Akar
Hussein Akar

Reputation: 425

You could always use the container to align it's child to center.

* {
  margin: 0;
  padding: 0;
}

.wrapper{
  height: 100vh;
  display: grid;
  grid-template-columns: 1fr 5fr 1fr;
  grid-template-rows:  1fr 7fr;
  text-align: center;
}

.header{
  background-color: red;
  grid-row: 1 / 2;
  grid-column: 1 / 2;
  height: 150px; /* Remove the height used it just for demo  */
  display: flex;
  align-items: center;
}

.nav{
  background-color: blue;
  grid-row: 2 / 3;
  grid-column: 1 / 2;
}

.main{
  background-color: green;
  grid-row: 1 / span 2;
  grid-column: 2 / 3;
}

.sidebar{
  background-color: yellow;
  grid-row: 1 / span 2;
  grid-column: 3 / 4;
}
<!DOCTYPE html>
<html lang="en" dir="ltr"> <!-- You had extra ` here and missing the closing > tag -->

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="css.css">
  <title>Learning stuff</title>
</head>

<body>
  <div class="wrapper">
    <header class="header">
      <h1>center me</h1>
    </header>
    <nav class="nav">navigation
    </nav>
    <main class="main">
      main content
    </main>
    <section class="sidebar">
      sidebar
    </section>
  </div>
</body>

</html> <!-- You had this tag missing -->

Upvotes: 0

Related Questions