【发布时间】:2020-12-27 11:10:55
【问题描述】:
我正在构建一个仅包含 HTML 和 CSS 的待办事项列表,我只希望网站主要部分的内容通过侧滚动条移动。
页面分为 3 个部分,分别包含在一个 flexbox 容器和一个标题中。在图像中,您可以看到代表该想法的模型。
主要部分是中间部分,必须只允许垂直滚动,所有其他元素,例如标题和次要部分必须保持固定在屏幕上。
我做了一个精简版的代码来简化我的情况并使其更容易理解:
HTML 代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>To-do list</title>
</head>
<body>
<header>
<h1>To-Do List</h1>
</header>
<div class="flex-container">
<section class="secondary-section">
<h2>Secondary Section</h2>
</section>
<section class="main-section">
<h2>Main Section</h2>
</section>
<section class="secondary-section">
<h2>Secondary Section</h2>
</section>
</div>
</body>
</html>
CSS 代码
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
header {
background-color: black;
width: 100%;
padding: 20px;
text-align: center;
color: white;
}
.flex-container {
display: flex;
flex: 1 1 0;
text-align: center;
height: 1000px;
}
.flex-container h2 {
margin-top: 30px;
font-size: 2rem;
}
.main-section {
background-color: rgb(255, 222, 144);
width: 100%;
}
.secondary-section {
background-color: rgb(114, 181, 245);
width: 100%;
}
我一直在尝试使用“位置:固定”并配置部分溢出,但我没有成功,将这些 flex 元素固定在屏幕上而不滚动移动它们的正确方法是什么?
【问题讨论】:
-
感谢您的回答,这对理解很有帮助,但我需要滚动条位于屏幕的右上角,但可能通过一些调整我可以解决这个问题。
标签: html css scroll flexbox fixed