我有一個頁面,兩個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>
我建議使用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