我正在尝试在我的应用程序中创建一个弹出窗口,我需要将其垂直居中。我不知道弹出窗口的高度,所以我不能硬编码任何值。
我正在尝试以下代码:Centering in the Unknown
到目前为止,定心工作正常,但存在问题。我的弹出窗口有一个固定的宽度(它们没有响应),所以我的目标是当窗口的宽度低于弹出窗口的宽度时,应该出现一个水平的scroolbar。
在更高分辨率下,居中工作正常:
但是当窗口分辨率低于弹出分辨率时,会发生这种情况:(实际弹出窗口在视口下移动)
.block {
text-align: center;
background: #c0c0c0;
border: #a0a0a0 solid 1px;
position: fixed;
top: 0;
bottom: 0;
right: 0;
left: 0;
}
.block:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
}
.centered {
display: inline-block;
vertical-align: middle;
width: 500px;
padding: 10px 15px;
border: #a0a0a0 solid 1px;
background: #f5f5f5;
}
<div class="block">
<div class="centered">
<h1>Some text</h1>
<p>But he stole up to us again, and suddenly clapping his hand on my shoulder, said—"Did ye see anything looking like men going towards that ship a while ago?"</p>
</div>
</div>
答案 0 :(得分:1)
据我所知,不可能触发带位置固定元素的滚动条,但有一种方法可以防止弹出窗口在视口下移动,我们可以使用字体大小技巧,在容器上设置font-size:0;
,在内部框中设置font-size:16px;
左右,请阅读此内容以获取更多信息 - https://stackoverflow.com/a/5078297/483779然后将固定width
更改为max-width
以使其响应,再没有滚动条。
<强> Updated Demo 强>
我还添加了其他一些方法。 1st 将保持内容框始终位于中间。 第二一个与您当前的演示类似。而 3rd 将能够触发滚动条,但它使用的是绝对位置。
方法1:使用CSS3 transform
,浏览器支持IE9 +。
<强> JsFidde Demo 强>
.wrapper {
text-align: center;
background: #c0c0c0;
border: #a0a0a0 solid 1px;
position: fixed;
top: 0;
bottom: 0;
right: 0;
left: 0;
}
.centered {
width: 500px;
padding: 10px 15px;
border: #a0a0a0 solid 1px;
background: #f5f5f5;
position: absolute;
left: 50%;
top: 50%;
-ms-transform: translate(-50%, -50%);
-webkit-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
<div class="wrapper">
<div class="centered">
<h1>Some text</h1>
<p>But he stole up to us again, and suddenly clapping his hand on my shoulder, said—"Did ye see anything looking like men going towards that ship a while ago?"</p>
</div>
</div>
方法2:使用CSS表格布局,它应该适用于IE8 +。注意,在标记中添加了额外的<div>
。
<强> JsFiddle Demo 强>
.wrapper {
text-align: center;
background: #c0c0c0;
border: #a0a0a0 solid 1px;
position: fixed;
top: 0;
bottom: 0;
right: 0;
left: 0;
display: table;
width: 100%;
height: 100%;
}
.centered {
display: table-cell;
vertical-align: middle;
}
.content {
max-width: 500px;
padding: 10px 15px;
border: #a0a0a0 solid 1px;
background: #f5f5f5;
margin: auto;
}
<div class="wrapper">
<div class="centered">
<div class="content">
<h1>Some text</h1>
<p>But he stole up to us again, and suddenly clapping his hand on my shoulder, said—"Did ye see anything looking like men going towards that ship a while ago?"</p>
</div>
</div>
</div>
方法3:使用绝对位置,当视口小于内容框时触发滚动条,将position:relative
更改为absolute
<强> JsFiddle Demo 强>
.wrapper {
text-align: center;
background: #c0c0c0;
border: #a0a0a0 solid 1px;
box-sizing: border-box;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
display: table;
border-collapse: collapse;
width: 100%;
height: 100%;
}
.centered {
display: table-cell;
vertical-align: middle;
}
.content {
width: 500px;
padding: 10px 15px;
border: #a0a0a0 solid 1px;
background: #f5f5f5;
margin: auto;
}
<div class="wrapper">
<div class="centered">
<div class="content">
<h1>Some text</h1>
<p>But he stole up to us again, and suddenly clapping his hand on my shoulder, said—"Did ye see anything looking like men going towards that ship a while ago?"</p>
</div>
</div>
</div>