【发布时间】:2016-09-10 07:22:25
【问题描述】:
这是我正在处理的页面:http://en08-phx.stablehost.com/~news/test.html
这是我用来使 div 居中的代码:
div {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 70%;
height: auto;
padding: 20px;
background-color:rgba(255, 255, 255, 0.2);
color: white;
text-align: center;
border-radius:5px;
border:2px solid rgba(255, 255, 255, 0.5);
}
每当我缩小浏览器的宽度或使用手机时,页面顶部就会开始被截断。
无论浏览器宽度的大小如何,我都希望 div 居中。但是,如果浏览器的高度太小,我宁愿只在顶部/底部添加 10px 的边距并确保所有文本都显示出来。
我到底做错了什么?
【问题讨论】:
-
这是因为您将 div 居中,无论设备的高度是多少,因此上部被截断。