简体   繁体   English

如何在容器div引导程序4中对齐内容

[英]how can I align content in a container div bootstrap 4

Hi I have a nav into a container and I want to center all elements: 嗨,我将导航仪放入了一个容器,我想将所有元素居中:

<!-- Main container -->
<div class="container align-content-center">

  <!-- Navigation bar -->
  <nav class="navbar navbar-expand-lg navbar-light bg-light fixed-top mx-auto">

    <!-- Link to home page -->
    <a class="navbar-brand" href="#">HOME</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
      aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>

    <!-- Search bar -->
    <form class="form-inline my-2 my-lg-0">
      <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
    </form>

  </nav>

</div>

More specifically I want all nav items to be aligned at the center of the container component 更具体地说,我希望所有导航项都在容器组件的中心对齐

GJCode, GJCode,

Here is an example of using Bootstrap classes and some HTML wrappers to center align the menu: https://jsfiddle.net/learnwithclyde/sm2bz4kL/ 这是一个使用Bootstrap类和一些HTML包装器将菜单居中对齐的示例: https : //jsfiddle.net/learnwithclyde/sm2bz4kL/

<div class="container">
  <div class="col-sm-6 col-sm-offset-3">
    <!-- Menu elements like brand and navigation --> 
  </div>
</div>

The class container is for making the width a narrower than container-fluid . container用于使宽度比container-fluid窄。 col-sm-6 is setting the width for the elements in the div, ie, the width of this div is going to be 6 columns max. col-sm-6设置div中元素的宽度,即该div的宽度最大为6列。

And finally, the col-sm-offset-3 is center align the div. 最后, col-sm-offset-3是div居中对齐。 col-sm-offset-3 simply adds a 3-column space before the div and 3-column space after the div. col-sm-offset-3只是在div之前添加了3列空间,在div之后添加了3列空间。 This totals to 6 columns. 总计6列。

Therefore, the total number of columns become 12 (6 from col-sm-6 and 6 from col-sm-offset-3 ). 因此,列的总数变为12( col-sm-6 6, col-sm-offset-3 )。

Note : depending on the contents of your menu, you might need to adjust the values of col-sm-6 and col-sm-offset-3 but this should help you understand the concept itself. 注意 :根据菜单的内容,可能需要调整col-sm-6col-sm-offset-3但这应有助于您理解概念本身。

Grid system Use mobile-first flexbox grid to build layouts of all shapes and sizes thanks to a twelve column system, five default responsive tiers, Sass variables and mixins, and dozens of predefined classes. 网格系统借助十二列系统,五个默认响应层,Sass变量和混合以及数十个预定义类,使用移动优先的flexbox网格构建各种形状和尺寸的布局。

How it works Bootstrap's grid system uses a series of containers, rows, and columns to layout and align content. 工作原理Bootstrap的网格系统使用一系列容器,行和列来布局和对齐内容。 It's built with flexbox and is fully responsive. 它是使用flexbox构建的,具有完全的响应能力。 Below is an example and an in-depth look at how the grid comes together. 下面是一个示例,并深入研究了网格如何组合在一起。

New to or unfamiliar with flexbox? 刚开始还是不熟悉flexbox? Read this CSS Tricks flexbox guide for background, terminology, guidelines, and code snippets. 阅读本CSS Tricks flexbox指南,了解背景,术语,指南和代码段。

<div class="container">
  <div class="row">
    <div class="col-sm">
      One of three columns
    </div>
    <div class="col-sm">
      One of three columns
    </div>
    <div class="col-sm">
      One of three columns
    </div>
  </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM