2016-11-17 86 views
1

我有一个页面,两个div并排排列在三分之一到三分之二的位置。这些包含在一个父div中。我的一个div有一个图像,我想要做的是有能力将图像垂直对齐到父div的顶部,中间或底部,其高度由第二个div中的文本数量定义。理想情况下,我希望能够为图片或内容添加一个类,这将设置对齐方式。设置与另一个div内嵌的div内的内容对齐

https://jsfiddle.net/y9mLr5xv/1/

<style type="text/css"> 
    section { 
    padding: 40px 0 40px; 
    display: block; 
    box-sizing: border-box; 
    font-size: 16px; 
    line-height: 1.5; 
    font-family: sans-serif; 
    } 

    .inner { 
    width: 95%; 
    max-width: 1040px; 
    margin-left: auto; 
    margin-right: auto; 
    } 

    .section-body { 
    width: 100%; 
    float: left; 
    margin: 0; 
    } 

    .content-a { 
    width: 32.7%; 
    float: left; 
    } 

    .content-b { 
    width: 62.1%; 
    float: left; 
    margin-left: 4.2%; 
    } 

    h3 { 
    margin: 0; 
    } 
</style> 

<section class="main"> 
    <div class="inner"> 
    <div class="section-header"> 
     <div class="content"> 
     <h1> 
      Header here 
     </h1> 
     <p class="strap"> 
      Strapline would go here 
     </p> 
     </div> 
    </div> 
    <div class="section-body"> 
    <div class="content-a"> 
    <img src="http://placehold.it/350x150"> 
    </div> 
    <div class="content-b"> 
    <h3> 
     Title here 
    </h3> 
    <p> 
     A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. 
    </p> 
    <ul> 
     <li>List item</li> 
     <li>List item</li> 
     <li>List item</li> 
     <li>List item</li> 
     <li>List item</li> 
    </ul> 
    <p> 
     A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. 
    </p> 
    </div> 
</div> 
    </div> 
</section> 
+0

我建议使用Bootstrap3。您可以使用'row'作为标题部分,左侧使用.col-xs-4,右侧使用col-xs-8 +图像使用img-responsive。然后小心使用'flex'和'flex-direction:column'。一般来说,将'flex'与bootstrap混合并不好,但在这种情况下,它应该没问题(你也可以试试Bootstrap4,据说它支持'flex'显示比以前版本更好)。对于顶部/中间/底部对齐,你可以简单地使用flex-start/center/flex-end(就像[here](http://www.w3schools.com/cssref/playit.asp?filename=playcss_align-items&preval=stretch )) – oboer

回答

-1

试试这个,它可以帮助你实现你的目标

.content-a img { 
    width: 45%; 
    float: left; 
} 

.content-b { 
    width: 45%; 
    float: right; 
// margin-left: 4.2%; 
} 
1

也许你可以使用display: table-cell

http://jsfiddle.net/440d1f96/

与HTML:

<div class="area vertical-top"> 
    <img src="http://lorempixel.com/300/150/cats"> 
</div> 

<div class="area"> 
    <p>lorem ipsum...</p> 
</div> 

和CSS:

.area { 
    background: red; 
    display:table-cell; 
    vertical-align: top; 
} 

.vertical-bottom { 
     vertical-align: bottom; 
} 

.vertical-middle { 
     vertical-align: middle; 
} 
+0

看起来不错。使用'display:flex'找到另一个解决方案。思考? –

+0

取决于您需要的浏览器支持:http://caniuse.com/#feat=flexbox –

1

实测值使用溶液display: flex

https://jsfiddle.net/johnthomson92/y9mLr5xv/2/

HTML:

<section class="main"> 
    <div class="inner"> 
    <div class="section-header"> 
     <div class="content"> 
     <h1> 
      Header here 
     </h1> 
     <p class="strap"> 
      Strapline would go here 
     </p> 
     </div> 
    </div> 
    <div class="section-body"> 
     <div class="content-a top"> 
     <img src="http://placehold.it/350x150"> 
     </div> 
     <div class="content-b"> 
     <h3> 
      Title here 
     </h3> 
     <p> 
      A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. 
     </p> 
     <ul> 
      <li>List item</li> 
      <li>List item</li> 
      <li>List item</li> 
      <li>List item</li> 
      <li>List item</li> 
     </ul> 
     <p> 
      A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. A bit of text here. Not sure what it would say but it be here. 
     </p> 
     </div> 
    </div> 
    </div> 
</section> 

CSS:

section { 
    padding: 40px 0 40px; 
    display: block; 
    box-sizing: border-box; 
    font-size: 16px; 
    line-height: 1.5; 
    font-family: sans-serif; 
    overflow: hidden; 
} 

.inner { 
    width: 95%; 
    max-width: 1040px; 
    margin-left: auto; 
    margin-right: auto; 
} 

.section-body { 
    width: 100%; 
    float: left; 
    margin: 0; 
    display: flex; 
    position: relative; 
} 

.strap { 
    margin-bottom: 20px; 
} 

.content-a { 
    width: 32.7%; 
    float: left; 
    display: flex; 
    align-items: flex-start; 
} 

.top { 
    align-items: flex-start; 
} 

.middle { 
    align-items: center; 
} 

.bottom { 
    align-items: flex-end; 
} 

.content-b { 
    width: 62.1%; 
    float: left; 
    margin-left: 4.2%; 
} 

p { 
    margin-bottom: 0; 
} 

h3 { 
    margin: 0; 
} 

img { 
    max-width: 100%; 
    display: block; 
}