I have 3 images vertically aligned in fixed height div. How can I make sure that the top and bottom padding between them remains even when an image is added or deleted.
I know I'm bound to stumble across this again the next time I search for it. Leaving this as a reference for myself. Difference is elements don't have to have fixed heights per-se:
http://codepen.io/anon/pen/ojBPaq?editors=110
HTML
<ul>
<li><div class="tile-wrap one"><div class="tile">wefwfwefww efwefwfwfewfwef wefwfwf wefwfe wefwf wefwfwfwfwf wef wefwfe</div></div></li>
<li><div class="tile-wrap two"><div class="tile"></div></div></li>
<li><div class="tile-wrap three"><div class="tile"></div></div></li>
</ul>
CSS
* { box-sizing: border-box; }
ul {
list-style: none;
margin: 0;
padding: 0;
background: grey;
width: 500px;
height: 400px;
display: table;
}
li {
display: table-row;
background: aqua;
vertical-align: bottom;
}
.tile-wrap {
display: table-cell;
vertical-align: middle;
}
.one {
vertical-align: top;
}
.two {
vertical-align: middle;
}
.three {
vertical-align: bottom;
}
.tile-wrap .tile {
width: 100px;
min-height: 100px;
background: white;
border: 1px solid blue;
}
You want to:
display:table
with a fixed height,<img>
in element with display:table-row
and display:table-cell
display:block
vertical-align:middle
This will cause the space to be evenly distributed vertically.
Code:
<ul id="img-list">
<li><span><img src="http://phrogz.net/tmp/gkhead.jpg"></span></li>
<li><span><img src="http://phrogz.net/tmp/gkhead.jpg"></span></li>
<li><span><img src="http://phrogz.net/tmp/gkhead.jpg"></span></li>
</ul>
#img-list { display:table; height:100px }
#img-list img { height:20px; display:block }
#img-list li { display:table-row }
#img-list li span { display:table-cell; vertical-align:middle; background:red }
#img-list li:first-child,
#img-list li:last-child { height:20px }