首页  >  问答  >  正文

无法使用 CSS + HTML 使文本居中

我正在尝试使用 CSS 和 HTML 使文本居中。我对网络开发非常陌生,所以才刚刚开始。我观看了基础知识课程(制作了第一页),现在我正在开发自己的项目(其他页面)

* {
  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>

但是,它根本不会居中!我花了几个小时研究它,所以我终于来这里寻求帮助。我附上了它的外观图像:

我想要的只是让它出现在中央)——至少水平方向! 我应该如何实现这一目标(请注意,该部分是第二部分)?谢谢。

P粉662802882P粉662802882184 天前343

全部回复(1)我来回复

  • P粉287726308

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

    1. 您的 部分 中的填充不均匀。您需要提供统一的值,例如 padding: 5rem 0; 以便整个部分的间距均匀

    2. 您在 .our-news 中使用了 grid-template-columns: 1fr 1fr ,它告诉容器内将有 2 列,占用相同的空间。所以你需要将此行更改为:

      grid-模板列:1fr;

    3. 您为 heading-secondary 提供了边距底部。删除该行,以便文本下方不会有任何不需要的空格。

    修改后的代码:

    * {
      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?

    回复
    0
  • 取消回复