Bootstrap 5 是一个流行的前端框架,用于快速构建响应式网站和应用程序。在 Bootstrap 5 中,固定高度容器并支持滚动内容可以通过以下几种方式实现:
以下是使用 Bootstrap 5 和一些自定义 CSS 来创建固定高度容器并支持滚动内容的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fixed Height Container with Scroll</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
.scrollable-container {
height: 300px; /* 固定高度 */
overflow-y: auto; /* 启用垂直滚动 */
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<div class="container mt-5">
<div class="scrollable-container">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. ...</p>
<!-- 更多内容 -->
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
.scrollable-container {
height: 300px; /* 固定高度 */
overflow-y: auto; /* 启用垂直滚动 */
border: 1px solid #ccc;
padding: 10px;
}
::-webkit-scrollbar
伪元素。.scrollable-container::-webkit-scrollbar {
width: 10px;
}
.scrollable-container::-webkit-scrollbar-track {
background: #f1f1f1;
}
.scrollable-container::-webkit-scrollbar-thumb {
background: #888;
border-radius: 5px;
}
.scrollable-container::-webkit-scrollbar-thumb:hover {
background: #555;
}
通过以上方法,你可以轻松地在 Bootstrap 5 中创建一个固定高度且支持滚动内容的容器。
领取专属 10元无门槛券
手把手带您无忧上云