如何使用背景图像后面的引导程序在中心上方放置<div>?

时间:2017-03-31 23:40:10

标签: html css twitter-bootstrap

我试图将我的照片<div class="shape">放在使用引导程序在中心上方的<div class="hero">背景图像的顶部。

然而,背景颜色和图像都出现在我的照片上方。我正在寻找背景图片,以便在我的照片背后,以及此时此刻不能看到的背景颜色。

如果您决定帮助我,请不要删除背景颜色,因为我想在稍后阶段使用它。

请全屏打开代码段以查看我的问题。

&#13;
&#13;
<style>
      .body {
      font-family: 'Roboto', sans-serif;
      color: black;
      }
      .navbar.navbar-default {
      background-color: #FFFFFF;
      height: 10vh;
      z-index: 100;
      }
      .navbar.navbar-default ul {
      list-style-type: none;
      text-align: center;
      }
      .navbar.navbar-default ul li {
      display: inline-block;
      }
      .dropdown-menu li {
      text-align: center;
      }
      .dropdown .dropdown-menu {
      background-color: #FFFFFF;
      }
      .dropdown .dropdown-menu a {
      color: white;
      }
      .navbar.navbar-default ul li a {
      display: inline-block;
      padding: 3.5vh 8px 4px;
      color: black;
      text-decoration: none;
      font-size: 14pt;
      font-family: 'Roboto', sans-serif;
      }
      .navbar.navbar-default ul li:after {
      content: '';
      position: absolute;
      right: 50%;
      bottom: 0;
      left: 50%;
      height: 3px;
      background-color: black;
      border-radius: 9px;
      transition: all .2s;
      }
      .nav.navbar-nav,
      .nav.navbar-nav>li {
      float: none;
      }
      .navbar.navbar-default ul li:hover:after {
      right: 0;
      left: 0;
      }
      .navbar.navbar-default ul.dropdown-menu li,
      .navbar.navbar-default ul.dropdown-menu li a {
      position: relative;
      display: block;
      }
      .section {
      min-height: 100vh;
      }
      .one {
      background-color:#c00;
      }
      .two {
      background: #563D7C;
      }
      #sections .section {
      padding-top: ~50px;
      }
      .hero {
      background: url("http://mowebusa.nobletechindia.com/wp-content/uploads/2016/05/computer.jpg") center center no-repeat;
      background-attachment: fixed;
      position: relative;
      background-size: cover;
      width: 100%;
      max-width: 100%;
      width: 100vw;
      height: 100%;
      }
      .shape {
      border-radius: 25px;
      background: #4D5061;
      content: url(http://i1126.photobucket.com/albums/l611/ldocherty1/IMG_0730_zpsiz4dqc47.jpg);
      color: white;
      height: 300px;
      margin: auto;
      padding: 3px;
      width: 300px;
      margin-left: auto;
      margin-right: auto;
      }
   </style>
&#13;
!DOCTYPE html>
<html lang="en">
   <head>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/2.9.4/jquery.fullpage.min.js"></script>
   </head>
   
   <body>
      <nav class="navbar navbar-default navbar-fixed-top">
         <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
               <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
               <span class="sr-only">Toggle navigation</span>
               <span class="icon-bar"></span>
               <span class="icon-bar"></span>
               <span class="icon-bar"></span>
               </button>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
               <ul class="nav navbar-nav">
                  <li><a href="#">Home</a>
                  </li>
                  <li class="dropdown">
                     <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">About Me <span class="caret"></span></a>
                     <ul class="dropdown-menu">
                        <li><a href="#">Action</a>
                        </li>
                        <li><a href="#section3">Contact</a>
                        </li>
                        <li><a href="#">Something else here</a>
                        </li>
                        <li><a href="#">Separated link</a>
                        </li>
                        <li><a href="#">One more separated link</a>
                        </li>
                     </ul>
                  </li>
                  <li class="dropdown">
                     <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Units <span class="caret"></span></a>
                     <ul class="dropdown-menu">
                        <li><a href="#">Action</a>
                        </li>
                        <li><a href="#">Another action</a>
                        </li>
                        <li><a href="#">Something else here</a>
                        </li>
                        <li><a href="#">Separated link</a>
                        </li>
                        <li><a href="#">One more separated link</a>
                        </li>
                     </ul>
                  </li>
                  <li><a href="#">Clients</a>
                  </li>
                  <li><a href="#contact-me">Contact Me</a>
                  </li>
               </ul>
            </div>
            <!-- /.navbar-collapse -->
         </div>
         <!-- /.container-fluid -->
      </nav>
      <div id="sections">
         <div class="section one">
            <div class="shape"></div>
            <div class="hero"></div>
            <a href="#section two"> <i class="fa fa-angle-down" style="font-size:100px;"></i></a>
         </div>
         <div class="section two"></div>
            <a href="#section one"> <i class="fa fa-angle-up" style="font-size:100px;"></i></a>
      </div>
      <script>
         $(document).ready(function() {
           $('#sections').fullpage();
         });
      </script>
   </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以按照以下步骤执行此操作:

  1. position: absolute添加到.shape以重叠英雄图片。
  2. z-index: 10 image
  3. 之上添加.shape以获取.hero

    现在,要居中对齐定位元素,请记住以下技巧:

    1. 使用left: 50%将定位元素从左侧放置50%。
    2. 添加margin-left: -150px
    3. 如果元素的宽度为1px.shape将在左起50%后开始),那么第一步就可以正常工作了,但由于它的宽度为300px,所以必须添加一个负余量 - 左边等于元素宽度的一半(300/2 = 150)。

      同样,对于垂直对齐,您可以使用top: 50%和负margin-top等于height的1/2。

      注意: padding-top: ~50px;出现问题,删除~,因为它是无效令牌。此外,填充不能为负。

      &#13;
      &#13;
      .body {
        font-family: 'Roboto', sans-serif;
        color: black;
      }
      
      .navbar.navbar-default {
        background-color: #FFFFFF;
        height: 10vh;
        z-index: 100;
      }
      
      .navbar.navbar-default ul {
        list-style-type: none;
        text-align: center;
      }
      
      .navbar.navbar-default ul li {
        display: inline-block;
      }
      
      .dropdown-menu li {
        text-align: center;
      }
      
      .dropdown .dropdown-menu {
        background-color: #FFFFFF;
      }
      
      .dropdown .dropdown-menu a {
        color: white;
      }
      
      .navbar.navbar-default ul li a {
        display: inline-block;
        padding: 3.5vh 8px 4px;
        color: black;
        text-decoration: none;
        font-size: 14pt;
        font-family: 'Roboto', sans-serif;
      }
      
      .navbar.navbar-default ul li:after {
        content: '';
        position: absolute;
        right: 50%;
        bottom: 0;
        left: 50%;
        height: 3px;
        background-color: black;
        border-radius: 9px;
        transition: all .2s;
      }
      
      .nav.navbar-nav,
      .nav.navbar-nav>li {
        float: none;
      }
      
      .navbar.navbar-default ul li:hover:after {
        right: 0;
        left: 0;
      }
      
      .navbar.navbar-default ul.dropdown-menu li,
      .navbar.navbar-default ul.dropdown-menu li a {
        position: relative;
        display: block;
      }
      
      .section {
        min-height: 100vh;
      }
      
      .one {
        background-color: #c00;
      }
      
      .two {
        background: #563D7C;
      }
      
      #sections .section {
        padding-top: 50px;
      }
      
      .hero {
        background: url("http://mowebusa.nobletechindia.com/wp-content/uploads/2016/05/computer.jpg") center center no-repeat;
        background-attachment: fixed;
        position: relative;
        background-size: cover;
        width: 100%;
        max-width: 100%;
        width: 100vw;
        height: 100%;
      }
      
      .shape {
        border-radius: 25px;
        background: #4D5061;
        content: url(http://i1126.photobucket.com/albums/l611/ldocherty1/IMG_0730_zpsiz4dqc47.jpg);
        color: white;
        height: 300px;
        margin: auto;
        padding: 3px;
        width: 300px;
      
        /* add this */
        position: absolute;
        left: 50%;
        margin-left: -150px;
        z-index: 10;
      }
      &#13;
      !DOCTYPE html>
      <html lang="en">
      
      <head>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/2.9.4/jquery.fullpage.min.js"></script>
      </head>
      
      <body>
        <nav class="navbar navbar-default navbar-fixed-top">
          <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
              <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                     <span class="sr-only">Toggle navigation</span>
                     <span class="icon-bar"></span>
                     <span class="icon-bar"></span>
                     <span class="icon-bar"></span>
                     </button>
            </div>
            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
              <ul class="nav navbar-nav">
                <li><a href="#">Home</a>
                </li>
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="true">About Me <span class="caret"></span></a>
                  <ul class="dropdown-menu">
                    <li><a href="#">Action</a>
                    </li>
                    <li><a href="#section3">Contact</a>
                    </li>
                    <li><a href="#">Something else here</a>
                    </li>
                    <li><a href="#">Separated link</a>
                    </li>
                    <li><a href="#">One more separated link</a>
                    </li>
                  </ul>
                </li>
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Units <span class="caret"></span></a>
                  <ul class="dropdown-menu">
                    <li><a href="#">Action</a>
                    </li>
                    <li><a href="#">Another action</a>
                    </li>
                    <li><a href="#">Something else here</a>
                    </li>
                    <li><a href="#">Separated link</a>
                    </li>
                    <li><a href="#">One more separated link</a>
                    </li>
                  </ul>
                </li>
                <li><a href="#">Clients</a>
                </li>
                <li><a href="#contact-me">Contact Me</a>
                </li>
              </ul>
            </div>
            <!-- /.navbar-collapse -->
          </div>
          <!-- /.container-fluid -->
        </nav>
        <div id="sections">
          <div class="section one">
      
            <div class="shape"></div>
            <div class="hero"></div>
            <a href="#section two"> <i class="fa fa-angle-down" style="font-size:100px;"></i></a>
          </div>
          <div class="section two"></div>
          <a href="#section one"> <i class="fa fa-angle-up" style="font-size:100px;"></i></a>
        </div>
        <script>
          $(document).ready(function() {
            $('#sections').fullpage();
          });
        </script>
      </body>
      
      </html>
      &#13;
      &#13;
      &#13;