在Bootstrap 4.1.3中,要使一个自动溢出的div
居中并占用剩余空间,可以使用Flexbox布局。以下是实现这一目标的基础概念和相关步骤:
.container
或.container-fluid
类来创建一个容器。.d-flex
类,使其成为一个Flex容器。.justify-content-center
和.align-items-center
类将内容水平和垂直居中。div
上应用.overflow-auto
类,使其内容超出时显示滚动条。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Centered Overflow Div</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<style>
.content {
height: 100vh; /* 设置高度为视口高度 */
width: 100%;
}
.overflow-div {
max-height: 80%; /* 设置最大高度 */
width: 80%; /* 设置宽度 */
background-color: #f8f9fa;
padding: 20px;
border: 1px solid #dee2e6;
}
</style>
</head>
<body>
<div class="container d-flex justify-content-center align-items-center h-100">
<div class="overflow-div overflow-auto">
<!-- 这里放置你的内容 -->
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<!-- 添加更多内容以测试溢出效果 -->
</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>
.container
:创建一个响应式固定宽度的容器。.d-flex
:将容器设置为Flexbox布局。.justify-content-center
:水平居中对齐内容。.align-items-center
:垂直居中对齐内容。.overflow-div
:自定义样式类,用于设置溢出div
的最大高度和宽度。.overflow-auto
:当内容超出容器时显示滚动条。通过以上步骤和示例代码,你可以轻松实现一个自动溢出的div
在Bootstrap 4.1.3中居中并占用剩余空间。
领取专属 10元无门槛券
手把手带您无忧上云