<div class="wrapper">
  <h1 class="main-title">Centering Content in CSS</h1>
  <section class="example">
    <h2 class="title">Table Centering</h2>
    <table class="table-centering">
      <tr class="table-centering__row">
        <td class="table-centering__content">
          :( why?
        </td>
      </tr>
    </table>
  </section>
  <section class="example">
    <h2 class="title">Tranform Centering</h2>
    <section class="transform-centering">
      <p class="transform-centering__content">
        :| slightly less why?
      </p>
    </section>
  </section>
  <section class="example">
    <h2 class="title">Flexbox Centering</h2>
    <section class="flexbox-centering">
      <p class="flexbox-centering__content">
        :) very much zero why?
      </p>
    </section>
  </section>
  <section class="example">
    <h2 class="title">Grid Centering</h2>
    <section class="grid-centering">
      <p class="grid-centering__content">
        :) very much zero why? #2
      </p>
    </section>
  </section>
</div>
// Common styles
@import url('https://fonts.googleapis.com/css2?family=Montserrat&display=swap');

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font: 10px/1 "Montserrat", sans-serif;
}

.example {
  margin-bottom: 4rem;
}

.main-title {
  font-size: 3rem;
  margin-bottom: 3rem;
  text-align: center;
}

.title {
  align-items: center;
  display: flex;
  font-size: 2.3rem;
  justify-content: center;
  padding-bottom: 1rem;
  white-space: no-wrap;
  
  &::after, &::before {
    background-color: #999;
    content: "";
    height: 0.2rem;
    margin: 0 1rem;
    width: 10%;
  }
}

.wrapper {
  padding: 1rem;
}

// Example specific
// Table styling

.table-centering {
  background-color: #aabbcc;
  min-height: 30rem;
  width: 100%;

  &__content {
    font-size: 3rem;
  }
  
  &__row {
    text-align: center;
    vertical-align: center;
  }
}

// Transform styling

.transform-centering {
  background-color: #aabbcc;
  min-height: 30rem;
  position: relative;
  width: 100%;

  &__content {
    background-color: #fedcba;
    border-radius: 0.5rem;
    font-size: 3rem;
    left: 50%;
    padding: 1rem;
    position: absolute;
    top: 50%;
    transform: translateX(-50%) translateY(-50%);
  }
}

// Flexbox styling

.flexbox-centering {
  align-items: center;
  background-color: #aabbcc;
  display: flex;
  justify-content: center;
  min-height: 30rem;
  width: 100%;

  &__content {
    background-color: #fedcba;
    border-radius: 0.5rem;
    font-size: 3rem;
    padding: 1rem;
  }
}

// Grid styling

.grid-centering {
  align-content: center;
  background-color: #aabbcc;
  display: grid;
  justify-items: center;
  min-height: 30rem;
  width: 100%;

  &__content {
    background-color: #fedcba;
    border-radius: 0.5rem;
    font-size: 3rem;
    padding: 1rem;
  }
}
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.