我正在实现一个只有 CSS 的模式。所以最后我有一个固定位置的容器和一个里面的div。我想要完成的是 div 始终在固定容器内居中,并且如果 div 的全部内容不可见(如果您调整浏览器大小)则滚动。这是我的代码:
html:
<div>
<a href="#openModal">open</a>
<div id="openModal" class="modalDialog">
<div class="container">
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
<div>test</div>
</div>
</div>
</div>
CSS:
.modalDialog {
position: fixed;
top:0;
left:0;
right: 0;
bottom: 0;
background: rgba(0,0,0,0.8);
z-index: 99999;
opacity: 0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
overflow: hidden;
}
.modalDialog:target {
opacity: 1;
pointer-events: auto;
}
.container {
position: relative;
padding: 5px 20px 13px 20px;
background: #fff;
margin: 10% auto;
overflow-y: scroll;
}
最佳答案
要使 container
div 居中,您必须添加 text-align: center;
。要在你的 div 中有一个滚动条,我再次猜测你的 container
div,你必须向你的 div 添加一个高度。然后,您可以使用 overflow: scroll;
添加滚动条。
.container {
position: relative;
padding: 5px 20px 13px 20px;
background: #fff;
margin: 10% auto;
overflow: scroll;
text-align: center;
height: 350px;
}
关于css - 将 div 居中并使其在固定容器内滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36353941/