我正在尝试使用 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>
但是,它根本不会居中!我花了几个小时研究它,所以我终于来这里寻求帮助。我附上了它的外观图像:
我想要的只是让它出现在中央)——至少水平方向! 我应该如何实现这一目标(请注意,该部分是第二部分)?谢谢。
您的
部分中的填充不均匀。您需要提供统一的值,例如padding: 5rem 0;以便整个部分的间距均匀您在
.our-news中使用了grid-template-columns: 1fr 1fr,它告诉容器内将有 2 列,占用相同的空间。所以你需要将此行更改为:grid-模板列:1fr;您为
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; }