[英]Bootstrap 4: vertically center content inside a container inside a custom div
I tried to read other answers but there's none for my specific case, as far as I could find. 我尝试阅读其他答案,但就我所能找到的具体情况而言,没有答案。
I wanted to make a translucid background inside a bootstrap container to improve text readability. 我想在引导容器中制作半透明的背景,以提高文本的可读性。 What I was able to achieve was this: 我能够实现的是:
This doesn't look very good: I want the text and buttons to be vertically and horizontally aligned inside the my-background div. 这看起来不太好:我希望文本和按钮在my-background div中垂直和水平对齐。
From other questions I found here, I tried adding the classes align-items-center justify-content
to my inner div but the result wasn't much better: 从我在这里发现的其他问题中,我尝试将align-items-center justify-content
类添加到我的内部div中,但结果并没有好得多:
As we can see, the "Button" button still touches the bottom of the background and the content itself still isn't vertically aligned inside the background. 如我们所见,“按钮”按钮仍然触及背景的底部,内容本身仍未在背景内垂直对齐。
How do I fix this? 我该如何解决?
Here's my code: 这是我的代码:
<div class="my-background">
<div class="container">
<div class="row align-items-center">
<div class="col-lg-12">
<div id="content align-items-center justify-content">
<h1>Hello World</h1>
<h3>Welcome to my fancy Hello World page!</h3>
<hr>
<button class="btn btn-default btn-lg">Button</button>
</div>
</div>
</div>
</div>
</div>
CSS: CSS:
html {
height: 100%;
}
.my-background {
text-align: center;
height: 100%;
margin-top: 100px;
background-color: rgba(0, 0, 0, 0.4);
}
#content {
margin: auto;
text-align: center;
padding-top: 25%;
}
For something like this I'd recommend using flex. 对于这样的事情,我建议使用flex。
A complete guide can be found here . 完整的指南可以在这里找到。
However, in order to get everything center aligned you can do something like this, which is in fact using flex. 但是,为了使所有内容居中对齐,您可以执行以下操作,实际上是使用flex。
html { height: 100%; } .my-background { display: flex; align-items: center; justify-content: center; text-align: center; height: 700px; margin-top: 100px; background-color: rgba(0, 0, 0, 0.4); } hr { width: 100%; } #content { margin: auto; text-align: center; padding-top: 25%; }
<div class="my-background"> <div class="container"> <div class="row align-items-center"> <div class="col-lg-12"> <div id="content align-items-center justify-content"> <h1>Hello World</h1> <h3>Welcome to my fancy Hello World page!</h3> <hr> <button class="btn btn-default btn-lg">Button</button> </div> </div> </div> </div> </div>
Please note that I changed the height of the background to ensure that you can see the result. 请注意,我更改了背景的高度,以确保您可以看到结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.