简体   繁体   English

使用HTML div中的引导程序将文本水平和垂直居中

[英]Center text horizontally and vertically using bootstrap in HTML div

Hi I'm trying to complete this page for freecodecamp and I'm stuck on a few details. 嗨,我正在尝试完成freecodecamp的本页,但我停留在一些细节上。

Here's my code 这是我的代码

I'm trying to get the "Martin Luther King Jr civil rights hero and icon" title centered vertically in the div. 我正在尝试使“马丁·路德·金·小民权英雄和图标”标题在div中垂直居中。 I'm also trying to center horizontally with the h3 tag "Timeline of Martin Luther King Jr.'s Life". 我还尝试使用h3标签“小马丁·路德·金的生平”的时间轴水平居中。

I've tried text-align:center; 我试过了text-align:center; and the text-center bootstrap class. 和文本中心引导程序类。 Neither seemed to work. 似乎都不起作用。 I've also tried adding !important to see if maybe the bootstrap class was overriding it somehow. 我也尝试添加!important以查看bootstrap类是否可能以某种方式覆盖了它。 Any ideas? 有任何想法吗?

HTML: HTML:

<h1 class="display-2 text-center">Martin Luther King Jr</h1>
    <h3 class="text-center text-muted font-italic">civil rights hero and icon</h3>

Make the div with class row display as a table and the two children as table-cell. 使具有类row的div显示为表格,将两个孩子显示为表格单元格。 Then you can apply a vertical align middle 然后您可以在中间应用垂直对齐

.row{
  display:table
}
.row .col,.row .col-5{
  display:table-cell;
}
.centered div:first-child{
  width: 100%;
  text-align:center;
}

click here for demo 点击此处进行演示

Since you're using flex box - you may want to leverage align-items and justify-content properties. 由于您使用的是弹性框-您可能需要利用align-itemsjustify-content属性。

Here's the slightly updated markup and css 这是稍微更新的标记和CSS

HTML 的HTML

<div class="col centered-content" style="border-style:solid;border-color:grey;border-radius:20px;background-color:white!important;background-color: white;">
    <div class="centered-content__inner">
        <h1 class="display-2 text-center" style="">Martin Luther King Jr</h1>
        <h3 class="text-center text-muted font-italic">civil rights hero and icon</h3>
    </div>
</div>

CSS: CSS:

.centered-content {
    display: flex;
    justify-content: center;
    align-items: center;
}
.centered-content__inner {
    background: none;
}

More on content centering with flex box here 此处更多有关使用flex box进行内容居中的内容

Hope this helps :) 希望这可以帮助 :)

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

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