html - 垂直居中到一个div

标签 html css twitter-bootstrap center

我遇到了一个问题。例如,我有一个像这样的 div :

<div class="container-fluid">
    <div class="row restaurant">
        <div class="col-md-6">
            <h2>
                Le restaurant
           </h2>
        </div>
        <div class="col-md-6">
            <p>
                Some text
            </p>
        </div>
    </div>

CSS 看起来像这样:

.restaurant{
    height: 68vh;
    background: url("../imgs/restaurant.jpg") no-repeat center center fixed;
    background-size: cover;
}

.restaurant h2{
    font-family: 'brownhill', serif;
    text-align: center;
    font-size: 100px;
    color:white;
    margin-bottom: 50px;
}

.restaurant p{
    font-family: 'minaxi', serif;
    text-align: justify;
    font-size: 18px;
    line-height: 23px;
    color:white;
    width:70%;
    margin:auto;
}

我遇到的问题是我想将 h2 和 p 部分垂直居中,并且它必须响应......现在我尝试使用如下解决方案:

margin-top:34%;
-webkit-transform: translateY(-50%);
-moz-transform: translateY(-50%);
-ms-transform: translateY(-50%);
-o-transform: translateY(-50%);
transform: translateY(-50%);

但在手机和平​​板电脑上它会产生一些问题..

有什么帮助吗?

提前致谢

最佳答案

你可以像那样使用 flexbox

html {
  margin:0;
}

.restaurant{
    height: 100vh;
    background: url("../imgs/restaurant.jpg") no-repeat center center fixed;
    background-size: cover;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    -webkit-box-align: center;
    -webkit-align-items: center;
    align-items: center;
    min-height: 24em;
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    justify-content: center
}

.restaurant h2{
    font-family: 'brownhill', serif;
    text-align: center;
    font-size: 100px;
    margin:0;
}

.restaurant p{
    font-family: 'minaxi', serif;
    text-align: justify;
    font-size: 18px;
    line-height: 23px;
    margin:auto;
}
<div class="container-fluid">
    <div class="row restaurant">
    <div class="headline">
        <div class="col-md-6">
            <h2>
                Le restaurant
           </h2>
        </div>
        <div class="col-md-6">
            <p>
                Some text
            </p>
        </div>
       </div>
    </div>

关于html - 垂直居中到一个div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43674148/

相关文章:

javascript - 做出选择后关闭移动菜单

html - 用CSS做一个相对定位的div方 block

javascript - 如何将下拉列表中检索到的值的 id 从数据库发送到 php 文件?

html - 如何在没有左右填充的情况下为 Bootstrap 列提供背景?

html - Bootstrap模态高度不变

css - 如何放置#foo :media(min-available-width:350px and max-available-width:750px) into SCSS

javascript - 多行标题下划线

css - 具有固定列宽的 Flexbox

javascript - jQuery照亮与bootstrap 2.2.2一起使用将无法一起工作: e. css(...)未定义照亮.js

css - 在 bootstrap/css 中使用哪个结构重新创建这个 Stackoverflow 单元