如何在bootstrap中水平居中两行

时间:2016-08-10 04:01:52

标签: html css

我目前正在设计一个网站,其中包含两行,每行包含3列。由于某种原因,我的所有内容都被推到了左边。我在代码中做错了吗?

以下是我正在使用的内容

<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title> Delightful Days Daycare</title>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <script src="https://use.fontawesome.com/b1366dc4f6.js"></script>
        <link rel="stylesheet" type="text/css" href="style.css">
        <!-- Header -->
    </head>
    <body>
        <div>
            <div class="background-box">
                <div class="row">
                    <div class="box-1 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button1"><a class="box-text" href="about-us.html">ABOUT US</button></a></h2><img src="img/info.png" class="about-img">Learn more about Mrs. Chris and Delightful Days </div>
                    <div class="box-2 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button2"><a class="box-text" href="photos.html">PHOTOS</a></button></h3><img src="img/photos.png" class="photos-img">View our photo gallery</div>
                    <div class="box-3 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button3"><a class="box-text" href="calendar.html">CALENDAR</a></button></h3><img src="img/calendar.png" class="cal-img">Stay up to date with our official calendar to view holidays and closed days</div>
                </div>
                <div class="row">
                    <div class="box-4 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button4"><a class="box-text" href="contact-us.html">CONTACT US</a></button></h3>
                        <img src="img/phone.png" class="phone-img">Need to get in touch? View our contact page for more information </div>
                    <div class="box-5 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button5"><a class="box-text" href="openings.html">OPENINGS</a></h2></button>
                        <img src="img/open.png" class="open-img">View available openings </div>
                    <div class="box-6 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button6"><a class="box-text" href="pricing.html">PRICING</a></button></h3>
                        <img src="img/money.png" class="pricing-img">View our pricing page for more information</div>
                </div>
            </div>
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <!-- Latest compiled and minified JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
    </body>
</html>

我的CSS:

.background-box{
    background-color: #4c4c4c;
    height: 1000.5px;
    color: white !important;
    font-size: 20px;
    text-align: center;
    position: auto;
    display: block;

}


.box-1{
    background-color: #FF3D7F;
    width: 330px;
    height: 394px;
    border-radius: 9px;
    /*margin-left: 181px;*/
    margin-top: 50px;
    margin: 2%;
}

.button{
    color: white;
    border:none;
    color: white;
    border-radius: 100px;
    height: 48px;
    width: 178px
}

.button1{
    background-color: #DB1759;
}

.button2{
    background-color: #C4C172;
}
.button3{
    background-color: #58A58B;
}
.button4{
    background-color: #B58C43;
}
.button5{
    background-color: #26978F;
}
.button6{
    background-color: #FF6362
}
.box-2{
    background-color: #DAD8A7;
    width: 330px;
    height: 394px;
    border-radius: 9px;
    /*margin-left: 44px;*/
    margin-top: 50px;
    margin: 2%;
}

.box-3{
    background-color: #7FC7AF;
    width: 330px;
    height: 394px;
    border-radius: 9px;
    /*margin-left: 44px;*/
    margin-top: 50px;
    margin: 2%;
}

.box-4{
    background-color: #D7B066;
    width: 330px;
    height: 394px;
    border-radius: 9px;
    /*margin-left: 181px;*/
    margin-top: 46px;
    margin: 2%;
}

.box-5{
    background-color: #3FB8AF;
    width: 330px;
    height: 394px;
    border-radius: 9px;
    /*margin-left: 44px;*/
    margin-top: 46px;
    margin: 2%;
}


.box-6{
    background-color: #FF9E9D;
    width: 330px;
    height: 394px;
    border-radius: 9px;

    /*margin-left: 44px;*/
    margin-top: 46px;
    margin: 2%;
}
.box-text{
    color: white;
       font-family: "Source Sans Pro";
   font-weight: 600;
   font-style: normal;
   font-size: 26px;
}

