简体   繁体   English

拉伸时强制Flex Div项目保持其宽度

[英]Forcing Flex Div items to maintain their width when stretching

Apologies if I am not clear. 抱歉,如果我不清楚。 I am showing three div boxes in each row and extending them through Flex to make sure they adapt to multiple displays. 我将在每行中显示三个div框,并通过Flex扩展它们,以确保它们适用于多个显示。

What happens is that they unfortunately stretch to maximum width if the second row has less than three divs. 不幸的是,如果第二行小于3 div,它们将不幸地伸展到最大宽度。

I want that even if there are two divs in the second row, it should have the same width as top row items. 我希望即使第二行有两个div,它的宽度也应与顶行项目的宽度相同。

For examples see this picture:- 例如看这张图片: 在此处输入图片说明

Here is what I am doing:- 这是我在做什么:-

HTML HTML

<div class="flex-div-list">  

        <div class="flex-box">
            <div class="info-box">
              <span class="extra-large">1234</span>Netus ultrices ullamcorper a mus nunc c
            </div>

            <div class="clearfix"></div>
        </div>
        <div class="flex-box">

            <div class="info-box">
              <span class="extra-large">824</span>onsectetur rhoncus aenean pharetra vulputate.
            </div>

          <div class="clearfix"></div>
        </div>
        <div class="flex-box">

            <div class="info-box">
              <span class="extra-large">XYZ%</span>onsectetur rhoncus aenean pharetra
            </div>

            <div class="clearfix"></div>
        </div>
        <div class="flex-box">

            <div class="info-box">
              <span class="extra-large">w-u</span>etur rhoncus aenean pharetra vulputate
            </div>

            <div class="clearfix"></div>
        </div>
        <div class="flex-box">

            <div class="info-box">
              <span class="extra-large">PODW</span>Penatibus vivamus natoque                          
            </div>

          <div class="clearfix"></div>
        </div>

</div>

CSS CSS

.flex-div-list {
    display: flex;
    position: relative;
    flex-flow: row wrap;
    align-items: stretch;
    justify-content: left;
}

.flex-box {
    float: left;
    padding: 5% 3% 1% 0%;
    width: 33.3%;
    height: auto;
    flex: 1;
    flex-basis: 33%;
}

.info-box {
    border: 1px #fff solid;
    padding-left: 10px;
    font-size: 1.7em;
    color: #fff;
    text-align: center;
    height: 9em;
    padding-right: 10px;
    padding-top: 15px;
    padding-bottom: 32px;
    overflow: hidden;
}

Thanks a lot. 非常感谢。

You just need to tell the boxes not to grow. 您只需要告诉盒子不要增长即可。

.flex-box {
    /* float: left; */
    /* overridden by flexbox */
    flex: 0 0 33.3%;
    padding: 1%;
}

 * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; margin: 0; padding: 0; } body { background: blue; } .flex-div-list { display: flex; position: relative; flex-flow: row wrap; justify-content: center; } .flex-box { /* float: left; */ /* overridden by flexbox */ flex: 0 0 33.3%; padding: 1%; } .info-box { border: 1px #fff solid; padding-left: 10px; font-size: 1.7em; color: #fff; text-align: center; height: 6em; padding-right: 10px; padding-top: 15px; padding-bottom: 32px; overflow: hidden; } 
 <div class="flex-div-list"> <div class="flex-box"> <div class="info-box"> <span class="extra-large">1234</span>Netus ultrices ullamcorper a mus nunc c</div> </div> <div class="flex-box"> <div class="info-box"> <span class="extra-large">824</span>onsectetur rhoncus aenean pharetra vulputate.</div> </div> <div class="flex-box"> <div class="info-box"> <span class="extra-large">XYZ%</span>onsectetur rhoncus aenean pharetra</div> </div> <div class="flex-box"> <div class="info-box"> <span class="extra-large">wu</span>etur rhoncus aenean pharetra vulputate</div> </div> <div class="flex-box"> <div class="info-box"> <span class="extra-large">PODW</span>Penatibus vivamus natoque</div> </div> </div> 

JSFiddle Demo JSFiddle演示

Here's what you have in your code: 这是代码中的内容:

.flex-box {
    float: left;
    padding: 5% 3% 1% 0%;
    width: 33.3%;
    height: auto;
    flex: 1;
    flex-basis: 33%;
}

When you specify flex: 1 you are saying (in shorthand): flex-grow: 1 , flex-shrink: 1 and flex-basis: 0 . 当您指定flex: 1 (简写为): flex-grow: 1flex-shrink: 1flex-basis: 0

flex-grow: 1 tells flex items to distribute all available space among themselves, which is why they evenly stretch across the maximum width of the container. flex-grow: 1告诉flex项目在它们之间分配所有可用空间,这就是为什么它们在容器的最大宽度上均匀伸展的原因。

If you don't want a flex item to grow, specify flex-grow: 0 or flex: 0 . 如果您不希望伸缩项目增加,请指定flex-grow: 0flex: 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM