html - 如何在 HTML 中使内部 div 元素可滚动(前提是主体不是)?

标签 html css

在我的 HTML 页面中,我想要以下内容:

  • HTML 和正文占据屏幕的整个高度。不可滚动。
  • 顶栏,固定,高度为 40 像素。
  • 左侧栏,固定,宽度为 40 像素。
  • 应该是可滚动的内容。

到目前为止,一切正常,但是,我似乎无法使内容部分在 y 方向上滚动,即使它溢出屏幕。我的代码如下所示:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        html, body {
            height: 100%;
            margin: 0;
            padding: 0;
        }
        body {
            overflow: hidden;
        }
        .top {
            width: 100%;
            height: 40px;
            background: rgba(0, 0, 0, 0.5);
            position: fixed;
            top: 0;
            left: 0;
        }
        .left {
            width: 40px;
            height: 100%;
            background: rgba(0, 0, 0, 0.25);
            position: fixed;
            top: 40px;
            left: 0;
        }
        .content {
            max-width: 480px;
            background: red;
            margin-top: 40px;
            margin-left: 40px;
            overflow-y: auto;
        }
    </style>
</head>
<body>
    <div class="top"></div>
    <div class="left"></div>
    <div class="content">
        <div>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
        <div>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
        <div>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
        <div>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
        <div>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
        <div>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
        <div>
            Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
        </div>
    </div>
</body>
</html>

你可以在这里看到:

https://jsfiddle.net/q123h658/

感谢您的帮助。

最佳答案

要使 overflow-y: auto 起作用,您需要指定 div 的 max-height

Working fiddle

关于html - 如何在 HTML 中使内部 div 元素可滚动(前提是主体不是)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57233633/

相关文章:

html - 如何对齐固定元素内的文本?

html - 图片在响应式设计中没有正确对齐

html - css 黑色文本包含彩色像素

html - 更改背景图像的参数 : url at runtime css

html - 如何让 div 溢出而不将 IE 中的电子邮件中的其他内容向下推?

html - 父标签的样式,当子类 radio :checked

javascript - 使用 javascript 设置表单操作不起作用

php - 响应式图像和文本对齐

javascript - 用 jquery 改变 !doctype

class - 每次应用类时的 CSS3 动画(Firefox 错误?)