4 个答案:

答案 0 :(得分:2)

如果您希望容器占据页面的宽度,则需要将行包装在容器或容器流体中。

<div>
    <div class="background-box container">
            <div class="row">
                <div class="box-1 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button1"><a class="box-text" href="about-us.html">ABOUT US</button></a></h2><img src="img/info.png" class="about-img">Learn more about Mrs. Chris and Delightful Days </div>
                <div class="box-2 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button2"><a class="box-text" href="photos.html">PHOTOS</a></button></h3><img src="img/photos.png" class="photos-img">View our photo gallery</div>
                <div class="box-3 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button3"><a class="box-text" href="calendar.html">CALENDAR</a></button></h3><img src="img/calendar.png" class="cal-img">Stay up to date with our official calendar to view holidays and closed days</div>
            </div>
            <div class="row">
                <div class="box-4 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button4"><a class="box-text" href="contact-us.html">CONTACT US</a></button></h3>
                    <img src="img/phone.png" class="phone-img">Need to get in touch? View our contact page for more information </div>
                <div class="box-5 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button5"><a class="box-text" href="openings.html">OPENINGS</a></h2></button>
                    <img src="img/open.png" class="open-img">View available openings </div>
                <div class="box-6 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button6"><a class="box-text" href="pricing.html">PRICING</a></button></h3>
                    <img src="img/money.png" class="pricing-img">View our pricing page for more information</div>
            </div>
    </div>
</div>

<强> Bootply
http://www.bootply.com/K4bQ0BrbiK

使用容器流体

<style>
/*the wrapper class*/
    .wrapper {
        margin: 0 auto;
        width: 60%;
    }
/*change the width as required*/
</style>

<div>
    <div class="background-box container-fluid">
  <div class="wrapper">
            <div class="row text-center">
                <div class="box-1 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button1"><a class="box-text" href="about-us.html">ABOUT US</a></button></h2><img src="img/info.png" class="about-img">Learn more about Mrs. Chris and Delightful Days </div>
                <div class="box-2 col-sm-4  col-md-4 col-lg-4"><h2><button class="button button2"><a class="box-text" href="photos.html">PHOTOS</a></button></h2><img src="img/photos.png" class="photos-img">View our photo gallery</div>
                <div class="box-3 col-sm-4  col-md-4 col-lg-4"><h2><button class="button button3"><a class="box-text" href="calendar.html">CALENDAR</a></button></h2><img src="img/calendar.png" class="cal-img">Stay up to date with our official calendar to view holidays and closed days</div>
            </div>
      </div>
      <div class="wrapper">
            <div class="row text-center">
                <div class="box-4 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button4"><a class="box-text" href="contact-us.html">CONTACT US</a></button></h2>
                    <img src="img/phone.png" class="phone-img">Need to get in touch? View our contact page for more information </div>
                <div class="box-5 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button5"><a class="box-text" href="openings.html">OPENINGS</a></button></h2>
                    <img src="img/open.png" class="open-img">View available openings </div>
                <div class="box-6 col-sm-4 col-md-4 col-lg-4"><h2><button class="button button6"><a class="box-text" href="pricing.html">PRICING</a></button></h2>
                    <img src="img/money.png" class="pricing-img">View our pricing page for more information</div>
            </div>
      </div>
    </div>
</div>

<强> Bootply
http://www.bootply.com/VNYm7x5XOI

答案 1 :(得分:1)

再添加一个

<强> CSS

.Row-centered
 { 
   margin: auto; 
   max-width: 300px;
}

<强> HTML

/* Add Class with in Row  */
<div class="row Row-centered">

Live Demo

答案 2 :(得分:0)

您可以使用此处提到的列偏移 string refs are being deprecated

答案 3 :(得分:0)

您可以为该元素提供保证金。

.background-box{

margin-left:40px;
margin-right:40px;
margin-top:auto;
margin-bottom:auto;

 }

您也可以使用填充。