我正试图在没有固定/指定高度的DIV中垂直对中一幅图像。我尝试过在以CSS技巧中未知的文章为中心上构建,但是要实现这个目标,您需要指定高度。相当欺骗性的文章标题。
这是我的JS Fiddle:http://jsfiddle.net/6J2WA/
这就是我想要的(图片):http://cl.ly/image/1k0h262c2c3s
CSS
/* This parent can be any width and height */
.block {
text-align: center;
}
/* The ghost, nudged to maintain perfect centering */
.block:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em; /* Adjusts for spacing */
}
/* The element to be centered, can
also be of any width and height */
.centered {
display: inline-block;
vertical-align: middle;
}
.inner {
max-width: 960px;
margin: 0 auto;
overflow: hidden;
background: #ef4;
}
.c-3 {
width: 29.3%;
float: left;
margin-left: 6%;
background: #e4f;
}
#you-and-us .c-3 { width: 33.5%; }
#you-and-us .c-3:first-child { text-align: right; margin: 0; }
#you-and-us .c-3:nth-child(2) { width: 21%; text-align: center; position: relative; }发布于 2013-03-13 17:44:46
您不能使用伪元素技术的原因是您没有足够的祖先元素,这些元素的高度是#you-and-us元素的100%。
如果抛出浮点数并将display: table-cell用于.c-3元素,则可以简化整个过程。
http://jsfiddle.net/6J2WA/5/
/* This parent can be any width and height */
.block {
text-align: center;
}
.inner {
max-width: 960px;
margin: 0 auto;
overflow: hidden;
background: #ef4;
}
.c-3 {
display: table-cell;
background: #e4f;
}
#you-and-us .c-3 { width: 33.5%; }
#you-and-us .c-3:first-child { text-align: right; }
#you-and-us .c-3:nth-child(2) { width: 21%; text-align: center; position: relative; background: yellow; vertical-align: middle; }https://stackoverflow.com/questions/15392002
复制相似问题