【发布时间】:2012-12-16 22:26:43
【问题描述】:
有没有办法将 DIV 垂直和水平居中,但重要的是,当窗口小于内容时,内容不会被剪切 div 必须有背景颜色和宽度和高度。
我总是以绝对定位和负边距使 div 居中,就像提供的示例一样。但它有一个问题,它会削减顶部的内容。有没有办法让 div .content 居中而不出现这个问题?
我这里有例子可以玩:http://jsbin.com/iquviq/1/edit
CSS:
body { margin: 0px; }
.background {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background-color: yellow;
}
/*
is there a better way than the absolute positioning and negative margin to center the div .content: div with background color a width and a hight?:
*/
.content {
width: 200px;
height: 600px;
background-color: blue;
position:absolute;
top:50%;
left:50%;
margin-left:-100px;/* half width*/
margin-top:-300px;/* half height*/
}
HTML:
<div class="background">
<div class="content"> some text </div>
</div>
我的问题不是重复的“如何使元素水平和垂直居中?” 1-我的问题之前被问过。 (只需检查日期)。 2-我的问题问得很清楚,而且是黑色的:“当窗口小于内容时,内容不会被剪切”
【问题讨论】:
-
我的问题不同于:“在页面上垂直和水平居中的最佳方式?”。正如我一开始问得很清楚的“当窗口小于内容时,内容不会被剪切”@Josh Crozier:我的问题与“如何将元素水平和垂直居中?” 1- 我的问题是两年前提出的。另一个问题是一年前提出的。 2-我的问题问得很清楚而且是黑色的:“当窗口小于内容时,内容不会被剪切”查看本教程以使用 CSS 水平和垂直对齐 div:frontendguruji.com/blog/…