Both the image and .whitebox
are inline-level elements within the same line box.
As a result, their vertical alignment is determined by the vertical-align
property:
This property controls how inline-level boxes are positioned vertically inside a line box.
By default, the value is set to baseline
:
Align the baseline of the box with its parent's baseline or bottom margin edge if no baseline exists.
Since the image lacks a baseline, its bottom margin edge will align with the baseline of .whitebox
. This baseline is calculated based on
The baseline of an 'inline-block' element is typically the last line box's baseline in normal flow, unless certain conditions apply.
Therefore, you can
Adjust the vertical alignment of the image and .whitebox
, for example:
img, .whitebox {
vertical-align: middle;
}
body {
background-color: #F2F2F2;
}
h3 {
font-family: sans-serif;
text-align: center;
color: #535353;
}
.forR {
width: 980px;
padding-left: 20px;
margin-left: auto;
margin-right: auto;
margin-bottom: 25px;
padding-left: 10px;
}
.inline {
display: inline;
position: relative;
}
.whitebox {
background-color: #fff;
height: 200px;
display: inline-block;
}
.box1 {
width: 737px;
}
img {
height: 200px;
width: 200px;
margin-right: 10px;
display: inline-block;
}
img, .whitebox {
vertical-align: middle;
}
<h3>Name</h3>
<div class="forR">
<img src="http://example.com/image.jpg">
<div class="whitebox box1">
<p class="inline">Name: John</p>
<p class="inline"></p>
<p class="inline"></p>
<p class="inline"></p>
<p class="inline"></p>
<p class="inline"></p>
</div>
</div>
Ensure that .whitebox
has no in-flow line boxes so that its baseline becomes its bottom margin edge. Essentially, the content should be considered out of flow:
An element is out of flow when floated, absolutely positioned, or is the root element. In-flow elements do not meet these criteria.
You could use float: left
as shown below:
.whitebox > * {
float: left;
}
body {
background-color: #F2F2F2;
}
h3 {
font-family: sans-serif;
text-align: center;
color: #535353;
}
.forR {
width: 980px;
padding-left: 20px;
margin-left: auto;
margin-right: auto;
margin-bottom: 25px;
padding-left: 10px;
}
.inline {
display: inline;
position: relative;
}
.whitebox {
background-color: #fff;
height: 200px;
display: inline-block;
}
.box1 {
width: 737px;
}
img {
height: 200px;
width: 200px;
margin-right: 10px;
display: inline-block;
}
.whitebox > * {
float: left;
}
<h3>Name</h3>
<div class="forR">
<img src="http://example.com/image.jpg">
<div class="whitebox box1">
<p class="inline">Name: John</p>
<p class="inline"></p>
<p class="inline"></p>
<p class="inline"></p>
<p class="inline"></p>
<p class="inline"></p>
</div>
</div>
Modify the overflow
property of .whitebox
from visible
to another value to ensure the baseline aligns with the bottom margin edge:
For instance, set it to overflow: hidden
:
.whitebox {
overflow: hidden;
}
body {
background-color: #F2F2F2;
}
h3 {
font-family: sans-serif;
text-align: center;
color: #535353;
}
.forR {
width: 980px;
padding-left: 20px;
margin-left: auto;
margin-right: auto;
margin-bottom: 25px;
padding-left: 10px;
}
.inline {
display: inline;
position: relative;
}
.whitebox {
background-color: #fff;
height: 200px;
display: inline-block;
}
.box1 {
width: 737px;
}
img {
height: 200px;
width: 200px;
margin-right: 10px;
display: inline-block;
}
.whitebox {
overflow: hidden;
}
<h3>Name</h3>
<div class="forR">
<img src="http://example.com/image.jpg">
<div class="whitebox box1">
<p class="inline">Name: John</p>
<p class="inline"></p>
<p class="inline"></p>
<p class="inline"></p>
<p class="inline"></p>
<p class="inline"></p>
</div>
</div>