search

Home  >  Q&A  >  body text

Can't center text using CSS + HTML

I'm trying to center text using CSS and HTML. I'm very new to web development so I'm just getting started. I watched the basics course (made the first page) and now I'm working on my own project (other pages)

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

html {
  /* font-size: 10px; */
  font-size: 62.5%
}

body {
  font-family: "Rubik", sans-serif;
  line-height: 1;
  font-weight: 400;
  color: #FFFFFF;
}

.second-page {
  background-color: #04041af9;
  padding: 4.8rem 0 9.6rem 0;
}

.our-news {
  max-width: 130rem;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9.6rem;
  align-items: center;
}

.heading-secondary {
  font-size: 5.2rem;
  font-weight: 700;
  /*line-height: 1.05;*/
  align-items: center;
  text-align: center;
  color: #FFFFFF;
  letter-spacing: -0.5px;
  margin-bottom: 3.2rem;
}
<section class="second-page">
  <div class="our-news">
    <h1 class="heading-secondary">
      Why buy through us?
    </h1>
  </div>
</section>

But, it won't be centered at all! I spent hours researching it so I finally came here to ask for help. I've attached an image of what it looks like:

All I want is for it to appear in the center) - at least horizontally! How should I achieve this (note that this part is part two)? Thanks.

P粉662802882P粉662802882233 days ago437

reply all(1)I'll reply

  • P粉287726308

    P粉2877263082024-04-01 10:21:39

    1. The padding in your section is uneven. You need to provide uniform values ​​such as padding: 5rem 0; so that the entire section is evenly spaced

    2. You used grid-template-columns: 1fr 1fr in .our-news, which tells that there will be 2 columns inside the container, taking up the same space. So you need to change this line to:

      grid-template column: 1fr;

    3. You provide a margin bottom for heading-secondary. Delete the line so there won't be any unwanted space below the text.

    Modified code:

    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    
    html {
      /* font-size: 10px; */
      font-size: 62.5%
    }
    
    body {
      font-family: "Rubik", sans-serif;
      line-height: 1;
      font-weight: 400;
      color: #FFFFFF;
    }
    
    .second-page {
      background-color: #04041af9;
      padding: 5rem 0;
    }
    
    .our-news {
      max-width: 130rem;
      margin: 0 auto;
      display: grid;
      grid-template-columns: 1fr;
      gap: 9.6rem;
      align-items: center;
    
    }
    
    .heading-secondary {
      font-size: 5.2rem;
      font-weight: 700;
      /*line-height: 1.05;*/
      align-items: center;
      text-align: center;
      color: #FFFFFF;
      letter-spacing: -0.5px;
    }

    Why buy through us?

    reply
    0
  • Cancelreply