How to center multiple divs inside a container in CSS
I am testing to center divider like the style of windows metro.
.container {
height: 300px;
width: 70%;
background: #EEE;
margin: 10px auto;
position: relative;
}
.block {
background: green;
height: 100px;
width: 100px;
float: left;
margin: 10px;
}
<div class="container">
<div class="block">1. name of the company</div>
<div class="block">2. name of the company</div>
<div class="block">3. name of the company</div>
<div class="block">4. name of the company</div>
<div class="block">5. name of the company</div>
<div class="block">6. name of the company</div>
<div class="block">7. name of the company</div>
<div class="block">8. name of the company</div>
</div>
The grey box is 70% and centering on the screen that is correct, but when I make the window wider and the green dividers are moving, you can see that the green boxes at some point are not centered.
How can help me on this one?
My previous answer showed a frankly outdated method (it still works, there are just better ways to achieve this). For that reason, I'm updating it to include a more modern, flexbox solution.
See support for it here; in most environments, it's safe to use.
This takes advantage of:
-
display: flex
: Display this element with flexbox behaviour -
justify-content: center
Align the inner elements centrally along the main axis of the container -
flex-wrap: wrap
Ensure the inner elements automatically wrap within the container (don't break out of it)
Note: As is usual with HTML & CSS, this is just one of many ways to get the desired result. Research thoroughly before you choose the way that's right for your specifications.
.container {
display: flex;
justify-content: center;
flex-wrap: wrap;
width: 70%;
background: #eee;
margin: 10px auto;
position: relative;
text-align:center;
}
.block {
background: green;
height: 100px;
width: 100px;
margin: 10px;
}
<div class="container">
<div class="block">1. name of the company</div>
<div class="block">2. name of the company</div>
<div class="block">3. name of the company</div>
<div class="block">4. name of the company</div>
<div class="block">5. name of the company</div>
<div class="block">6. name of the company</div>
<div class="block">7. name of the company</div>
<div class="block">8. name of the company</div>
</div>
Original Answer
You could apply the style text-align:center;
to your container and display your .block
s as inline-block elements:
.container {
width: 70%;
background: #eee;
margin: 10px auto;
position: relative;
text-align:center;
}
.block {
background: green;
height: 100px;
width: 100px;
display:inline-block;
margin: 10px;
}
<div class="container">
<div class="block">1. name of the company</div><!--
--><div class="block">2. name of the company</div><!--
--><div class="block">3. name of the company</div><!--
--><div class="block">4. name of the company</div><!--
--><div class="block">5. name of the company</div><!--
--><div class="block">6. name of the company</div><!--
--><div class="block">7. name of the company</div><!--
--><div class="block">8. name of the company</div>
</div>
Here's an updated Fiddle
Notice I've commented out the white-space between your <div>
s. Because the elements are now displayed inline, your spaces will be acknowledged. This is one of many ways to 'fight the space'.
If you change the style on your .block
element so that instead of float:left;
, you use display:inline-block;
, you can then add text-align:center
to the .container
.
.container {
height: 300px;
width: 70%;
background: #EEE;
margin: 10px auto;
position: relative;
text-align: center
}
.block {
background: green;
height: 100px;
width: 100px;
margin: 10px;
display: inline-block;
}
<div class="container">
<div class="block">1. name of the company</div>
<div class="block">2. name of the company</div>
<div class="block">3. name of the company</div>
<div class="block">4. name of the company</div>
<div class="block">5. name of the company</div>
<div class="block">6. name of the company</div>
<div class="block">7. name of the company</div>
<div class="block">8. name of the company</div>
</div>
So basically your CSS needs these changes
.container { text-align:center; }
.block { display:inline-block; *display:inline; zoom:1; vertical-align:top; }
to make the CSS compatible with IE7.
To align the bottom tiles to the left side, some javascript is needed. Due to .querySelector() backward compatibility the following works everywhere including IE8+; for simplification and IE7 compatibility jQuery is highly recommended:
if (!window.addEventListener) {
window.addEventListener = function (type, listener, useCapture) {
attachEvent('on' + type, function () {
listener(event);
});
};
}
window.addEventListener('load', makePaddings, false);
window.addEventListener('resize', makePaddings, false);
function makePaddings() {
var container = document.querySelector('.container');
var blocks = document.querySelectorAll('.block');
var br = [],
max = 0,
i = 0;
var top = blocks[0].getBoundingClientRect().top;
while (blocks[i] && blocks[i].getBoundingClientRect().top == top) {
i++;
}
var show = blocks.length % i ? i - blocks.length % i : 0; /* how many paddings are needed */
var paddings = document.querySelectorAll('.padding');
if (paddings.length < show) { // add missing paddings
i = show - paddings.length;
while (i--) {
var elem = document.createElement('div');
elem.className = 'padding';
elem.style.visibility = 'hidden';
container.appendChild(elem);
}
paddings = document.querySelectorAll('.padding');
}
for (i = 0; i < paddings.length; i++) {
paddings[i].style.display = (i < show) ? 'inline-block' : 'none';
}
}
jsfiddle
Now you can use Flexbox property as base for your layouts. This will allow you more control over the child elements.
.container {
width: 70%;
background: #EEE;
margin: 10px auto;
position: relative;
display:flex;
flex-wrap:wrap;
justify-content:center;
}
.block {
background: green;
height: 100px;
width: 100px;
margin: 10px;
}
<div class="container">
<div class="block">1. name of the company</div>
<div class="block">2. name of the company</div>
<div class="block">3. name of the company</div>
<div class="block">4. name of the company</div>
<div class="block">5. name of the company</div>
<div class="block">6. name of the company</div>
<div class="block">7. name of the company</div>
<div class="block">8. name of the company</div>
</div>
Note: You must need to validate the support and maybe need some vendor prefixes. But as April of 2017 most browsers allow the use.
.container {
background: lightgrey;
height: auto;
width: 70%;
margin: 10px auto;
position: relative;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
}
.block {
background: green;
height: 100px;
width: 100px;
margin: 10px;
}
<div class="container">
<div class="block">1. name of the company</div>
<div class="block">2. name of the company</div>
<div class="block">3. name of the company</div>
<div class="block">4. name of the company</div>
<div class="block">5. name of the company</div>
<div class="block">6. name of the company</div>
<div class="block">7. name of the company</div>
<div class="block">8. name of the company</div>
</div>