在其他可变宽度元素旁边设置HTML元素的可变百分比宽度

时间:2014-09-17 02:53:25

标签: html css variable-width

我有一个给定CSS的HTML结构 captionprogress元素都应该在同一行中呈现。 caption元素不应具有固定的宽度,progress元素应根据其内联设置宽度填充caption旁边的其余空格,这意味着每progress个{} element将具有不同的总像素宽度,但应仅填充给定百分比的可用空间 HTML结构和CSS规则可以以任何方式更改。

是否可以仅使用CSS解决此问题?



.table {
    padding: 15px;
    width: 280px;
    border: 1px solid black;
    font-family: sans-serif;
    font-size: 12px;
}

.caption {
    float: left;
}

.progress {
    height: 14px;
    border: 1px solid white;
    border-radius: 4px;
    background-color: green;
    overflow: hidden;
}

.value {
    margin-left: 5px;
}

<div class="table">
    <div class="row">
        <div class="caption">Short text: </div>
        <div class="progress" style="width:11.65%">
            <span class="value">11.65</span>
        </div>
    </div>
    
    <div class="row">
        <div class="caption">A bit longer text: </div>
        <div class="progress" style="width:100%">
            <span class="value">100.00</span>
        </div>
    </div>
    
    <div class="row">
        <div class="caption">X: </div>
        <div class="progress" style="width:45.50%">
            <span class="value">45.50</span>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:1)

您是否考虑过使用Flexbox

只需添加此规则:

.row {
    display: flex;
}

如果您担心浏览器支持,可以选择使用display:table。您应该更改标记和CSS,如下所示:

&#13;
&#13;
.table {
        border: 1px solid black;
        font-family: sans-serif;
        font-size: 12px;
        padding: 15px;
        width: 280px;
    }
    .inner-table {
        display: table;
    }
    .row {
        display: table-row;
    }
    .caption {
        display: table-cell;
        white-space: nowrap;
        width: 1%;
    }
    .progress {
        background-color: green;
        border: 1px solid white;
        border-radius: 4px;
        display: table-cell;
        height: 14px;
    }
    .value {
        margin-left: 5px;
        display:block;
        width:0;
        overflow: visible;
    }
&#13;
<div class="table">
        <div class="inner-table">
            <div class="row">
                <div class="caption">Short text: </div>
                <div style="width:1.65%" class="progress">
                    <span class="value">1.65</span>
                </div>
                <div class="remainder"></div>
            </div>
        </div>
        
        <div class="inner-table">
            <div class="row">
                <div class="caption">A bit longer text: </div>
                <div style="width:100%" class="progress">
                    <span class="value">100.00</span>
                </div>
                  <div class="remainder"></div>
            </div>
        </div>
        
        <div class="inner-table">
            <div class="row">
                <div class="caption">X: </div>
                <div class="progress" style="width:45.50%">
                    <span class="value">45.50</span>
                </div>
                <div class="remainder"></div>
            </div>
        </div>
    </div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

请试试这个 - 填充权:5px;显示:内联;在进度类中添加这些属性,并删除正在进行的宽度。

答案 2 :(得分:0)

嗯,仅仅为了将来的参考,我正在玩一下flexbox thingie并想出了这个:

&#13;
&#13;
.table {
    padding: 15px;
    width: 280px;
    border: 1px solid black;
    font-family: sans-serif;
    font-size: 12px;
}

.row {
    display: flex;
}

.caption {
    margin: 1px 5px 1px 0;
}

.progress {
    flex-grow: 1;
  	margin: auto;
}

.progress-content {
    height: 14px;
    border-radius: 4px;
    background-color: green;
}

.value {
    margin-left: 5px;
}
&#13;
<div class="table">
    <div class="row">
        <div class="caption">Short text:</div>
        <div class="progress">
            <div class="progress-content" style="width:11.65%">
                <span class="value">11.65</span>
            </div>
        </div>
    </div>
    
    <div class="row">
        <div class="caption">A bit longer text:</div>
        <div class="progress">
            <div class="progress-content" style="width:100%">
                <span class="value">100.00</span>
            </div>
        </div>
    </div>
    
    <div class="row">
        <div class="caption">X:</div>
        <div class="progress">
            <div class="progress-content" style="width:45.50%">
                <span class="value">45.50</span>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13;

如果我得到没有flexbox的解决方案,会接受它作为答案:)