gpt4 book ai didi

html - 如何只允许在主要部分滚动? (HTML 和 CSS)

转载 作者:行者123 更新时间:2023-12-04 08:56:26 24 4
gpt4 key购买 nike

我正在构建一个只有 HTML 和 CSS 的待办事项列表,我只希望网站主要部分的内容被侧滚动条移动。
该页面分为 3 个部分,它们包含在一个 flexbox 容器中,以及一个标题。在图像中,您可以看到代表该想法的模型。
Page Layout
主要部分在中间,必须只允许垂直滚动,所有其他元素如标题和次要部分必须保持固定在屏幕上。
我做了一个简化版本的代码来简化我的情况并使其更容易理解:
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 元素固定在屏幕上的正确方法是什么?

最佳答案

试试这个,
对于固定 标题通过添加

header {
position: fixed;
height:80px;
top: 0;
}
已修复 次级部件
.secondary-section {
width: calc(100% / 3);
height: 100vh;
position: fixed;
top: 80px; // height of header
}
可滚动 主要部分
.main-section {
width: calc(100% / 3);
position:absolute;
bottom:0;
right:calc(100% / 3);
left:calc(100% / 3);
top: 80px; // height of header
height: inherit;
z-index: -1;
}

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
header {
background-color: black;
width: 100%;
padding: 20px;
text-align: center;
color: white;
position: fixed;
top: 0;
height:80px;
}

.flex-container {
display: flex;
flex: 1 1 0;
text-align: center;
height: 1000px;
top:80px;
}

.flex-container h2 {
margin-top: 30px;
font-size: 2rem;
}

.main-section {
background-color: rgb(255, 222, 144);
width: calc(100% / 3);
position:absolute;
bottom:0;
right:calc(100% / 3);
left:calc(100% / 3);
top: 0px;
height: inherit;
z-index: -1;
top:80px;
}

.secondary-section {
background-color: rgb(114, 181, 245);
width: calc(100% / 3);
height: 100vh;
position: fixed;
top:80px;
}
.secondary-section.right{
right:0;
}
<!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>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
</section>
<section class="secondary-section right">
<h2>Secondary Section</h2>
</section>
</div>
</body>
</html>

关于html - 如何只允许在主要部分滚动? (HTML 和 CSS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63804519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com