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

时间:2016-07-26 19:06:38

标签: html css

以下代码是我计划在我的网站上使用的自定义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>
&#13;
&#13;
&#13;

为了解决此问题,我在overflow-y: auto;课程中添加了行.extra。这是导致问题的原因。当您运行下面的代码时,输​​出的一半是&#34;缺少&#34;。我不确定为什么会这样。

&#13;
&#13;
* {
	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;
}
&#13;
<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>
&#13;
&#13;
&#13;

我很感激帮助解决这个问题。

1 个答案:

答案 0 :(得分:1)

输出的一半是&#34;缺失&#34;由于leftcenter类中定义的wrap位置。

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