Bootstrap主页旋转木马对齐垂直中心

时间:2014-09-08 02:57:16

标签: twitter-bootstrap carousel

我正在尝试在Bootstrap网站的主页上制作旋转木马。我希望它位于页面的中心(垂直和水平),并且负责,因此无论用户使用什么尺寸的屏幕,它始终保持在页面的中心。我可以使它水平居中,但垂直。除了全屏背景图像之外,旋转木马是主页上唯一的东西。这是迄今为止的相关代码。

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Esther Arts</title>

        <!-- Bootstrap -->
        <link href="css/bootstrap.min.css" rel="stylesheet">
        <script src="/js/jquery-2.1.1.min.js"></script>
        <script src="/js/bootstrap.min.js"></script>
        <!-- customized CSS -->
        <link href="css/styles.css" rel="stylesheet">
        <!-- Font Awesome -->
        <link rel="stylesheet" href="font_awesome_4.2.0/css/font-awesome.min.css">

        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
          <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
          <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript" ></script>
        <script type="text/javascript">
            $(function() {
                var images = ['bg1.jpg', 'bg2.jpg', 'bg3.jpg', 'bg4.jpg', 'bg5.jpg', 'bg6.jpg'];
                $('#homepage').css(
                    {'background-image': 'url(img/' + images[Math.floor(Math.random() * images.length)] + ')'}
                );   
            });
        </script>
        <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
        <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css" rel="stylesheet">
    </head>
    <body id="homepage">
        <!-- website title, navigation bar -->
        <div class="navbar navbar-default navbar-static-top">
            <div class="container">
                <!-- website title -->
                <a class="navbar-brand" href="http://www.estherarts.com">Esther Arts</a>

                <!-- button group -->
                <button class="navbar-toggle" data-toggle="collapse" data-target=".navHeaderCollapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <div class="collapse navbar-collapse navHeaderCollapse">
                    <ul class="nav navbar-nav navbar-right">
                        <li class="active"><a href="#">Home</a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Arts</a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Twitter</a></li>
                                <li><a href="#">FB</a></li>
                                <li><a href="#">Google Plus</a></li>
                            </ul>
                        </li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Calendar</a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Twitter</a></li>
                                <li><a href="#">FB</a></li>
                                <li><a href="#">Google Plus</a></li>
                            </ul>
                        </li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">News</a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Twitter</a></li>
                                <li><a href="#">FB</a></li>
                                <li><a href="#">Google Plus</a></li>
                            </ul>
                        </li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown">Invest</a>
                            <ul class="dropdown-menu">
                                <li><a href="#">Twitter</a></li>
                                <li><a href="#">FB</a></li>
                                <li><a href="#">Google Plus</a></li>
                            </ul>
                        </li>
                        <li>
                            <a href="contact.php">Contact</a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="high_container">
            <div class="container" id="carousel_container">
                <div class="row">
                    <div class="well">
                    <!-- Carousel
                    ================================================== -->
                        <div id="myCarousel" class="carousel slide">
                            <div class="carousel-inner">
                                <div class="item active">
                                    <div class="row">
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb11" />
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb12" />
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb13" />
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                    </div>
                                </div>
                                <div class="item">
                                    <div class="row">
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb21" />
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb22" />
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb23" />
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb24" />
                                            </a>
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                </div>
                                <div class="item">
                                    <div class="row">
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb31" />
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb32" />
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb33" />
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb34" />
                                            </a>
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                        <div class="col-md-3">
                                            <a href="#" class="thumbnail">
                                                <img class="img-responsive" src="http://placehold.it/250x160" alt="Thumb14">
                                            </a>
                                        </div>
                                </div>
                            </div>
                            <a class="left carousel-control" href="#myCarousel" data-slide="prev"><i class="fa fa-chevron-left fa-2x"></i></a>
                            <a class="right carousel-control" href="#myCarousel" data-slide="next"><i class="fa fa-chevron-right fa-2x"></i></a>
                            <ol class="carousel-indicators">
                                <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
                                <li data-target="#myCarousel" data-slide-to="1"></li>
                                <li data-target="#myCarousel" data-slide-to="2"></li>
                            </ol>
                        </div><!-- End Carousel --> 
                    </div><!-- End Well -->
                </div>
            </div>
        </div>

        <!-- sticky footer -->
        <div class="navbar navbar-default navbar-fixed-bottom">
            <div class="container" align="right">
                <a class="navbar-text">&reg 2014 estherarts.com</a>
                <li class="btn-group dropup pull-right" align="right">
                    <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                        Language <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu" role="menu">
                        <li><a href="#">English</a></li>
                        <li><a href="#">Chinese (Simplified)</a></li>
                        <li><a href="#">Chinese (Traditional)</a></li>
                        <li><a href="#">Ukrainian</a></li>
                    </ul>
                </li>
                <a class="btn btn-social-icon btn-twitter btn-lg pull-right" align="right">
                    <i class="fa fa-twitter"></i>
                </a>
                <a class="btn btn-social-icon btn-facebook btn-lg pull-right" align="right">
                    <i class="fa fa-facebook"></i>
                </a>
            </div>
        </div>

        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
        <!-- Include all compiled plugins (below), or include individual files as needed -->
        <script src="js/bootstrap.min.js"></script>
    </body>
</html>

CSS部分,

.page-header {
    margin-top: 0;
}
.panel-body {
    padding-top: 0;
}

#homepage {
    /* background: url(../img/bg1.jpg) no-repeat center center fixed; */
    -webkit-background-size: cover;
    -moz-background-size: cover;
    background-size: cover;
    -o-background-size: cover;
    height: 100%;
    margin: 0px;
    border-style:solid;
}

.high_container {
    position:relative;
}

.container {
    clear:both;
    position: relative;
    margin: 0 auto;
    text-align: left;
    vertical-align: middle;
}

#carousel_container {
    clear:both;
    position: relative;
    margin: 0 auto;
    text-align: left;
    vertical-align: middle;
}

.vertical-center {
  height:100%;
  width:100%;

  text-align: center;  /* align the inline(-block) elements horizontally */
  font: 0/0 a;         /* remove the gap between inline(-block) elements */
}

.vertical-center:before {    /* create a full-height inline block pseudo=element */
  content: ' ';
  display: inline-block;
  vertical-align: middle;  /* vertical alignment of the inline element */
  height: 100%;
}

.vertical-center > .container {
  max-width: 100%;
  background-color: gold;

  display: inline-block;
  vertical-align: middle;  /* vertical alignment of the inline element */
                           /* reset the font property */
  font: 16px/1 "Helvetica Neue", Helvetica, Arial, sans-serif;
}

#contact_form {
    float: none;
    margin-left: auto;
    margin-right: auto;
}

#myCarousel {
    display: -webkit-box;
    display: -moz-box;
    display: -ms-flexbox;
    display: -moz-flex;
    display: -webkit-flex;
    display: flex;
    -webkit-flex-flow: row wrap;
    flex-flow: row wrap;
    -webkit-align-content: center;
    align-content: center;
}
#myCarousel .thumbnail {
    margin-bottom: 0;
}

.carousel-control.left, .carousel-control.right {
    background-image:none !important;
}

.carousel-control {
    color:#fff;
    top:40%;
    color:#428BCA;
    bottom:auto;
    padding-top:4px;
    width:30px;
    height:30px;
    text-shadow:none;
    opacity:1;
}

.carousel-control:hover {
    color: #d9534f;
}

.carousel-control.left, .carousel-control.right {
    background-image:none !important;
}

.carousel-control.right {
    left:auto;
    right:-32px;
}

.carousel-control.left {
    right:auto;
    left:-32px;
}

.carousel-indicators {
    bottom:-30px;
}

.carousel-indicators li {
    border-radius:0;
    width:10px;
    height:10px;
    background:#ccc;
    border:1px solid #ccc;
}

.carousel-indicators .active {
    width:12px;
    height:12px;
    background:#3276b1;
    border-color:#3276b1;
}

http://imgur.com/5fOvB8J 这是截图。下面的图像是我主页的全屏背景图像。

当我创建边框以查看 body 元素所在的位置时,我在下面有这个图片, http://imgur.com/kuUyW74

我不明白为什么 body 元素不包含页脚,这与我在代码中定义的方式不同。

2 个答案:

答案 0 :(得分:2)

尝试以下代码。希望它会对你有所帮助。只需根据您的要求设置heightmarginposition即可。

 container{
  height: 87%;
  margin-top: -10%;
  position: fixed;
  top: 50%;
}

答案 1 :(得分:0)

如果div.container的高度是其父级/顶级div的50%,则将其位置设为绝对值并设置

.parent-of-container{
   position:relative;
}

.container{

 position:absolute;
 top:25%;


 ....  
}