gpt4 book ai didi

html - CSS 属性 "overflow-y: auto"导致非常意外的结果

转载 作者:太空宇宙 更新时间:2023-11-04 09:57:29 25 4
gpt4 key购买 nike

以下代码是我计划在我的网站上使用的自定义 404 页面的一部分。但是,当我添加代码行 overflow-y: auto;

时,出现了一个主要问题

下面的代码具有我预期的输出。然而,当 div 中的代码超过 75vh 时,溢出是不可见的。

* {
margin: 0;
padding: 0;
}

.main {
min-height: 100vh;
font-size: 1em;
overflow-Y: hidden;
}

.center {
float: left;
position: relative;
left: 50%;
}

.wrap {
width: 100%;
float: left;
position: relative;
left: -50%;
}

.load_extra {
display: block;
position: fixed;
z-index: 11;
bottom: 15px;
}

.prep {
align: center;
background: #00eaff;
outline: none;

padding: 8px;

color: white;

border-color: white;
border-style: dotted;
border-width: 3px;
border-radius:50%;
font-size: 1.375em;
}

.extra {
display: block;
position: fixed;
bottom: 10px;
max-height: 75vh;
width: 80vw;
z-index: 10;
}

pre {
font-family: monospace, monospace;
font-size: 0.85em;
display: block;
overflow-y: auto;
word-break: break-all;
white-space:normal;
padding: 10px;
margin: 0 0 10px;
line-height: 1.42857143;
color: #333;
word-break: break-all;
word-wrap: break-word;
background-color: #f5f5f5;
border: 1px solid #ccc;
border-radius: 4px;
max-height: 50vh;
}
<body class="main">
<div class="center load_extra">
<div class="wrap">
<button id="extra" class="prep">Button</button>
</div>
</div>

<div id="infoCont" class="center extra">
<div class="wrap">
<h1>Extra Information</h1>
<pre>Some URL</pre>
<p>The requested URL shown above could not be found on the server</p>
<hr>
</div>
</div>
</body>

为了解决这个问题,我在 .extra 类中添加了 overflow-y: auto; 行。这就是造成问题的原因。当您运行以下代码时,一半的输出是“缺失的”。我不确定为什么会这样。

* {
margin: 0;
padding: 0;
}

.main {
min-height: 100vh;
font-size: 1em;
overflow-Y: hidden;
}

.center {
float: left;
position: relative;
left: 50%;
}

.wrap {
width: 100%;
float: left;
position: relative;
left: -50%;
}

.load_extra {
display: block;
position: fixed;
z-index: 11;
bottom: 15px;
}

.prep {
align: center;
background: #00eaff;
outline: none;

padding: 8px;

color: white;

border-color: white;
border-style: dotted;
border-width: 3px;
border-radius:50%;
font-size: 1.375em;
}

.extra {
display: block;
position: fixed;
bottom: 10px;
max-height: 75vh;
width: 80vw;
z-index: 10;
overflow-y: auto;
}

pre {
font-family: monospace, monospace;
font-size: 0.85em;
display: block;
overflow-y: auto;
word-break: break-all;
white-space:normal;
padding: 10px;
margin: 0 0 10px;
line-height: 1.42857143;
color: #333;
word-break: break-all;
word-wrap: break-word;
background-color: #f5f5f5;
border: 1px solid #ccc;
border-radius: 4px;
max-height: 50vh;
}
<body class="main">
<div class="center load_extra">
<div class="wrap">
<button id="extra" class="prep">Button</button>
</div>
</div>

<div id="infoCont" class="center extra">
<div class="wrap">
<h1>Extra Information</h1>
<pre>Some URL</pre>
<p>The requested URL shown above could not be found on the server</p>
<hr>
</div>
</div>
</body>

如能帮助解决此问题,我将不胜感激。

最佳答案

由于 centerwrap 类中定义的 left 位置,一半的输出“丢失”了。

center 类将从 50% 开始定位您的容器,然后内部容器 (wrap) 再次以 -50% 重新定位。由于溢出应用于父 div,因此一半内容不再可见。

一个解决方案可能是将 overflow-y: auto; 移动到 wrap 类。

另一种是选择另一种方式使infoCont div居中。

<div id="infoCont" class="extra">
<h1>Extra Information</h1>
<pre>Some URL</pre>
<p>The requested URL shown above could not be found on the server</p>
<hr>
</div>

.extra {
display: block;
position: fixed;
bottom: 10px;
max-height: 75vh;
width: 80vw;
z-index: 10;
overflow-y: auto;
margin: 0 auto; /* set margin to auto */
left: 0; /* set also left and right because position is fixed */
right: 0;
}

查看工作 example .

关于html - CSS 属性 "overflow-y: auto"导致非常意外的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38597999/

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