I have a bunch of inline-block elements over several lines which I'd like to center horizontally. The inline-block elements all have the same fixed size, but I'd like the centering to be able to handle page resizing and adding or removing elements.
I've stripped down the html/css and removed the attempt at centering for clarity. It's at http://jsfiddle.net/fe25H/1/
If you resize the results window so that the third inline-block element drops down, the container fills the width and we get this:
-----------------BODY------------------
| |
||-------------CONTAINER-------------||
||-INLINEBLOCK---INLINEBLOCK-- ||
|||____________||____________| ||
||-INLINEBLOCK-- ||
|||____________| ||
||___________________________________||
|_____________________________________|
rather than this:
-----------------BODY------------------
| |
| |----------CONTAINER---------| |
| |-INLINEBLOCK---INLINEBLOCK--| |
| ||____________||____________|| |
| |-INLINEBLOCK-- | |
| ||____________| | |
| |____________________________| |
|_____________________________________|
edit based on ptriek's answer regarding a JavaScript solution:
Ptriek's code was a useful starting point; it works for the specific case, but not the general one. I've mostly rewritten it to be more flexible (see http://jsfiddle.net/fe25H/5/).
Inline block divs can be centered by applying text-align:center to their parent.
An inline element does not start on a new line. An inline element only takes up as much width as necessary. This is a <span> element inside a paragraph.
Inline-block elements are similar to inline elements, except they can have padding and margins added on all four sides. You'll have to declare display: inline-block in your CSS code. One common use for using inline-block is for creating navigation links horizontally, as shown below.
Compared to display: inline , the major difference is that display: inline-block allows to set a width and height on the element. Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not.
After thinking a bit about it, I agree with Wex' comment above.
So I fiddled a JavaScript solution (jQuery) - I'm not an expert on this, so the code might be improved - but I guess it does exactly what you need:
var resizeContainer = function () {
var w_window = $(window).width();
var w_block = $('.inlineblock').width();
if (w_window < w_block * 3 && w_window >= w_block * 2) {
$('.container').width(w_block * 2);
} else if (w_window < w_block * 2) {
$('.container').width(w_block);
} else {
$('.container').width(w_block * 3);
}
};
$(document).ready(resizeContainer);
$(window).resize(resizeContainer);
body {
text-align:center;
}
.container {
display: inline-block;
background-color: #aaa;
text-align:left;
}
.inlineblock {
display: inline-block;
width: 200px;
height: 200px;
background-color: #eee;
}
<div class='container'>
<div class='inlineblock'></div>
<div class='inlineblock'></div>
<div class='inlineblock'></div>
</div>
http://jsfiddle.net/ptriek/fe25H/4/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With