2017-06-05 60 views
2

我想堆叠这些元素,使它们躺在另一个之上,但它们最终会水平地压扁。我究竟做错了什么?相对定位的内容不是垂直堆叠?

HTML:

.content-wrapper { 
 
     position: absolute; 
 
     top: 0; 
 
     left: 0; 
 
     height:100%; 
 
     width:100%; 
 
     display: flex; 
 
     align-items: center; 
 
     justify-content: center; 
 
    } 
 
    .content-box { 
 
     background-color: #f2f2f2; 
 
     padding: 5vh 5vw; 
 
     font-family: "Roboto"; 
 
     color: #676767; 
 
     text-align: center; 
 
     max-width: 60vw; 
 
     position: relative; 
 
     z-index:10; 
 
     margin: 1vh 
 
    }
<div class="content-wrapper"> 
 
     <div class="content-box"> 
 
      <span class="title"> Stats </span> 
 
      <br> 
 
      <div class="sep"></div> 
 
      <p> Lorem ipsum 1 </p> 
 
     </div> 
 
    
 
     <div class="content-box"> 
 
      <span class="title"> Stats </span> 
 
      <br> 
 
      <div class="sep"></div> 
 
      <p> Lorem ipsum 2 </p> 
 
     </div> 
 
    </div>

+0

“我试图让他们趴在彼此顶部堆叠这些元素, “? Dint让你... – Lal

回答

0

使用Flex盒代替。 flex direction

.box-list { 
 
    padding: 10px; 
 
    display: flex; 
 
    flex-direction: column; 
 
    justify-content: center; 
 
    align-items: center; 
 
} 
 
.box { 
 
    flex: 0 0 auto; 
 
    width: 50px; 
 
    height: 50px; 
 
    background-color: #ccc; 
 
    
 
    margin-bottom: 10px; 
 
}
<section class="box-list"> 
 
    <div class="box"></div> 
 
    <div class="box"></div> 
 
    <div class="box"></div> 
 
</section>

+0

OP _is_使用flexbox ....? – LGSon

0

添加flex-direction: column;到包装:

.content-wrapper { 
 
     position: absolute; 
 
     top: 0; 
 
     left: 0; 
 
     height:100%; 
 
     width:100%; 
 
     display: flex; 
 
     flex-direction: column; 
 
     align-items: center; 
 
     justify-content: center; 
 
    } 
 
    .content-box { 
 
     background-color: #f2f2f2; 
 
     padding: 5vh 5vw; 
 
     font-family: "Roboto"; 
 
     color: #676767; 
 
     text-align: center; 
 
     max-width: 60vw; 
 
     position: relative; 
 
     z-index:10; 
 
     margin: 1vh 
 
    }
<div class="content-wrapper"> 
 
     <div class="content-box"> 
 
      <span class="title"> Stats </span> 
 
      <br> 
 
      <div class="sep"></div> 
 
      <p> Lorem ipsum 1 </p> 
 
     </div> 
 
    
 
     <div class="content-box"> 
 
      <span class="title"> Stats </span> 
 
      <br> 
 
      <div class="sep"></div> 
 
      <p> Lorem ipsum 2 </p> 
 
     </div> 
 
    </div>