Search code examples
htmlcsscentermargin

Centering divs in a container div


I have a container div that houses 9 divs. The problem I am having is with centring these 9 divs in the container div. I have tried using margin: 0 auto; but to no avail.

Any help with this would be appreciated.

index.html

<div id="container">

     <!-- 1st row of images in menu -->

    <div class="square imgcentre"><img id="bbqChickenBurger" src="images/bbqChickenBurger.jpg" width="260" height="212" alt="BBQ Chicken Burger and Chips" /></div>
    <div class="square imgcentre"><img id="vegePizza" src="images/vegePizza.jpg" width="260" height="212" alt="Vegetarian Pizza" /></div>
    <div class="square imgcentre"><img id="parmaHamBaguette" src="images/parmaHamBaguette.jpg" width="260" height="212" alt="Parma Ham Baguette" /></div>

    <!-- 2nd row of images in menu -->

    <div class="square imgcentre"><img id="spaghettiBolognese" src="images/spaghettiBolognese.jpg" width="260" height="212" alt="Spaghetti Bolognese" /></div>
    <div class="square imgcentre"><img id="chiliCottageCheeseWrap" src="images/chiliCottageCheeseWrap.jpg" width="260" height="212" alt="Chili Cottage Cheese Wrap" /></div>
    <div class="square imgcentre"><img id="chickenSalad" src="images/chickenSalad.jpg" width="260" height="212" alt="Chicken Salad" /></div>

    <!-- 3rd row of images in menu -->

    <div class="square imgcentre"><img id="brownieBite" src="images/brownieBite.jpg" width="260" height="212" alt="Brownie Bite with Vanilla Ice Cream" /></div>
    <div class="square imgcentre"><img id="strawberrySundae" src="images/strawberrySundae.jpg" width="260" height="212" alt="Strawberry Sundae" /></div>
    <div class="square imgcentre"><img id="cheesecake" src="images/cheesecake.jpg" width="260" height="212" alt="Cheesecake" /></div>

    </div>

style.css

#container{
    width: 1200px;
    margin: 0 auto;
    height: 790px;
    border-bottom:solid 2px #d8d8d8;
}

.square {
    float:left;
    position: relative;
    width:30%;
    padding-bottom :17px; 
    margin:1.66%;
    background-position:center center;
    background-repeat:no-repeat;
    background-size:contain;
    border:solid 2px;

}

.imgcentre{
    text-align: center;
    margin-right: 5px;
    margin-bottom: 5px;
    margin-left: 5px;
}

Thank you.


Solution

  • margin: auto doesn't centre content inside an element, it (sometimes) centres the element itsef. If you want to centre divs inside the container, replace float: left on .square by display: inline-block and then apply text-align: center to the container.

    #container{
        width: 1200px;
        margin: 0 auto;
        height: 790px;
        border-bottom: solid 2px #d8d8d8;
        text-align: center
    }
    
    .square {
        position: relative;
        width:30%;
        padding-bottom :17px; 
        margin:1.66%;
        background-position:center center;
        background-repeat:no-repeat;
        background-size:contain;
        border:solid 2px;
        display: inline-block
    
    }
    
    .imgcentre{
        text-align: center;
        margin-right: 5px;
        margin-bottom: 5px;
        margin-left: 5px;
    }
    <div id="container">
    
         <!-- 1st row of images in menu -->
    
        <div class="square imgcentre"><img id="bbqChickenBurger" src="images/bbqChickenBurger.jpg" width="260" height="212" alt="BBQ Chicken Burger and Chips" /></div>
        <div class="square imgcentre"><img id="vegePizza" src="images/vegePizza.jpg" width="260" height="212" alt="Vegetarian Pizza" /></div>
        <div class="square imgcentre"><img id="parmaHamBaguette" src="images/parmaHamBaguette.jpg" width="260" height="212" alt="Parma Ham Baguette" /></div>
    
        <!-- 2nd row of images in menu -->
    
        <div class="square imgcentre"><img id="spaghettiBolognese" src="images/spaghettiBolognese.jpg" width="260" height="212" alt="Spaghetti Bolognese" /></div>
        <div class="square imgcentre"><img id="chiliCottageCheeseWrap" src="images/chiliCottageCheeseWrap.jpg" width="260" height="212" alt="Chili Cottage Cheese Wrap" /></div>
        <div class="square imgcentre"><img id="chickenSalad" src="images/chickenSalad.jpg" width="260" height="212" alt="Chicken Salad" /></div>
    
        <!-- 3rd row of images in menu -->
    
        <div class="square imgcentre"><img id="brownieBite" src="images/brownieBite.jpg" width="260" height="212" alt="Brownie Bite with Vanilla Ice Cream" /></div>
        <div class="square imgcentre"><img id="strawberrySundae" src="images/strawberrySundae.jpg" width="260" height="212" alt="Strawberry Sundae" /></div>
        <div class="square imgcentre"><img id="cheesecake" src="images/cheesecake.jpg" width="260" height="212" alt="Cheesecake" /></div>
    
        </div>