当我点击链接时,会弹出一个弹出窗口。问题是它没有与屏幕中心对齐。顺便说一句,我的代码也可以帮助网站(和弹出窗口)在移动版本中完美呈现。
HTML代码:
<a href="#" data-reveal-id="exampleModal">POP UP</a>
<div id="exampleModal" class="reveal-modal">
<h2>POP UP</h2>
<p>
<font size="4">window window window.window window window. window.
</font>
</p>
<a class="close-reveal-modal">×</a>
</div>
css代码:
.reveal-modal
{
background:#e1e1e1;
visibility:hidden;
display:none;
top:100px;
left:50%;
width:820px;
position:absolute;
z-index:41;
padding:30px;
-webkit-box-shadow:0 0 10px rgba(0,0,0,0.4);
-moz-box-shadow:0 0 10px rgba(0,0,0,0.4);
box-shadow:0 0 10px rgba(0,0,0,0.4)
}
我尝试了一些正确的:50%也没有用。不应该离开工作吗?
答案 0 :(得分:16)
这些是要做的更改:
<强> CSS:强>
#container {
width: 100%;
height: 100%;
top: 0;
position: absolute;
visibility: hidden;
display: none;
background-color: rgba(22,22,22,0.5); /* complimenting your modal colors */
}
#container:target {
visibility: visible;
display: block;
}
.reveal-modal {
position: relative;
margin: 0 auto;
top: 25%;
}
/* Remove the left: 50% */
<强> HTML:强>
<a href="#container">Reveal</a>
<div id="container">
<div id="exampleModal" class="reveal-modal">
........
<a href="#">Close Modal</a>
</div>
</div>
答案 1 :(得分:2)
如果你想要的效果是无论你在哪里滚动到屏幕中心都是中心,它甚至比那更简单:
在CSS中使用(例如)
div.centered{
width: 100px;
height: 50px;
position:fixed;
top: calc(50% - 25px); // half of width
left: calc(50% - 50px); // half of height
}
不需要JS。
答案 2 :(得分:1)
/*-------- Bootstrap Modal Popup in Center of Screen --------------*/
/*---------------extra css------*/
.modal {
text-align: center;
padding: 0 !important;
}
.modal:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -4px;
}
.modal-dialog {
display: inline-block;
text-align: left;
vertical-align: middle;
}
/*----- Modal Popup -------*/
<div class="modal fade" role="dialog">
<div class="modal-dialog" >
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h5 class="modal-title">Header</h5>
</div>
<div class="modal-body">
body here
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
答案 3 :(得分:0)
为了使弹出窗口准确居中,只需将div高度的一半的负顶边距和div宽度的一半的负左边距应用在一起就很简单。对于此示例,如下所示:
.div {position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 50%;
}