我有以下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)";
}
我尝试在div上使用position: relative;
,然后在position: absolute;
和h3
标记上使用a
,但这不起作用。
如果有人可以帮助我,我将不胜感激。我正在使用Bootstrap 3.
这是一个bootply演示http://www.bootply.com/sQ5gyYn7Ru
答案 0 :(得分:2)
一种方法是将面板包装在容器中,将背景颜色放在容器上,然后使用几行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%);
}
答案 1 :(得分:2)
我发现将容器div设置为display: table
很有用,并将内容包装在内部div设置为display: table-cell
。
然后您可以使用vertical-align
属性:
/* 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>
答案 2 :(得分:0)
使用具有“查看高度”的容器从上到下居中:
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%;
}