简体   繁体   English

在相对容器中垂直放置块级元素

[英]Vertically Positioning a block level element in a Relative container

I am currently trying to achieve this effect: 我目前正在尝试实现此效果:

尝试做

and I have gotten very close, Here's what I have so far: 而且我已经很接近了,这是我到目前为止的结果:

JSFIDDLE DEMO JSFIDDLE演示

CSS: 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: 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>

My only problem is the box on the left ("icon"), needs to vertically align with the line that runs through. 我唯一的问题是左侧的框(“图标”),需要与穿过的线垂直对齐。 But the box can not be position absolute. 但是盒子不能是绝对位置。

RULES: 规则:

  1. Has to be responsive. 必须响应。
  2. Needs to work from IE8 up. 需要从IE8开始工作。
  3. Needs to account for the fact that the text on the right is dynamic and could grow or shrink. 需要考虑到以下事实:右侧的文本是动态的,并且可能会增长或缩小。

add this code to your .icon class: 将此代码添加到您的.icon类中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM