Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend Twitter Bootstrap image carousel for dynamically sized, centered images

I'm trying to extend the default Bootstrap image carousel to support dynamically sized images (500x400 max), centered both horizontally and vertically. Additionally, I'd like to maintain the original caption layout, which anchors the caption to the bottom of the image, with the caption div extending fully across the image (but no further.)

I've put together a fiddle, which is a fairly clean implementation of the default Bootstrap setup (there are only 4 additional styles at the end of the css section):

http://jsfiddle.net/rdugan/JFBFU/26/

I can fairly easily accomplish the horizontal centering and caption requirements by adding a surrounding 'inline' div around the image and caption, and using 'text-align: center' on the parent. However, the vertical centering remains a problem (as always.)

As an alternative, I've also tried using 'display: table-cell' (and the accompanying centering styles) on different divs with varying results - in some cases I mess up the carousel functionality, while in others I accomplish the image centering, but lose the caption anchoring.

Any hints would be greatly appreciated - been banging my head on this one for quite some time.

like image 242
Ryan Dugan Avatar asked Apr 06 '12 01:04

Ryan Dugan


People also ask

How do I get my carousel picture to fit?

1 method is to use a div with a specified size, use css to set the image as a background image for the div and set background-size to fit, contain or cover. 2 A better approach is to use an image manipulation program like photoshop, i personally use an online application called https://www.photopea.com.

How do I stop bootstrap carousel from sliding?

Use the [interval]="'0'" input. This will disable the auto-sliding feature. Here's the link to the Carousel Documentation.


1 Answers

You can center horizontally with these rules:

.carousel-inner { text-align: center; }

.carousel .item > img { display: inline-block; }

For vertical aligning you should check out this: http://www.student.oulu.fi/~laurirai/www/css/middle/

And for keeping the image aspect ratio on resizing you just have to change only the width or only the height of the image not both at a time and it will resize with its original ratio.

like image 131
laszlo-horvath Avatar answered Nov 15 '22 19:11

laszlo-horvath