html - 使用带有弹性容器的 bootstrap 4 设计一些设计
问题描述
描述:
- A 和 B 组件的所有页面应具有 50% 的宽度和 100% 的高度。
- C 组件应水平和垂直居中。
- D 组件应位于顶部。
- A 包含 C。
- B 包含 D。
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<style>
html {
height: 100%;
}
body {
min-height: 100%;
background-color: blue;
}
.row-height100
{
min-height: 100%;
}
</style>
</head>
<body class="d-flex">
<div class="flex-fill flex-row row-height100 justify-content-center">
<div class="flex-fill p-2 bg-success d-flex justify-content-center row-height100">
<div>Flex item 1</div>
</div>
<div class="flex-fill p-2 bg-success d-flex justify-content-start row-height100">Flex item 2</div>
</div>
</body>
</html>
解决方案
- 将你的 body 或一个包裹元素的 div 元素更改为一个flexbox ,使用
d-flex
和 useh-100
使 flexbox 全高。 - 并
flex-fill
让你的元素"A"
和"B"
元素共享并占据整个宽度的 50%。 - 也
"A"
可以"B"
使用d-flex
. "C"
使用 . 将您和"D"
元素的内容水平居中justify-content-center
。- 使用 .垂直居中对齐
"C"
元素的内容align-items-center
。 - 使用垂直顶部对齐
"D"
元素的内容align-items-start
。
检查并运行以下代码片段以获取我上面描述的实际示例:
/* CSS */
html, body {height: 100%;width: 100%;}
body {min-height: 100%;background-color: blue;}
/* ignore following two css properties. Added just for visual example's sake */
.flex-fill {border: 4px solid #000;}
.flex-fill div {border: 4px solid #000; background-color: #FFF; padding: 5px;}
<!-- Scripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script><script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script><link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<!-- HTML -->
<div class="d-flex h-100">
<div class="flex-fill d-flex justify-content-center align-items-center bg-success">
<div>Flex item 1</div>
</div>
<div class="flex-fill d-flex justify-content-center align-items-start bg-success">
<div>Flex item 2</div>
</div>
</div>
推荐阅读
- c++ - 除了我的代码之外,还有什么理由会加快计时时间?
- r - 使用 map (purrr) 遍历 R 中的两个向量
- java - 如何在 oKHTTP android studio 中返回数据
- javascript - Firebase 云消息传递 - 从 JavaScript 客户端订阅主题
- c++ - 在 C++ 中存储 JSON 对象的设计模式是什么?
- python - 数据框列名 np 数组在列名之一之后有 \n
- php - 具有字符串值列类型的 laravel 集合 orderBy 方法
- python - 对两行之间的 Pandas 列求和
- git - 尝试通过共享库在 Jenkins 中使用 GitSCM 类签出代码时,为什么会出现 stackoverflow 错误?
- vue.js - 从 Quasar 引导文件访问存储