垂直在“相对”容器中定位块级元素

时间:2014-05-13 12:18:39

标签: html css

我目前正在努力实现这一效果:

Trying to do

我已经非常接近了,这是我到目前为止所拥有的:

JSFIDDLE DEMO

CSS:

.block {
            margin-top: 1em;
            position: relative;
            overflow: auto;
            height: 100%;
            width: 100%;
            float: left;
        }
        .icon {
            background: #000000;
            display: inline-block;
            height: 50px;
            width: 112px;
        }
        .line {
            position: absolute;
            background: #000099;
            width: 100%;
            height: 1px;
            z-index: -1;
            top: 50%;
            left: 0;
            margin-left: -416px;
        }
        .text {
            /*background: #004746;*/
            display: inline-block;
            position: relative;
            vertical-align: middle;
            max-width: 400px;
            float: right;
            border-left: 1px solid #000099;
            padding-left: 1em;
        }
        .text:after {
            content:"";
            background: #000099;
            position: absolute;
            bottom: 0;
            left: 0;
            width: 1em;
            height: 1px;
        }
        .text:before {
            content:"";
            background: #000099;
            position: absolute;
            top: 0;
            left: 0;
            width: 1em;
            height: 1px;
        }
        .text p {
            padding: 0 0 1em 0;
        }
        .text p:last-child {
            margin: 0;
        }

HTML:

<div class="block">
    <div class="icon"></div>
    <div class="line"></div>
    <div class="text">
        <p>Lorem ipsum dolor sit amet, pri eu liber utroque quaestio, ei dicta quaeque sed. Civibus omnesque concludaturque vim eu, ex his nostro quodsi, graecis commune posidonium mei ad. Nam facilis alienum fastidii te, te quando euripidis usu. Torquatos consetetur suscipiantur mel eu, duo cu impedit feugait.</p>

        <p>Vocibus urbanitas suscipiantur pro ut, cu nisl nobis nonumy mel. Posse omnes urbanitas usu in, nusquam invidunt ad sed, mucius recusabo has ea. Aliquip voluptua ius eu, ex vix justo mundi, indoctum scripserit mei cu. Te sit tantas albucius probatus.</p>
    </div>
</div>

我唯一的问题是左侧的框(“图标”),需要与贯穿的线垂直对齐。但是盒子不能绝对位置。

规则:

  1. 必须有回应。
  2. 需要从IE8开始工作。
  3. 需要说明右边的文字是动态的,可以增长或缩小。

1 个答案:

答案 0 :(得分:0)

将此代码添加到.icon类:

.icon {
   position: absolute;
   top: 50%;
   margin-top: -25px;
}