简体   繁体   English

在div中垂直对齐两个元素

[英]vertical align two elements within a div

I have the following 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>

and I want the text and button to appear centred within the div. 我希望文本和按钮在div中居中显示。 Currently it appears like so: 当前看起来像这样:

在此处输入图片说明

and I am having no luck getting to centre. 而且我没有运气到中心。 Here is the css for the transparent-panel 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)";
}

I tried using position: relative; 我尝试使用position: relative; on the div and then position: absolute; 在div上,然后position: absolute; on the h3 and a tag but that didn't work. h3a标签上,但这h3

If anyone can help me out it would be much appreciated. 如果有人可以帮助我,将不胜感激。 I am using Bootstrap 3. 我正在使用Bootstrap 3。

Here is a bootply demo http://www.bootply.com/sQ5gyYn7Ru 这是一个bootply演示http://www.bootply.com/sQ5gyYn7Ru

One way to do it would be to wrap the panel in a container, put the background color on the container and then use a few lines of CSS to vertically center the panel within the container: 一种方法是将面板包装在容器中,将背景色放在容器上,然后使用几行CSS将面板在容器内垂直居中:

HTML: 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: 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%);
}

Bootply Example 引导示例

I find it useful to set the container div as display: table , and wrap the content in a inner div set as display: table-cell . 我发现将容器div设置为display: table并将内容包装在设置为display: table-cell的内部div中很有用。

Then you can use the vertical-align property: 然后,您可以使用vertical-align属性:

Updated BootPly 更新了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> 

Use a container with View Height for top to bottom centering: 使用具有“查看高度”的容器从上到下居中:

height: 100vh;

The View Height will always use the windows display height. 视图高度将始终使用窗口显示高度。

Fiddle 小提琴

HTML: 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: 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