【问题标题】:List is not centered with Flexbox on smaller screen sizes在较小的屏幕尺寸上,列表不以 Flexbox 为中心
【发布时间】:2018-02-12 00:03:49
【问题描述】:

在 600 像素时,我的 flexbox 变成了一列。第二个框中的列表在较小的屏幕尺寸上未正确居中。我尝试在媒体查询内外添加边距和填充。它坐得太靠右了。当我调整宽度时,它似乎卡在左侧。我该如何解决这个问题?

/* FLEXBOX */

.flexbox {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  justify-content: center;
}

.box {
  padding: 5px;
}


/* Table and Cell for Vertical Alignment */

.table {
  display: table;
}

.cell {
  display: table-cell;
  vertical-align: middle;
}


/* SHOWCASE  */

#showcase {
  background-color: #1d2120;
  height: 100vh;
  width: 100%;
  display: table;
}

#showcase h1 {
  padding-top: 0px;
  font-family: Ubuntu;
  font-size: 4em;
  color: #da5d61;
}

#showcase h2 {
  color: #bcd5d1;
  font -family: Cairo;
  font-size: 2em;
  margin: 0 0 20px;
}

#showcase h3 {
  color: #ba9077;
  font-family: Cairo;
  font-size: 1em;
}

#inline {
  font-size: 3em;
}

#inline li {
  font-size: 1.5em;
  padding: 0 10px 0 10px;
}

@media screen and (max-width: 600px) {
  .flexbox {
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    text-align: center;
  }
  #contact .container {
    width: 60%;
  }
}

@media screen and (max-width: 610px) {
  .flexbox {
    -webkit-box-orient: vertical;
    -webkit-box-direction: normal;
    -ms-flex-direction: column;
    flex-direction: column;
    text-align: center;
  }
  #contact .container {
    width: 60%;
  }
}
<!-- SHOWCASE -->
<header id="showcase">
  <div class="cell">
    <div class="flexbox">

      <div class="box text-center">
        <h1 class="text animated pulse">Ellis Smith</h1>
        <h2><i class="fa fa-pencil" aria-hidden="true"></i> Copywriter</h2>
        <h3>Quality copy, never copied.</h3>
      </div>
      <!-- /box -->

      <nav class="box">
        <ul>
          <li>
            <a href="#services">Services</a></li>
          <li>
            <a href="#testimonials-placeholder">Testimonials</a>
            <li>
              <a href="#contact">Contact</a></li>
        </ul>
      </nav>
      <!-- /box -->
    </div>
    <!-- /flexbox -->
  </div>
  <!-- /cell -->
</header>
<!-- /showcase -->

<!--/END SHOWCASE -->

【问题讨论】:

  • 嗯...如果您已经在使用 Bootstrap,为什么不想为此使用 Bootstrap? Bootstrap 4 中的所有内容都是基于 flexbox 的,在使用 Bootstrap 时添加自定义 css hack 通常不是一个好主意,因为它往往需要更多 css hack 来解决由原始 css hack 引起的问题。
  • 好点。所以我可以移除 flexbox 并在表格的单元格内放置一个引导网格?
  • 是的,您完全可以单独使用 Bootstrap 完成所有操作,不,您不应该将其放入表格单元格中。表格单元格的应用案例非常少见。只是一个普通的行列对。我会在一分钟内发布一些东西。

标签: html css flexbox bootstrap-4 twitter-bootstrap-4


【解决方案1】:

要在 Bootstrap 4 中处理此问题,您只需创建一个行列对并将您的内容放入列中。

如果您只有一个想要居中的列表,那么我建议对该列使用 col-auto 类(它会自动更改列的宽度以适应内容),然后添加类 mx-auto (边距水平自动)到那个。这将使列居中。

这是代码 sn-p (注意:代码 sn-p 假定您将整个东西放入 Bootstrap .container.container-fluid):

<div class="row">
    <div class="col-auto mx-auto">
        <nav class="box">
            <ul>
                <li>
                    <a href="#services">Services</a></li>
                <li>
                    <a href="#testimonials-placeholder">Testimonials</a>
                <li>
                    <a href="#contact">Contact</a></li>
            </ul>
        </nav><!-- /box -->
    </div>
</div>

【讨论】:

  • 我的弹性盒子里面有两个盒子。您将如何合并第一个框:

    Ellis Smith

    Copywriter

    优质文案,绝不抄袭。

  • 如何在没有表格和单元格的情况下在我的页面上居中。你说我也可以删除它对吧?
  • 是的,删除表格单元格。至于另一个盒子:可以以完全相同的方式进行居中。问题是:您希望这些框在所有屏幕尺寸上都以这种方式显示吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-22
  • 2023-02-16
  • 2016-06-18
  • 1970-01-01
  • 2022-12-15
  • 2018-12-01
  • 1970-01-01
相关资源
最近更新 更多