内联块子自动宽度奇怪的行为

时间:2017-04-13 18:53:21

标签: html css

考虑以下标记和CSS:



.parent {
    position: relative;
    float: right;
    width: 400px;
    height: 35px;
    padding: 0 4px;
    margin: 7px 20px;
    border: 1px solid #aaa;
    border-radius: 5px;
    cursor: text;
    white-space: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
}

.child {
    display: inline-block;
    height: 100%;
    min-width: 1px;
    vertical-align: top;
    background-color: rgba(255, 0, 0, 0.5);
}

.sub-child {
    display: inline-table;
    height: calc(100% - 8px);
    background-color: #ddd;
    line-height: 27px;
    font-size: 0.9em;
    padding: 0 7px;
    margin: 4px 4px 4px 0;
    border-radius: 25px;
}

.sub-child span {
    display: table-cell;
    vertical-align: middle;
}

.sub-child .img {
    width: 20px;
    background-color: blue;
}

.sub-child .name {
    padding: 0 7px;
    max-width: 100px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}

.sub-child .closebtn {
    position: relative;
    top: -1px;
    color: #525353;
    font-weight: bold;
    font-size: 20px;
    cursor: pointer;
}

.sub-child .closebtn:before {
    content: "\00d7";
}

.sub-child .closebtn:hover {
    color: #000;
}

.other-child {
    display: inline-block;
    height: 100%;
    width: 100px;
    background-color: rgba(0, 0, 0, 0.5);
}

<div class="parent">
    <div class="child">
        <div class="sub-child">
            <span class="img"></span>
            <span class="name">TestClient</span>
            <span class="closebtn"></span>
        </div>
        <div class="sub-child">
            <span class="img"></span>
            <span class="name">Biggle Boo</span>
            <span class="closebtn"></span>
        </div>
        <div class="sub-child">
            <span class="img"></span>
            <span class="name">Ratty Tatty</span>
            <span class="closebtn"></span>
        </div>
        <div class="sub-child">
            <span class="img"></span>
            <span class="name">Shig Wiggle Miggleson</span>
            <span class="closebtn"></span>
        </div>
    </div>
    <div class="other-child"></div>
</div>
&#13;
&#13;
&#13;

我创建了一个jsfiddle来说明我遇到的问题。除非移除.child范围,否则.img宽度不会自动扩展到其内容的大小。似乎为table-cell元素指定固定宽度会更改容器的行为。这是一个错误吗?

1 个答案:

答案 0 :(得分:0)

我通过从.img table-cell元素中删除固定宽度并插入具有固定宽度的子元素来修复此问题。但我仍然不确定为什么为表格单元格指定固定宽度会破坏.child元素的自动宽度。

.parent {
    position: relative;
    float: right;
    width: 400px;
    height: 35px;
    padding: 0 4px;
    margin: 7px 20px;
    border: 1px solid #aaa;
    border-radius: 5px;
    cursor: text;
    white-space: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
}

.child {
    display: inline-block;
    height: 100%;
    min-width: 1px;
    vertical-align: top;
    background-color: rgba(255, 0, 0, 0.5);
}

.sub-child {
    display: inline-table;
    height: calc(100% - 8px);
    background-color: #ddd;
    line-height: 27px;
    font-size: 0.9em;
    padding: 0 7px;
    margin: 4px 4px 4px 0;
    border-radius: 25px;
}

.sub-child span {
    display: table-cell;
    vertical-align: middle;
}

.sub-child .img:before {
    content: "";
    width: 20px;
    height: 100%;
    background-color: blue;
}

.sub-child .name {
    padding: 0 7px;
    max-width: 100px;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
}

.sub-child .closebtn {
    position: relative;
    top: -1px;
    color: #525353;
    font-weight: bold;
    font-size: 20px;
    cursor: pointer;
}

.sub-child .closebtn:before {
    content: "\00d7";
}

.sub-child .closebtn:hover {
    color: #000;
}

.other-child {
    display: inline-block;
    height: 100%;
    width: 100px;
    background-color: rgba(0, 0, 0, 0.5);
}
<div class="parent">
    <div class="child">
        <div class="sub-child">
            <span class="img"></span>
            <span class="name">TestClient</span>
            <span class="closebtn"></span>
        </div>
        <div class="sub-child">
            <span class="img"></span>
            <span class="name">Biggle Boo</span>
            <span class="closebtn"></span>
        </div>
        <div class="sub-child">
            <span class="img"></span>
            <span class="name">Ratty Tatty</span>
            <span class="closebtn"></span>
        </div>
        <div class="sub-child">
            <span class="img"></span>
            <span class="name">Shig Wiggle Miggleson</span>
            <span class="closebtn"></span>
        </div>
    </div>
    <div class="other-child"></div>
</div>

fixed fiddle