我试图将 DIV 元素的内容居中,该元素的滚动条隐藏在父元素后面。看起来像这样:
HTML:
<div class="parent">
<div class="child">
<div class="content">
This should be centered! This should be centered!This should be centered!This should be centered!This should be centered!This should be centered!This should be centered!This should be centered!This should be centered!This should be centered!This should be centered!This should be centered!This should be centered!This should be centered!This should be centered!
</div>
</div>
</div>
CSS:
.parent {
width: 300px; /* unknown width - 100%* - browser size */
height: 200px; /* unknown width - 100%* - broser size */
overflow: hidden;
background: #dedede;
}
.child {
width: 120%;
height: 100%;
overflow-y: scroll;
background: #000;
}
.content {
width: 150px;
margin: 0 auto;
background: #fff;
}
我尝试了各种各样的东西,但似乎没有任何效果。
我希望跨浏览器完成此操作,如果用户重新调整窗口大小,它仍然可以工作。我的问题是不同浏览器中的滚动条宽度会发生变化,并且屏幕宽度也是未知的。