Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center a group of div's in a div

Tags:

html

centering

Is it possible to center a group of divs in one div so it'll look like this?

http://oi49.tinypic.com/1yo2dh.jpg

I wonder if you can do it without using a table.

Right now I got this HTML:

<nav class="imagemenu">
    <div id="categories">
        <div class="cata" onClick="#" style="cursor:pointer;"> <img src="image/placeholder.jpg" /> </div>
        <div class="cata" onClick="#" style="cursor:pointer;"> <img src="image/placeholder.jpg" /> </div>
        <div class="cata" onClick="#" style="cursor:pointer;"> <img src="image/placeholder.jpg" /> </div>
        <div class="cata" onClick="#" style="cursor:pointer;"> <img src="image/placeholder.jpg" /> </div>    
        <div class="cata" onClick="#" style="cursor:pointer;"> <img src="image/placeholder.jpg" /> </div>
        <div class="cata" onClick="#" style="cursor:pointer;"> <img src="image/placeholder.jpg" /> </div>
        <div class="cata" onClick="#" style="cursor:pointer;"> <img src="image/placeholder.jpg" /> </div>
        <div class="cata" onClick="#" style="cursor:pointer;"> <img src="image/placeholder.jpg" /> </div>         
        <div class="cata" onClick="#" style="cursor:pointer;"> <img src="image/placeholder.jpg" /> </div>
        <div class="cata" onClick="#" style="cursor:pointer;"> <img src="image/placeholder.jpg" /> </div>
    </div>
</nav>  

And this CSS:

.home-menu {
    width:780px;
    height: 340px;
    margin-top:10px;
    padding: 20px;
    background-color:#CCC;
}

#categories {
    width:740px;
    height:340px;
    margin:0 auto;
    background-color:#333;
}

.cata {
    width:150px;
    height:100px;
    margin-bottom: 20px;
    float:left;
    background-color:#FFF;
    opacity:0.5;
}
.cata {
    opacity:1.0;
}

Sizes are still depending but is there a possible option to easily center all the cata's in the categories div?

I tried some options like overflow and text-align:center from other related questions, and perhaps I used them wrong but they don't work.

like image 409
RChellz Avatar asked Dec 04 '22 12:12

RChellz


1 Answers

You can add text-align:

#categories {
    […]
    text-align:center;
}

and display: inline-block and you have to remove the float from this CSS rule:

.cata{
    […]
    display:inline-block;
}

Demo

http://jsfiddle.net/EdHS9/

like image 193
insertusernamehere Avatar answered Jan 09 '23 09:01

insertusernamehere