使用“absolute”定位的父元素,能否将子元素div居中?

8
<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>

image

在当前情况下,是否有可能使div居中?


哪个 div?就我所看到的,它是居中的 @kirix - samayo
你是在尝试将文本居中放置在虚线边框内吗,@kirix? - samayo
我假设他想要像左边图片所示垂直居中 div.text,尽管这样看起来很糟糕。我不确定这是否可能。也许你应该查看 vertical-align:middle; - danijar
@TheCOMPLETEPHPNewbie 我需要在父元素中居中点。 - Kin
@sharethis 是的,你说得对,所以问题开始于“是否可能……” - Kin
3个回答

2

虽然有点晚,但这是我的答案......诀窍是在我们的文本容器div旁边有一个帮助div,如下所示的代码......希望这可以帮助你 :D

<div style=" position: absolute;
             display: inline-block;
             top: 20%;
             bottom: 20%;
             right: 20%;
             left: 20%;
             background-color: #434154;
             text-align: center;">
    <div style="display: inline-block;
                height: 100%;
                vertical-align: middle;"></div>
    <div style="position: relative;
                color: #FFFFFF;
                background-color: #546354;
                display: inline-block;
                vertical-align: middle;">
               THIS IS CENTERED WITHOUT SCRIPTING :D
    </div>
</div>

2
这是一段关于CSS的翻译:

尽管需要进行一些在IE6 / 7中无法工作的有趣更改,但仅使用CSS完全可以实现。

如果将父容器设置为display: table-cellvertical-align: middle,并将子元素设置为display: inline-block,则会获得类似表格的效果,其中内容位于中间。

自己看看吧!


1
如果您知道要居中的 div 的高度(我假设它是 .text),那么可以这样做:
.square .text {
    height: 100px;
    top: 50%;
    margin-top: -50px; /* This should be half of height */
}

这个操作会将div的顶部放置在父容器的50%处。margin-top属性将其向上推,使其居中于父容器。

编辑:使用transforms属性展示示例:

.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%);
}

这将无法在不支持transforms的浏览器上工作。请查看此http://jsfiddle.net/WEQVK/


我们可以假设高度并非如问题中的右图所预期那样固定。 - danijar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接