How can I center text vertically and horizontally in responsive (bootstrap) container? I've been playing around with various table and table-cell display methods, but cannot get anything to work correctly.
Here is the html, I'm trying to center the contents of the span tag over the image.
- I need the image to control the size of the container's height, without it the container just collapses to the height of the line.
- I cannot set a static height on the div>div>div either since it scales with the browser size/view.
HTML
<div class="row media-thumbs">
<div class="col-sm-4 col-xs-12">
<div>
<span>Some copy should go here</span>
<a href="#">
<img class="img-responsive" src="default-tile.gif">
</a>
</div>
</div>
</div>
WORKING CODE
With Alexander's help/clues, I modified the plugin ( https://github.com/PaulSpr/jQuery-Flex-Vertical-Center )to get the correct width and height
(function( $ ){
$.fn.flexVerticalCenter = function( onAttribute ) {
return this.each(function(){
var $this = $(this); // store the object
//var attribute = onAttribute || 'margin-top'; // the attribute to put the calculated value on
//alert($this.css('padding-top'));
// recalculate the distance to the top of the element to keep it centered
var resizer = function () {
// get parent height minus own height and devide by 2
$this.css({
'margin-top' : ((( $this.parent().height() - $this.height() ) / 2) - parseInt($this.css('padding-top')) ),
'width' : ($this.parent().width())
});
};
// Call once to set.
resizer();
// Call on resize. Opera debounces their resize by default.
$(window).resize(resizer);
// Apply a load event to images within the element so it fires again after an image is loaded
$this.find('img').load(resizer);
});
};
})( jQuery );