繁体   English   中英

在div中垂直对齐两个元素

[英]vertical align two elements within a div

我有以下div:

<div class="transparent-panel">
    <h3>We asked some of our supports the following questions</h3>
    <a href="#" class="button btn btn-white-big video-button-two">WATCH VIDEO</a>
</div>

我希望文本和按钮在div中居中显示。 当前看起来像这样:

在此处输入图片说明

而且我没有运气到中心。 这是transparent-panel div的CSS:

.transparent-panel {
    padding: 40px 20px 40px 20px;
    width: 100%;
    height: 100%; 
    background: rgba(51, 153, 51, 0.7);
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#75FFFFFF, endColorstr=#75FFFFFF)";
}

我尝试使用position: relative; 在div上,然后position: absolute; h3a标签上,但这h3

如果有人可以帮助我,将不胜感激。 我正在使用Bootstrap 3。

这是一个bootply演示http://www.bootply.com/sQ5gyYn7Ru

一种方法是将面板包装在容器中,将背景色放在容器上,然后使用几行CSS将面板在容器内垂直居中:

HTML:

<div class="panel-container">
    <div class="transparent-panel">
        <h3>We asked some of our supports the following questions</h3>
        <a href="#" class="button btn btn-white-big video-button-two">WATCH VIDEO</a>
    </div>
</div>

CSS:

html,body {
  height:100%;
}
.panel-container {
    height:100%;
    background: rgba(51, 153, 51, 0.7);
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#75FFFFFF, endColorstr=#75FFFFFF)";
}
.transparent-panel {
    padding: 40px 20px 40px 20px;
    width: 100%;
    text-align:center;
    /* Code to vertically center below */
    position: relative;
    top: 50%;
    transform: translateY(-50%);
}

引导示例

我发现将容器div设置为display: table并将内容包装在设置为display: table-cell的内部div中很有用。

然后,您可以使用vertical-align属性:

更新了BootPly

 /* CSS used here will be applied after bootstrap.css */ .teachers-image { background-size: cover; height: 418px; color: #ffffff; } .transparent-panel { padding: 0 20px; display: table; width: 100%; height: 100%; background: rgba(51, 153, 51, 0.7); -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#75FFFFFF, endColorstr=#75FFFFFF)"; } .transparent-panel > div { display: table-cell; vertical-align: middle; } .btn-white-big { background-color: #ffffff; height: 50px; color: #339933; font-size: 18px; font-weight: 500; line-height: 30px; @include add-border(3px, white, all); @include border-radius(30px); &:hover, &:focus, &.focus { background-color: #339933 !important; color: white; } } 
 <div class="teachers-image"> <div class="transparent-panel"> <div> <h3>We asked some of our supports the following questions</h3> <a href="#" class="button btn btn-white-big video-button-two">WATCH VIDEO</a> </div> </div> </div> 

使用具有“查看高度”的容器从上到下居中:

height: 100vh;

视图高度将始终使用窗口显示高度。

小提琴

HTML:

<div class="container">
<div class="transparent-panel">
    <h3>We asked some of our supports the following questions</h3>
    <a href="#" class="button btn btn-white-big video-button-two">WATCH VIDEO</a>
</div>
</div>

CSS:

    .container {
    height: 100vh;
    background: rgba(51, 153, 51, 0.7);
    -ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#75FFFFFF, endColorstr=#75FFFFFF)";
}
.transparent-panel {
    width: 100%;
    text-align:center;
    position: relative;
    top: 50%;

}

暂无
暂无

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

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