代码之家  ›  专栏  ›  技术社区  ›  ItFreak

绝对容器中的CSS flex布局

  •  0
  • ItFreak  · 技术社区  · 8 年前

    我有下面的HTML/CSS,它有一个容器( .full-width.section3 )它有绝对的高度。但是,将内容放在中心不起作用。既然我是新来的flex模型,有人能解释给我,为什么三个孩子 div

    body {
      margin:0;
      
    }
    .container {
      display:flex;
      flex-wrap:wrap;     
      height:100vh;
      background-color: white;
    }
    .full-width {
      width:100%;              
    }
    
    .full-width.section3{
    	height: 795px;
    }
    
    .full-width > .content > .third-parent{
      height: 100%;
      display: flex;
    }
    
    .full-width > .content > .third-parent > .third{
      position: relative;
      flex: 1 1 0px;
      width: 100%;
      border: 1px solid black;
    }
    
    .full-width > .content > .third-parent > .third > img{
      position: absolute;
      width: 100%;
      height: auto;
      left: 50%;
      top:50%;
      visibility: visible;
      text-align: center;
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
    }
    
    .middle-text{
      position: absolute;
      width: 100%;
      left: 50%;
      top:50%;
      visibility: visible;
      text-align: center;
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
    }
    
    
    .full-width > .content > .third-parent > .third > .middle-text > .list-div2{
        display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
    }
    
    .full-width > .content > .third-parent > .third > .middle-text > .list-div2  li{
       position: relative;
      display: block;
      margin-bottom: 5px;
      padding: 10px;
      text-align: left;
      text-transform: uppercase;
      visibility: visible;
    }
    
    .list-div2 li::before{
      content: '\2022';
      margin-right: 10px;
    }
    .items-list2 {
      margin: 0;
      padding: 0;
    }
    <div class="container">
    <div class="full-width section3">
        <div class="content">
           <div class="third-parent">
             <div class="third" id="one">
               <img src="https://fakeimg.pl/350x200/?text=right">
              </div>
            <div class="third" id="two">
              <div class="middle-text">
                <h1>Some headline</h1>
                      <div class="list-div2">
            <ul class="items-list2" id="list">
              <li>Entry A</li>
              <li>Entry B</li>
              <li>Entry C</li>
              <li>Entry D</li>
            </ul>
          </div>
              </div>
            </div>
            <div class="third" id="three">
              <img src="https://fakeimg.pl/350x200/?text=left">
            </div>
             </div>
           </div>
    </div>
    </div>
    3 回复  |  直到 8 年前
        1
  •  5
  •   Katinka Hesselink    8 年前

    你把弹性和绝对定位结合起来了。这打破了子元素的灵活性:

    因为它是流出,一个绝对定位的子flex

    资料来源: https://www.w3.org/TR/css-flexbox-1/#abspos-items

        2
  •  0
  •   Paulie_D    8 年前

    我不太清楚你想实现什么,但flex布局并不适合绝对定位的孩子

    如果对中是一个问题,那么在flexbox中有一个可用的选项。

      justify-content: center;
      align-items: center;
    

    body {
      margin: 0;
    }
    
    .container {
      display: flex;
      flex-wrap: wrap;
      height: 100vh;
      background-color: white;
    }
    
    .full-width {
      width: 100%;
    }
    
    .full-width.section3 {
      height: 795px;
    }
    
    .full-width>.content>.third-parent {
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px solid black;
      text-align: center;
    }
    
    .full-width>.content>.third-parent>.third {
      position: relative;
      flex: 1 1 0px;
      width: 100%;
      border: 1px solid black;
    }
    
    .full-width>.content>.third-parent>.third>img {
      max-width: 100%;
      display: block;
    }
    
    .full-width>.content>.third-parent>.third>.middle-text>.list-div2 {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
    }
    
    .full-width>.content>.third-parent>.third>.middle-text>.list-div2 li {
      position: relative;
      display: block;
      margin-bottom: 5px;
      padding: 10px;
      text-align: left;
      text-transform: uppercase;
      visibility: visible;
    }
    
    .list-div2 li::before {
      content: '\2022';
      margin-right: 10px;
    }
    
    .items-list2 {
      margin: 0;
      padding: 0;
    }
    <div class="container">
      <div class="full-width section3">
        <div class="content">
          <div class="third-parent">
            <div class="third" id="one">
              <img src="https://fakeimg.pl/350x200/?text=right">
            </div>
            <div class="third" id="two">
              <div class="middle-text">
                <h1>Some headline</h1>
                <div class="list-div2">
                  <ul class="items-list2" id="list">
                    <li>Entry A</li>
                    <li>Entry B</li>
                    <li>Entry C</li>
                    <li>Entry D</li>
                  </ul>
                </div>
              </div>
            </div>
            <div class="third" id="three">
              <img src="https://fakeimg.pl/350x200/?text=left">
            </div>
          </div>
        </div>
      </div>
    </div>
        3
  •  0
  •   Anduel    8 年前

    你需要去掉位置:绝对;分类中间文本并添加显示:inline;使中柱居中

    body {
      margin:0;
      
    }
    .container {
      display:flex;
      flex-wrap:wrap;     
      height:100vh;
      background-color: white;
    }
    .full-width {
      width:100%;              
    }
    
    .full-width.section3{
    	height: 795px;
    }
    
    .full-width > .content > .third-parent{
      height: 100%;
      display: flex;
    }
    
    .full-width > .content > .third-parent > .third{
      position: relative;
      flex: 1 1 0px;
      width: 100%;
      border: 1px solid black;
    }
    
    .full-width > .content > .third-parent > .third > img{
      position: absolute;
      width: 100%;
      height: auto;
      left: 50%;
      top:50%;
      visibility: visible;
      text-align: center;
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
    }
    
    .middle-text{
      width: 100%;
      visibility: visible;
      text-align: center;
      -webkit-transform: translate(-50%, -50%);
      transform: translate(-50%, -50%);
      margin: auto;
      display: inline;
    }
    
    
    .full-width > .content > .third-parent > .third > .middle-text > .list-div2{
        display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
    }
    
    .full-width > .content > .third-parent > .third > .middle-text > .list-div2  li{
       position: relative;
      display: block;
      margin-bottom: 5px;
      padding: 10px;
      text-align: left;
      text-transform: uppercase;
      visibility: visible;
    }
    
    .list-div2 li::before{
      content: '\2022';
      margin-right: 10px;
    }
    .items-list2 {
      margin: 0;
      padding: 0;
    }
    <div class="container">
    <div class="full-width section3">
        <div class="content">
           <div class="third-parent">
             <div class="third" id="one">
               <img src="https://fakeimg.pl/350x200/?text=right">
              </div>
            <div class="third" id="two">
              <div class="middle-text">
                <h1>Some headline</h1>
                      <div class="list-div2">
            <ul class="items-list2" id="list">
              <li>Entry A</li>
              <li>Entry B</li>
              <li>Entry C</li>
              <li>Entry D</li>
            </ul>
          </div>
              </div>
            </div>
            <div class="third" id="three">
              <img src="https://fakeimg.pl/350x200/?text=left">
            </div>
             </div>
           </div>
    </div>
    </div>