如何将行内的 Flexbox 项目居中?

我试图将行内的列居中,但它们不会居中。我已经尝试多次更改代码,但似乎找不到错误。已经在“服务”和“第 3 节”课程中尝试过justify-content:centeralign-items:center但没有成功。我也无法将顶行的“我做什么”文本居中。

现在的样子:

https://i.stack.imgur.com/WGplQ.png

         <div class="what-i-do">

            <h3>WHAT I DO</h3>

          </div>

          <div class="row services">

            <div class="column">

              <div class="first-column">

                <h3 class="services-title">

                  Content <br />

                  Creation

                </h3>

                <div class="services-text">

                  Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

                  Aenean commodo ligula eget dolor. Aenean massa. Cum sociis

                  natoque penatibus et magnis dis parturient montes, nascetur

                  ridiculus mus. Donec quam felis, ultricies nec, pellentesque

                  eu.

                </div>

              </div>

            </div>

            <div class="column">

              <div class="second-column">

                <h3 class="services-title">

                  Strategy <br />

                  Session

                </h3>

                <div class="services-text">

                  Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

                  Aenean commodo ligula eget dolor. Aenean massa. Cum sociis

                  natoque penatibus et magnis dis parturient montes, nascetur

                  ridiculus mus. Donec quam felis, ultricies nec, pellentesque

                  eu.

                </div>

              </div>

            </div>



饮歌长啸
浏览 114回答 3
3回答

慕无忌1623718

在行服务{显示:flex;&nbsp;justify-content(也尝试项目):center;}很难说一切都去哪里了,但尝试一下。

米脂

我在任何地方都没有看到display: flex规则。从这里开始,然后删除列宽,因为它们违背了弹性布局的目的。.row {&nbsp; &nbsp; display: flex;}.column {&nbsp; &nbsp; padding: 0 20px;}.services {&nbsp; &nbsp; align-items: center;&nbsp; &nbsp; justify-content: center;}.services-title {&nbsp; &nbsp; font-family: "Ibarra Real Nova", serif;&nbsp; &nbsp; align-items: center;&nbsp; &nbsp; color: rgba(40, 44, 48, 1);&nbsp; &nbsp; font-weight: bold;&nbsp; &nbsp; text-align: center;&nbsp; &nbsp; font-size: 45px;}.services-text {&nbsp; &nbsp; font-family: "Lato", sans-serif;&nbsp; &nbsp; text-align: justify;&nbsp; &nbsp; line-height: 1.8;&nbsp; &nbsp; letter-spacing: 0em;&nbsp; &nbsp; font-size: 16px;&nbsp; &nbsp; font-weight: 300;}<div class="row services">&nbsp; &nbsp; <div class="column">&nbsp; &nbsp; &nbsp; &nbsp; <div class="first-column">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3 class="services-title">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Content <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="services-text">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Lorem ipsum dolor sit amet, consectetuer adipiscing elit.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Aenean commodo ligula eget dolor. Aenean massa. Cum sociis&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; natoque penatibus et magnis dis parturient montes, nascetur&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ridiculus mus. Donec quam felis, ultricies nec, pellentesque&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eu.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class="column">&nbsp; &nbsp; &nbsp; &nbsp; <div class="second-column">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3 class="services-title">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Strategy <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Session&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="services-text">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Lorem ipsum dolor sit amet, consectetuer adipiscing elit.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Aenean commodo ligula eget dolor. Aenean massa. Cum sociis&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; natoque penatibus et magnis dis parturient montes, nascetur&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ridiculus mus. Donec quam felis, ultricies nec, pellentesque&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eu.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class="column">&nbsp; &nbsp; &nbsp; &nbsp; <div class="third-column">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h3 class="services-title">Branding</h3>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="services-text">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Lorem ipsum dolor sit amet, consectetuer adipiscing elit.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Aenean commodo ligula eget dolor. Aenean massa. Cum sociis&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; natoque penatibus et magnis dis parturient montes, nascetur&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ridiculus mus. Donec quam felis, ultricies nec, pellentesque&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; eu.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div></div>&nbsp;&nbsp;&nbsp;&nbsp;

沧海一幻觉

和align-items:center都justify-items:center在 CSS 网格中工作。您的问题可能与其他 CSS 影响网格的高度/宽度或样式规则display:grid本身的位置有关:html, body{height: 100%}.grid{&nbsp; &nbsp; height: 60%;&nbsp; &nbsp; display: grid;&nbsp; &nbsp; grid-template-columns: 1fr 1fr;&nbsp; &nbsp; background-color: salmon;&nbsp; &nbsp; align-items: center;&nbsp; &nbsp; justify-items: center;}<div class="grid">&nbsp; &nbsp; <div>Lorem ipsum</div>&nbsp; &nbsp; <div>Lorem ipsum lorem ipsum</div></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5