简体   繁体   English

Bootstrap-在容器流体内垂直和水平居中放置内容

[英]Bootstrap - Center content vertically and horizontally inside container-fluid

Just like the title says, in Bootstrap 4, I can't figure out how to center content inside a column that's inside .container-fluid. 就像标题中所说的那样,在Bootstrap 4中,我无法弄清楚如何在.container-fluid内的列中居中显示内容。

Here's my code, can someone please let me know what I did wrong? 这是我的代码,有人可以让我知道我做错了吗?

 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet" /> <div class="container-fluid"> <div class="row"> <div class="col-md-4 my-auto mx-auto"> <div class="mx-auto"> <h1 class="mb-5">This is a title </h1> <p class="mb-5">Lorem ipsum</p> <a href="#">Click me</a> </div> </div> <div class="col-md-8"> <p>Something else that's not important but needs to be here </p> </div> </div> 

Why doesn't the .mx-auto work and what can I use instead of it? .mx-auto为什么不起作用,我可以代替它使用什么?

The col-* are flex items, so you have to also make them flex container in order to center their content like you want (vertically or/and horizontally) using margin:auto with elements ( my-auto / mx-auto ) or align-items-* / jusity-content-* with the container: col-*是flex项目,因此您还必须使它们成为flex容器,以便使用margin:auto结合元素( my-auto / mx-auto )或align-items-*来使其内容居中(垂直或/和水平) align-items-* / jusity-content-*与容器:

 <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet" /> <div class="container-fluid"> <div class="row"> <div class="col-md-4 d-flex flex-column align-items-center justify-content-center"> <h1 class="mb-5">This is a title </h1> <p class="mb-5">Lorem ipsum</p> <a href="#">Click me</a> </div> <div class="col-md-8 d-flex flex-column "> <p class="my-auto mx-auto">Something else that's not important but needs to be here </p> </div> </div> 

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

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