简体   繁体   English

如何使水平的div弹性

[英]How to make a horizontal set of divs elastic

I have a set of 1-4 divs sitting vertically inside of a <section> . 我有一组1-4个div垂直坐在<section>内部。 There are 4 buttons which decide how many divs will be invoked. 有4个按钮决定将调用多少个div。 I need the divs to expand to a proportional size, side by side, when they are instantiated. 当实例化它们时,我需要将div并排扩展到成比例的大小。 Here is some HTML and CSS code which relate to this. 这是与此相关的一些HTML和CSS代码。

在此处输入图片说明

First the HTML5 markup... 首先是HTML5标记...

<section id="main">
  <div id="resultContainer">
    <section id="i0" class="wDay">
        <h2>
            <span class="day" ></span>
            <span class="temp" ></span>
        </h2>
        <img src="">
        <span class="wDescript" style="color:#2050ff;word-break:break-all;"></span> 
        <span class="wind" ></span><br/> 
        <span class="humid" ></span> 
        <span class="other1" ></span> 
        <span class="other2" ></span> 
    </section>
    <section id="i1" class="wDay">
        <h2>
            <span class="day"></span>
            <span class="temp"> </span>
        </h2>
        <img src="">
        <span class="wDescript" style="color:#2050ff;word-break:break-all;"></span> 
        <span class="wind" ></span><br/> 
        <span class="humid"></span> 
        <span class="other1"></span> 
        <span class="other2"></span> 
    </section>
    <section id="i2" class="wDay">
        <h2>
            <span class="day" ></span>
            <span class="temp" </span>
        </h2>
        <img src="">
        <span class="wDescript" style="color:#2050ff;word-break:break-all;"></span> 
        <span class="wind"></span><br/>  
        <span class="humid"></span> 
        <span class="other1"></span> 
        <span class="other2"></span> 
    </section>
    <section id="i3" class="wDay">
        <h2>
            <span class="day"></span>
            <span class="temp"></span>
        </h2>
        <img src="">
        <span class="wDescript" style="color:#2050ff;word-break:break-all;"></span> 
        <span class="wind"></span><br/>  
        <span class="humid"></span> 
        <span class="other1"></span> 
        <span class="other2"></span> 
    </section>
  </div>
</section>

And the CSS: 和CSS:

#main {
filter: none;
position: relative;
top: 20px;
height: 400px;
-webkit-background-color: rgba(72, 72, 72, 0.2);
background-color: rgba(72, 72, 72, 0.2);
/*background-color: #484848;*/
width: 460px;
margin: 0 auto;
-webkit-border-radius: 5px;
-webkit-border: 1px solid #a1a1a1;
border-radius: 5px;
border: 1px solid #a1a1a1;
}

TIA TIA

Dennis 丹尼斯

Since Legacy support was required and I had trouble even with the use of a table, I improvised a non-CSS only solution. 由于需要传统支持,即使使用表也遇到了麻烦,所以我提出了一种非CSS的解决方案。 Clicking on the buttons 1-4 gives me the divisor (nDays) and I used it to divide the width of the container, which gave me the correct width for each div which houses the forecast day div. 单击按钮1-4,可以得到除数(nDays),然后用它来划分容器的宽度,这可以为容纳预测日div的每个div提供正确的宽度。

                dayWidth = parseInt(420/nDays);
                $('#i'+i).css('width',dayWidth);
                $('#i'+i).css('display','inline-block');

Works great, without all the convolutions of trying to make CSS do it automatically. 效果很好,无需尝试使CSS自动进行所有操作。

And here is the result. 这就是结果。

在此处输入图片说明

Edit: I missed the bit about legacy support being required, making this answer not quite appropriate. 编辑:我错过了有关需要遗留支持的一点,使这个答案不太合适。

CSS solution: CSS解决方案:

#resultContainer{
    table-layout: fixed; /* Stops cell widths varying depending on content */
}

.wDay{
    display:table-cell;  /* Display as table cell. */
    width: 1%;           /* Needs some width to start with */
    text-align: center;  /* centres cell content and balances "table" margins */
}

There's no need to put a display: table; 无需放置display: table; in the parent div but you could to make it clearer. 在父div中,但您可以使其更清晰。

Need to check different browsers! 需要检查不同的浏览器!

please try this one in css 请在css中尝试这个

.wDay{
    float: left;
    margin: 5px;
}

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

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