我在一个网站上工作,我希望我的大部分元素是居中对齐的.每个元素都被包装在section中,这样我就可以顺畅地从一个页面滚动到下一个页面.我有一个h1元素,它表示"Build Your Future",我试图将它完美地向左对齐,恰好在它表示"Purpose"的位置上方.然而,无论我做什么,我似乎都不能覆盖我的section,它表示这个特定元素的位置是align-items: center.我试过更改宽度和文本对齐方式,但它仍然卡在中间.

section {
  background-color: #fff;
  height: 100vh;
  height: fill-available;
  min-height: -moz-available;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow-x: hidden;
}

.box {
  background-color: white;
  display: flex;
  justify-content: space-between;
  width: 60vw;
}

.service h1 {
  display: flex;
  width: 100%;
  align-items: left;
  text-align: left;
}
<section class="service">
  <!-- First Row -->
  <div>
    <h1>Build Your Vision</h1>
  </div>
  <!-- Second Row -->
  <div class="box">
    <div>
      <h2>Purpose</h2>
      <p>Built with your vision</p>
    </div>
    <div>
      <h2>Authentication</h2>
      <p>From services you can trust</p>
    </div>
    <div>
      <h2>User Interface</h2>
      <p>Accessible from any device</p>
    </div>
  </div>
</section>

推荐答案

要解决这个问题,您应该使用h1元素的父容器(在本例中是div),而不是<section>元素.您遇到的问题是由于元素的限制造成的.

要解决这个问题,您可以通过指定类来应用一个CSS规则.在本例中,<section>元素具有类"服务",因此我们可以利用它.此外,我们希望只针对节中的第一个div,这可以使用:nth-child(1) Select 器来实现.

最后,我们将所选div的宽度设置为与Box类相同的长度.以下是更新后的代码:

.service > div:nth-child(1) {
    width: 60vw;
}

通过应用这条css规则,带有"service"类的Section元素内的第一个div的宽度将被设置为视区宽度的60%.

希望这个能帮上忙!如果你有任何进一步的问题,请告诉我.

section {
  background-color: #fff;
  height: 100vh;
  height: fill-available;
  min-height: -moz-available;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  overflow-x: hidden;
}

.box {
  background-color: white;
  display: flex;
  justify-content: space-between;
  width: 60vw;
}

.service h1 {
  display: flex;
  width: 100%;
  align-items: left;
  text-align: left;
}

.service > div:nth-child(1) {
    width: 60vw;
}
<section class="service">
  <!-- First Row -->
  <div>
    <h1>Build Your Vision</h1>
  </div>
  <!-- Second Row -->
  <div class="box">
    <div>
      <h2>Purpose</h2>
      <p>Built with your vision</p>
    </div>
    <div>
      <h2>Authentication</h2>
      <p>From services you can trust</p>
    </div>
    <div>
      <h2>User Interface</h2>
      <p>Accessible from any device</p>
    </div>
  </div>
</section>

Css相关问答推荐

Tailwind CSS group-hover不适用于自定义前置码

将CSS更改为Mat-Form-Field,其中包含搜索框

更改MAT-FORM-FIELD的CSS,使其在有Angular 的HTML中显示为普通文本框

这个 CSS :is() Select 器没有像我想象的那样工作

创建一个CSS动画的对角线线条.

如何简化CSS?

将框架放入 bootstrap 模式的正确方法是什么?

包含tailwind 中固定纵横比框的内容

如何在特定元素之前 Select 每个元素

如何在 nth-child 中正确传递 CSS 变量?

在整个 DIV 周围添加 CSS 框阴影

如何在渲染之前获取react 组件的大小(高度/宽度)?

如何在不使用浮动的情况下垂直对齐 div?

CSS图像最大宽度设置为原始图像大小?

位置固定宽度 100%

完成后如何防止css3动画重置?

内联块列表项中不需要的边距

为什么对 HTML 表单而不是表格使用定义列表(DL、DD、DT)标签?

如何创建一个带点的
标签?

CSS背景图像不透明度?