我正在尝试使用 Bootstrap 4 将我的 Container 放在页面中间。到目前为止,我一直没有成功。任何帮助,将不胜感激。
我已经在 Codepen.io 构建了它,所以你们可以玩它,让我知道什么是有效的,因为我没有想法……
var currentAuthor = "";
var currentQuote = "";
function randomQuote() {
$.ajax({
url: "https://api.forismatic.com/api/1.0/?",
dataType: "jsonp",
data: "method=getQuote&format=jsonp&lang=en&jsonp=?",
success: function( response ) {
$("#quote-content").html('<h2 id="quote-content" class="display-5"><i class="fa fa-quote-left" aria-hidden="true"> ' + response.quoteText + ' <i class="fa fa-quote-right" aria-hidden="true"></i></h2>');
$("#quote-author").html('<p id="quote-author" class="lead"><em>' + response.quoteAuthor + '</em></p>');
currentAuthor = response.quoteAuthor;
currentQuote = response.quoteText
}
});
}
function openURL(url){
window.open(url,'Share', 'width=550, height=400, toolbar=0, scrollbars=1 ,location=0 ,statusbar=0,menubar=0, resizable=0');
}
function tweetQuote(){
openURL('https://twitter.com/intent/tweet?hashtags=quotes,freecodecamp&related=freecodecamp&text=' + encodeURIComponent('"' + currentQuote + '" - ' + currentAuthor));
}
$(document).ready(function () {
randomQuote();
$("#get-another-quote-button").click(function(){
randomQuote();
});
$('#tweet').on('click', function() {
tweetQuote();
});
});
html, body {
background-image: url("https://www.mylinea.com/wp-content/uploads/beautiful-trees-stock-photo-055.jpg");
background-color: #17234E;
margin-bottom: 0;
min-height: 30%;
background-repeat: no-repeat;
background-position: center;
-webkit-background-size: cover;
background-size: cover;
}
.btn-new-quote {
color: #0C0C0D;
background-color: transparent;
border-color: #414147;
}
.btn-new-quote:hover {
color: #0C0C0D;
background-color: #9A989E;
border-color: #0C0C0D;
}
#tweet {
color: RGB(100, 100, 100);
}
#tweet:hover {
color: RGB(50, 50, 50);
}
.jumbotron {
position: relative;
top: 50%;
transform: translateY(-50%);
opacity: .85;
border-color: RGB(50, 50, 50);
padding-bottom: 8px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css">
<div class="container">
<div class="row justify-content-center align-self-center">
<div class="col-sm-6">
<div class="jumbotron vertical-center text-center">
<h2 id="quote-content" class="display-5"><i class="fa fa-quote-left" aria-hidden="true"></i><i class="fa fa-quote-right" aria-hidden="true"></i></h2>
<p id="quote-author" class="lead"><em></em></p>
<hr class="my-2">
<div class="row align-items-center justify-content-between">
<div class="col-sm-1-4 text-left">
<a id="tweet" href="#">
<h2 class="display-4"><i class="fa fa-twitter" aria-hidden="true"></i></h2>
</a>
</div>
<div class="col-sm-1-4 text-right">
<button id="get-another-quote-button" type="button" class="btn btn-outline-secondary btn-new-quote">Don't Quote Me on This...</button>
</div>
</div>
</div>
</div>
</div>
</div>
原文由 canaan seaton 发布,翻译遵循 CC BY-SA 4.0 许可协议
重要的! 垂直中心是相对于父级的 高度
现在,到垂直居中…
引导程序 5(2021 年更新)
Bootstrap 5 仍然基于 flexbox ,因此垂直居中的工作方式与 Bootstrap 4 相同。例如,
align-items-center
,justify-content-center
或自动边距可用于 flexbox 父级(row
或d-flex
)。align-items-center
(row
或d-flex
)justify-content-center
(d-flex flex-column
)my-auto
Bootstrap 5中的垂直中心
引导程序 4
您可以使用新的 flexbox & size 实用程序 来制作
container
全高和display: flex
。这些选项 不需要额外的 CSS (除了 容器的高度(即:html,body)必须为 100% )。选项 1
align-self-center
在 flexbox 子节点上https://codeply.com/go/fFqaDe5Oey
选项 2
align-items-center
在 flexbox 父级(.row
是display:flex; flex-direction:row
)https://codeply.com/go/BumdFnmLuk
选项 3
justify-content-center
在 flexbox 父级上(.card
是display:flex;flex-direction:column
)https://codeply.com/go/3gySSEe7nd
更多关于 Bootstrap 4 垂直居中
现在 Bootstrap 4 提供了 flexbox 和 其他实用程序,有许多垂直对齐的方法。 http://www.codeply.com/go/WG15ZWC4lf
1 - 使用自动边距垂直居中:
另一种垂直居中的方法是使用
my-auto
。这将使元素在其容器中居中。例如,h-100
使行全高,而my-auto
将使col-sm-12
列垂直居中。垂直居中使用自动边距 演示
my-auto
表示垂直y轴上的边距,相当于:2 - 带有 Flexbox 的垂直中心:
由于 Bootstrap 4
.row
现在是display:flex
您可以简单地使用align-self-center
在任何列上将其垂直居中…或者,在整个
align-items-center
.row
以垂直居中对齐所有col-*
在行中…垂直中心不同高度的列 演示
请参阅此 Q/A 以居中,但保持相同的高度
3 - 使用 Display Utils 垂直居中:
Bootstrap 4 具有可用于
display:table
、display:table-cell
、display:inline
等的显示工具,这些工具可以与 垂直对齐方式 对齐使用内联块或表格单元格元素。使用 Display Utils 演示 垂直居中
更多示例
<div>
中的垂直中心图像.container 中的垂直居中 .row
<div>
中的垂直中心和底部父项内的垂直中心子项
垂直中心全面屏jumbotron
重要的! 我提到身高了吗?
请记住,垂直居中是相对于 父元素的高度而言的。如果您想以整个页面为中心,在大多数情况下,这应该是您的 CSS…
或在父容器/容器上使用
min-height: 100vh
(在 Bootstrap 4.1+ 中为min-vh-100
)。如果你想在父元素中居中一个子元素。父级必须具有定义的 高度。另见:
bootstrap 4中的垂直对齐
Bootstrap 中心垂直和水平对齐