在css flexbox中调整大小时如何在div中包含文本

时间:2017-05-25 07:37:30

标签: html css flexbox

如果我使用表格和表格单元格,我可以这样实现:

    #wrap {
        /* Your styling. */
        position: absolute;
        border: 1px solid black;
        z-index: 999999;
        right: 0;
        height: 60%;
        text-align: center;
    
        /* Solution part I. */
        display: table;
    }
    
    /* Solution part II. */
    #inside {
        width: 100%;
        height: 100%;
        display: table-cell;
        vertical-align: middle;
    }
 <div id=wrap>
      <div id=inside>
        Content, content, content.
      </div> 
    </div>
    

但是如何在css Flex框中实现相同的目标呢?

我的代码是这样的flex:

.dflex{
   display: flex;
}

.orange{
   background: #F97434;
}
    
.section2 {
    height: 874px;
    min-height: 34em;
    height: 1056px;
    background: #424242;
}

.block{
    padding: 0% 20%;
}

.block-wrap1, .block-wrap2{
    /*height: 37%;*/
    height: 323px;
    width: 40.8%;
    border-radius: 0.25em;
    padding: 1em;
    transition: all 1s ease;
    margin: auto 0 1.5em 2em;
}

.section2 .block-content{
    padding: 0 1em;
    align-self: center;
}

.block-content{
    line-height: 1.5em;
    font-size: 1.6em;
}
<section class="section2 cscroll">

<div class="block dflex">
    <div class="block-wrap1 orange dflex"><div class="block-content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's</div> </div>
    <div class="block-wrap1 orange dflex"><div class="block-content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's</div> </div>
</div>

<div class="block dflex">
    <div class="block-wrap2 orange dflex"><div class="block-content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's</div> </div>
    <div class="block-wrap2 orange dflex"><div class="block-content">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's</div> </div>
</div>

</section>

由于两个块在两行上,display:flex使用了两次,因此stretch对于所有块都相等。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

修改

如果您有多个不同高度的积木,我建议您使用strech而不是固定高度。如下例所示:

.dflex{
    display: flex;
    flex-flow: row wrap;
    justify-content: center;
    align-items: stretch;
}

.section2 {
    min-height: 34em;
}


.block{
    padding: 0% 20%;
}

.block-wrap1{
    //flex: 0 0 100%;
    flex: 1 1 0px;
    min-width: initial;
    border-radius: 0.25em;
    padding: 1em;
    transition: all 1s ease;
    border: 1px solid black;
    word-break: break-all;
}

.section2 .block-content{
    padding: 0 1em;
    align-self: center;
}

.block-content{
    line-height: 1.5em;
    font-size: 1.6em;
    margin: 0;
}
<section class="section2">
    <div class="block dflex">
        <div class="block-wrap1">
            <p class="block-content">Isn’t the pharmaceutical industry just out to make money?</p> 
        </div>
        
         <div class="block-wrap1">
            <p class="block-content">Isn’t the pharmaceutical.</p> 
        </div>
        
         <div class="block-wrap1">
            <p class="block-content">Isn’t the pharmaceutical industry.</p> 
        </div>
    </div>
</section>