I am using Bootstrap to display a random tweet and a static image alongside it.
It looks great, but the text is always vertically at the top, instead of center of the image.
How do I resolve this so no matter the length of the tweet, it’ll display in the middle vertically?
Demo: https://jsfiddle.net/9wwuznpL/
/* Latest compiled and minified CSS included as External Resource*/
/* Optional theme */
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
background:black;
margin: 10px;
color:white
}
img {
float:left
}
/*========== Non-Mobile First Method ==========*/
/* Large Devices, Wide Screens */
@media only screen and (max-width : 1200px) {
}
/* Medium Devices, Desktops */
@media only screen and (max-width : 992px) {
}
/* Small Devices, Tablets */
@media only screen and (max-width : 768px) {
img {
float:none
}
}
/* Extra Small Devices, Phones */
@media only screen and (max-width : 480px) {
}
/* Custom, iPhone Retina */
@media only screen and (max-width : 320px) {
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col-md-6 text-center col-md-push-3">
<div class="tweet">
<!--<i class="fa fa-twitter fa-5" aria-hidden="true" style="font-size: 4em;color:white"></i>-->
<img src="http://placehold.it/100x100">
<blockquote>
<p>
RT <a href="http://twitter.com/thetomzone">@thetomzone</a> : Seriously, drop everything you're...
</p>
</blockquote>
</div>
</div>
</div>
4
Answers
Give
JSFIDDLE
I would say the easiest way is to set some css like this:
Or you could use
display: flex;
if the browser support is enough: http://caniuse.com/#search=flexDemo: https://jsfiddle.net/9wwuznpL/4/
You need to change the default display of both elements, image and blockquote, to inline-block. Then you can use vertical-align css property and set its value to middle. You should set a width or max-width to the blockquote element, because if you don’t do this the blockquote could place itself below the image.
I modified your fiddle: https://jsfiddle.net/9wwuznpL/1/
As you could see you can use calc for the quote width value, e.g. if the image is 100px wide the quote must be calc(100% – 110px). You must know that inline-block elements work as typography, so an empty space will work as a nbsp; so you should add around 4 extra pixels. In my example I added 10 more pixels, but with only 4 this should work properly.
You can achieve your effect in a few different ways: Flexbox, absolute positioning with transformations, and
display: table
/table-cell
withvertical-align
. Since the other answers have already covered the other types, here’s the Flexbox version: