You could use an extra element and vertical-padding
to force your div to keep the same ratio that it has a 2:1 image or not.
DEMO and basic css:
.image:before {
content:'';
display:inline-block;
vertical-align:middle;
padding-top:50%;/* equals 50% of width of parent */
width:0;
box-shadow:0 0 0 5px red;/* let's see where it stands for demo purpose */
}
In order to have this working in your codepen:
img
should turn back to their default display
(inline-block
), so just remove display:block;
and be vertical-alligned in middle
to the pseudo element , the gap under img
that appears when on baseline
, will be no longer here.
.image
needs either:
- In CSS
font-size:0;
- In HTML, the code
<div><img src="..
should not be indented
- In HTML white-space should be commented
<div><!-- code indented --><img src="...
to avoid extra white-space
and break in 2 lines when img
is full width
.
I did link in the demo another version where image could be bigger than initial space wanted without breaking the layout (base on idea that elements remain in the flow, no absolute
positionning involved): EXTRA