官方地址
https://getbootstrap.com/docs/4.4/utilities/position/
其他文档地址
- https://getbootstrap.net
- http://bs4.vx.link
通用属性
.postition-static
.position-relative
.position-absolute
.position-fixed
.position-sticky
<div class="container position-relative" style="height: 1000px;">
<h1>通用属性</h1>
<div class="position-static bg-primary" style="width: 100px; height: 100px;">position-static</div>
<div class="position-relative bg-secondary" style="width: 100px; height: 100px;">position-relative</div>
<div class="position-absolute bg-success" style="width: 100px; height: 100px;">position-absolute</div>
<div class="position-fixed bg-warning" style="left: 10px; width: 100px; height: 100px;">position-fixed</div>
<div class="position-sticky bg-danger" style="top: 10px; width: 100px; height: 100px;">position-sticky</div>
<div class="position-sticky bg-dark" style="top: 10px; width: 100px; height: 100px;">position-sticky</div>
<div class="position-sticky bg-light" style="top: 10px; width: 100px; height: 100px;">position-sticky</div>
</div>
固定在顶部
.fixed-top
<div class="container">
<h1>固定在顶部</h1>
<p class="lead">将一个元素置于可视区的顶部,从边到边。必要时你需要增加额外的 CSS。</p>
<div class="fixed-top bg-primary" style="width: 100px; height: 100px;">固定在顶部</div>
</div>
固定在底部
.fixed-bottom
<div class="container">
<h1>固定在底部</h1>
<p class="lead">一个元素置于可视区的底部,从边到边。必要时你需要增加额外的 CSS。</p>
<div class="fixed-bottom bg-primary" style="width: 100px; height: 100px;">固定在顶部</div>
</div>
sticky置顶
.sticky-top
<div class="container border border-primary bg-light" style="height: 1000px;">
<h1>粘性置顶</h1>
<div class="sticky-top bg-primary" style="width: 100px; height: 100px;">sticky-top bg-primary</div>
<div class="sticky-top bg-success" style="width: 100px; height: 100px;">sticky-top bg-success</div>
<div class="sticky-top bg-danger" style="width: 100px; height: 100px;">sticky-top bg-danger</div>
</div>
整体代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no"/>
<title>11定位</title>
<!--<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">-->
<!-- 新 Bootstrap4 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<pre class="pre-scrollable">
若不明白sticky,阅读https://www.cnblogs.com/coco1s/p/6402723.html
or 翻阅自已的笔记
</pre>
<div class="container position-relative" style="height: 1000px;">
<h1>通用属性</h1>
<div class="position-static bg-primary" style="width: 100px; height: 100px;">position-static</div>
<div class="position-relative bg-secondary" style="width: 100px; height: 100px;">position-relative</div>
<div class="position-absolute bg-success" style="width: 100px; height: 100px;">position-absolute</div>
<div class="position-fixed bg-warning" style="left: 10px; width: 100px; height: 100px;">position-fixed</div>
<div class="position-sticky bg-danger" style="top: 10px; width: 100px; height: 100px;">position-sticky</div>
<div class="position-sticky bg-dark" style="top: 10px; width: 100px; height: 100px;">position-sticky</div>
<div class="position-sticky bg-light" style="top: 10px; width: 100px; height: 100px;">position-sticky</div>
</div>
<div class="container">
<h1>固定在顶部</h1>
<p class="lead">将一个元素置于可视区的顶部,从边到边。必要时你需要增加额外的 CSS。</p>
<div class="fixed-top bg-primary" style="width: 100px; height: 100px;">固定在顶部</div>
</div>
<div class="container">
<h1>固定在底部</h1>
<p class="lead">一个元素置于可视区的底部,从边到边。必要时你需要增加额外的 CSS。</p>
<div class="fixed-bottom bg-primary" style="width: 100px; height: 100px;">固定在顶部</div>
</div>
<div class="container border border-primary bg-light" style="height: 1000px;">
<h1>粘性置顶</h1>
<div class="sticky-top bg-primary" style="width: 100px; height: 100px;">sticky-top bg-primary</div>
<div class="sticky-top bg-success" style="width: 100px; height: 100px;">sticky-top bg-success</div>
<div class="sticky-top bg-danger" style="width: 100px; height: 100px;">sticky-top bg-danger</div>
</div>
<!--<div style="width: 10px; height: 9000px;"></div>-->
<!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<!-- bootstrap.bundle.min.js 用于弹窗、提示、下拉菜单,包含了 popper.min.js -->
<script src="https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
<!-- 最新的 Bootstrap4 核心 JavaScript 文件 -->
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>