是否有可能以"绝对"父母为中心?

Kin*_*Kin 5 html css

<style type="text/css">
.square {
    width:251px;
    height:207px; 
    border: 1px solid #d6d6d6;
    -webkit-box-shadow: 1px 1px 3px rgba(0,0,0,.1);
    -moz-box-shadow: 1px 1px 3px rgba(0,0,0,.1);
    box-shadow: 1px 1px 3px rgba(0,0,0,.1);
    margin: 10px;
    overflow: hidden;
    position: relative;
    background-color:#fff;
    /*display: inline-block;*/
    float: left;
    cursor: pointer;
    text-align: left;
}
.square img {
    display: block;
    margin: 0px;
    padding: 9px;
    width:234px !important;
    height:190px !important;
    position:absolute;
}
.square .caption {
    width:214px;
    height:170px;
    background:#000;
    color:#fff;
    padding:10px;
    position:absolute;
    left:9px;
    top:9px;
    /*display:none;*/
    filter:alpha(opacity=80);
    -moz-opacity:0.8;
    -khtml-opacity: 0.8;  
    opacity: 0.8;
}
.square .text{
    border:1px dotted #d6d6d6;
    margin: 10px;
    padding: 5px;
    vertical-align: center;
    max-height: 100px;
    overflow: auto;
}
.square .until {
    font-size: 12px;
    font-style: italic;
    position: absolute;
    right: 10px;
    bottom: 5px;
}
</style>

<div class="square">
    <a href="/" >
        <img width="234" height="190" src="files/2011/12/17.jpg"  alt="17" title="17"/>
    </a>
    <a href="/" rel="bookmark">
        <div class="caption">
            <h2>Half A Beatle</h2>
            <div class="text">lol</div>
            <div class="until">Until: 01 01 2012</div>
        </div>
    </a>
</div>
Run Code Online (Sandbox Code Playgroud)

图片

那么在当前情况下可以将div中心化吗?

Myl*_*tos 1

如果您知道要居中的 div 的高度(我假设它是 .text),那么您可以执行以下操作:

.square .text {
    height: 100px;
    top: 50%;
    margin-top: -50px; /* This should be half of height */
}
Run Code Online (Sandbox Code Playgroud)

如果将 div 顶部放置在父容器的 50% 处,所有这些都会完成。margin-top 将其向上推,使中心位于父级的中心。

编辑:显示使用转换的示例:

.square .text{
    border:1px dotted #d6d6d6;
    margin: 0 10px;
    padding: 5px;
    vertical-align: center;
    max-height: 100px;
    overflow: auto;
    position: absolute;
    left: 0;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
}
Run Code Online (Sandbox Code Playgroud)

但这在不支持转换的浏览器上不起作用。请参阅此http://jsfiddle.net/WEQVK/