In my previous response, I shared an outdated method to achieve a certain layout (although it still works, there are now better ways to accomplish the same result). To stay current, I am updating my answer with a more modern approach using flexbox.
You can check the browser support for flexbox here; in most cases, it is safe to use.
This updated solution makes use of:
display: flex
: Setting elements to display as flexbox items
justify-content: center
: Aligning inner elements centrally along the main axis of the container
flex-wrap: wrap
: Ensuring that inner elements automatically wrap within the container without breaking out
Note: In HTML & CSS, there are multiple ways to achieve the desired outcome. It's essential to research thoroughly and choose the right method based on your specific requirements.
.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
The style text-align:center;
applied to your container and displaying .block
s as inline-block elements was previously recommended:
.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>
For an updated demonstration, you can view this Fiddle.
Please note that I have removed white-space between the <div>
s since they are displayed inline, ensuring proper spacing. This technique is one of several ways to manage space between inline-block elements. You can find more information about this here.