简体   繁体   English

Bootstrap 4:将内容垂直放置在自定义div内的容器内

[英]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. 请注意,我更改了背景的高度,以确保您可以看到结果。

如何将<kbd>内容</kbd>垂直居中<div></div><div id="text_translate"><p>我正在尝试制作钢琴,但我从基础开始。 我住在以<strong>div</strong>的内部内容为中心。 我希望它位于他父亲 div 的中间并居中</p><p>继承人代码:html</p><pre> &lt;div class="keys"&gt; &lt;div class="key" id="keyA"&gt; &lt;kbd&gt;A&lt;/kbd&gt; &lt;/div&gt; &lt;div class="key" id="keyS"&gt; &lt;kbd&gt;S&lt;/kbd&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> css:</p><pre> * { margin: 0; padding: 0; box-sizing: border-box; } body{ background-color: chocolate; }.keys{ padding: 4px 5px; position: fixed; height: 40%; max-height: 60%; width: 60%; max-width: 80%; display: flex; justify-content: space-around; top: 50%; left: 50%; transform: translate(-50%, -50%); border: white solid; align-items: center; text-align: center; }.key{ border: black solid 1px; font-family: 'Montserrat', sans-serif; font-size: 22px; max-height: 95%; height: 90%; width: 70px; max-width: 95%; border-radius: 10px; } kbd{ /*border to see the with and weight*/ border: pink solid 1px; color: aliceblue; }</pre><p> 这是一个小提琴: <a href="https://jsfiddle.net/hnd9jr4y/" rel="nofollow noreferrer">https://jsfiddle.net/hnd9jr4y/</a>谢谢</p></div> - How to center vertically <kbd> content inside an <div>

暂无
暂无

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

相关问题 将div垂直居中放置在容器中 - Center div vertically inside container Bootstrap-在容器流体内垂直和水平居中放置内容 - Bootstrap - Center content vertically and horizontally inside container-fluid Bootstrap轮播内垂直居中div - Vertically center div inside Bootstrap carousel 引导网格内垂直居中div - center vertically div inside bootstrap grid 如何将<kbd>内容</kbd>垂直居中<div></div><div id="text_translate"><p>我正在尝试制作钢琴,但我从基础开始。 我住在以<strong>div</strong>的内部内容为中心。 我希望它位于他父亲 div 的中间并居中</p><p>继承人代码:html</p><pre> &lt;div class="keys"&gt; &lt;div class="key" id="keyA"&gt; &lt;kbd&gt;A&lt;/kbd&gt; &lt;/div&gt; &lt;div class="key" id="keyS"&gt; &lt;kbd&gt;S&lt;/kbd&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> css:</p><pre> * { margin: 0; padding: 0; box-sizing: border-box; } body{ background-color: chocolate; }.keys{ padding: 4px 5px; position: fixed; height: 40%; max-height: 60%; width: 60%; max-width: 80%; display: flex; justify-content: space-around; top: 50%; left: 50%; transform: translate(-50%, -50%); border: white solid; align-items: center; text-align: center; }.key{ border: black solid 1px; font-family: 'Montserrat', sans-serif; font-size: 22px; max-height: 95%; height: 90%; width: 70px; max-width: 95%; border-radius: 10px; } kbd{ /*border to see the with and weight*/ border: pink solid 1px; color: aliceblue; }</pre><p> 这是一个小提琴: <a href="https://jsfiddle.net/hnd9jr4y/" rel="nofollow noreferrer">https://jsfiddle.net/hnd9jr4y/</a>谢谢</p></div> - How to center vertically <kbd> content inside an <div> 如何将一个bootstrap容器集中在div中? - How to center a bootstrap container inside a div? 我如何使用Bootstrap 3在div中垂直居中放置div - How can I vertically center a div inside a div with Bootstrap 3 在Div内将Div垂直居中 - Vertically center a Div inside a Div 将div垂直居中放置div - Center div inside a div vertically 在bootstrap的词缀div内居中对齐内容? - Center align content inside an affix div in bootstrap?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM