英文:
Stop child overflowing parent
问题
如何使侧边栏可滚动,最好使用Bootstrap类?
要使侧边栏可滚动,您可以使用Bootstrap的overflow-auto
类,该类会在内容溢出时自动显示滚动条。请按照以下步骤进行操作:
-
将
overflow-y: hidden
和max-height: 100vh
从main
中移除,以允许内容溢出。 -
为侧边栏的
list-group
元素添加Bootstrap的overflow-auto
类。
修改后的HTML和CSS代码如下:
<!-- language: lang-css -->
main {
height: 100vh;
}
.list-group.scrollarea {
overflow-y: auto;
}
<!-- language: lang-html -->
<div class="list-group list-group-flush scrollarea">
<!-- 你的侧边栏内容 -->
</div>
这将使侧边栏可滚动,只要内容溢出,滚动条就会出现。
英文:
I have a page with a sidebar. The sidebar content overflows the page, so I've added overflow-y: hidden
and max-height: 100vh
to main
and overflow-y: auto
to the list group to make it scroll but that does not work. The overflowing content is cut off but the scroll bar is not there.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
main {
height: 100vh;
max-height: 100vh;
overflow-y: hidden;
}
.scrollarea {
overflow-y: auto;
}
<!-- language: lang-html -->
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
</head>
<body>
<main>
<header class="col-12 navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
<div class="navbar-nav ms-auto">
<div class="nav-item text-nowrap justify-self-end">
<a class="nav-link px-3" href="#">Sign out</a>
</div>
</div>
</header>
<div class="container-fluid d-flex flex-column">
<div class="row flex-grow-1">
<div class="col-md-4 border-end gx-0 d-flex flex-column flex-shrink-0">
<div class="d-flex justify-content-center mt-2 border-bottom">
<h2>Placeholder</h2>
</div>
<div class="list-group list-group-flush scrollarea">
<a href="#" class="list-group-item list-group-item-action active">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
</div>
</div>
<div class="col-md-8">
main content
</div>
</div>
</div>
</main>
</body>
</html>
<!-- end snippet -->
How can I make the sidebar scrollable, preferably using Bootstrap classes?
答案1
得分: 1
以下是您提供的内容的中文翻译:
这里是一个使用flexbox
技巧和一些position
属性来实现的示例,请注意,这个示例不涵盖小屏幕视图中的响应式布局主题。
- 使用
flex
CSS来准备布局结构(头部、侧边栏、主内容)。 - 然后在可滚动区域上使用
position
:在relative
容器内的absolute
框。
希望对您有所帮助,祝您编程愉快!
英文:
Here a sample using flexbox
trick with some position
property to achieve that, and please do take note that the sample here is not covering the topic of responsive layout in small screen view.
- Use
flex
css to prepare the layout structure (header, sidebar, main content) - Then use
position
on the scrollable area: anabsolute
box within arelative
container
Hope it helps and Happy coding!
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
main {
height: 100vh;
}
<!-- language: lang-html -->
<html>
<head>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
</head>
<body>
<main class="d-flex flex-column">
<header class="col-12 navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
<div class="navbar-nav ms-auto">
<div class="nav-item text-nowrap justify-self-end">
<a class="nav-link px-3" href="#">Sign out</a>
</div>
</div>
</header>
<div class="flex-grow-1 container-fluid d-flex flex-column">
<div class="row flex-grow-1">
<div class="col-md-4 border-end gx-0 d-flex flex-column">
<div class="d-flex justify-content-center mt-2 border-bottom">
<h2>Placeholder</h2>
</div>
<div class="flex-grow-1 position-relative">
<div class="position-absolute w-100 h-100 overflow-y-auto">
<div class="list-group list-group-flush scrollarea">
<a href="#" class="list-group-item list-group-item-action active">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
<a href="#" class="list-group-item list-group-item-action">
<div class="">
<div class="d-flex align-items-center justify-content-between">
<strong>Title</strong>
<span class="badge rounded-pill bg-danger">10</span>
</div>
<p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Ipsa velit necessitatibus eveniet blanditiis eum maxime odio autem labore omnis repudiandae cumque explicabo nisi suscipit iste rem, a sapiente voluptas incidunt?</p>
</div>
</a>
</div>
</div>
</div>
</div>
<div class="col-md-8">
main content
</div>
</div>
</div>
</main>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论