twitter-bootstrap - 似乎无法让主体内的容器 div 与 Bootstrap4 垂直居中对齐
问题描述
我已经经历了关于同一主题的多个 Stack Overflow 问题,但是我根本无法让它发挥作用。我几乎尝试了以下问题中提到的所有解决方案,但似乎没有一个对我有用。
这就是我的代码的样子
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<title>Document</title>
<style>
body {
height: 100%;
}
.col {
border: 1px solid red;
}
.row {
height: 200px;
border: 1px solid blue;
}
</style>
</head>
<body>
<div class="container h-100 align-items-center">
<div class="jumbotron my-auto">
<div class="row">
<div class="col align-self-end">
col1
</div>
<div class="col align-self-start">
col2
</div>
</div>
<div class="row align-items-center">
<div class="col">
col1
</div>
<div class="col">
col2
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</body>
</html>
我在这里想念什么?
附件是我的页面外观截图。
我知道我可以使用属性将 div 定位在页面的中心,position
但我很想知道是否可以仅使用 Bootstrap 来完成而不添加任何自定义 CSS。
解决方案
一种解决方案是声明container
要采用100vh
(视口高度的 100%),然后您可以向其添加d-flex
类,并jumbotron
使用align-self-center
.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<title>Document</title>
<style>
.col {
border: 1px solid red;
}
.row {
height: 200px;
border: 1px solid blue;
}
.my-container {
height: 100vh;
}
</style>
</head>
<body>
<div class="container my-container d-flex">
<div class="jumbotron w-100 align-self-center">
<div class="row">
<div class="col align-self-end">
col1
</div>
<div class="col align-self-start">
col2
</div>
</div>
<div class="row align-items-center">
<div class="col">
col1
</div>
<div class="col">
col2
</div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
</body>
</html>
推荐阅读
- javascript - 试图在函数范围之外的javascript中使用变量
- android - 防止用户在 Facebook 和 Google 登录按钮上单击两次
- .htaccess - 如果我从 Google 访问,err_too_many_redirects 错误
- scala - spark根据前一行的旧值对列进行计算
- java - 如何在java中测试RMI程序
- azure - 我们如何在 Xamarin 表单中删除/自定义 Azure AD B2C 身份验证 webview 页面标题栏?
- logging - Stackdriver Logging、Logback 和 Logger 名称
- java - 混合实例和静态同步方法,得到意想不到的测试输出
- reactjs - 点击地图后刷新调用api
- c# - 如何隐藏由于此过程而创建的 CMD 窗口?