将div放在容器div中

时间:2015-02-17 18:10:06

标签: html css center margin

我有一个装有9个div的容器div。我遇到的问题是将这9个div放在容器div中。我尝试过使用margin: 0 auto;,但无济于事。

对此有任何帮助将不胜感激。

的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;
}

谢谢。

2 个答案:

答案 0 :(得分:0)

包装所有子div,如下所示:

#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;
}
.holder {
  width: 80%;
  height: 80%;
  margin: 0 auto;
}
<div id="container">
  <div class="holder">
    <!-- 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>
</div>

希望它有所帮助。

答案 1 :(得分:0)

margin: auto不会将内容置于元素内部,它(有时)会将元素集中在一个元素中。如果您想将div置于容器内,请将float: left上的.square替换为display: inline-block,然后将text-align: center应用于容器。

#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>