2017-04-18 110 views
1

我想要兩個相鄰的文本框。第一個應該給第二個更重要的空間。我怎樣才能優先考慮一個div比另一個

怎麼可能不給固定的寬度和使用文本溢出:省略號放在了第一位?

例子:https://jsfiddle.net/by2e9uej/

div { 
 
    border: 1px solid black; 
 
} 
 
#test1 { 
 
    width: 250px; 
 
    overflow: hidden; 
 
    white-space: nowrap; 
 
} 
 
#test1 > div { 
 
    display: inline-block; 
 
} 
 
#test2 { 
 
    -o-text-overflow: ellipsis; /* Opera */ 
 
    text-overflow: ellipsis; /* IE, Safari (WebKit) */ 
 
    overflow:hidden;    /* don't show excess chars */ 
 
    white-space:nowrap;   /* force single line */ 
 
    vertical-align: bottom; 
 
}
<div id="test1"> 
 
    <div id="test2">long text standing in here</div> 
 
    <div id="test3">more important text in here</div> 
 
</div>

+0

這些被動態檢索到的內容是什麼? – Swellar

+0

是的,他們不在數據庫中 – naja08

+0

給你空間是什麼意思?他們是否都想留在一條線上,並且你想怎麼處理溢出? – Pete

回答

4

您可以使用Flexbox,就和第一個孩子的div設置overflow: hiddentext-overflow: ellipsis

div { 
 
    border: 1px solid black; 
 
} 
 
#test1 { 
 
    width: 250px; 
 
    display: flex; 
 
} 
 
#test1 > div { 
 
    white-space: nowrap; 
 
} 
 
#test2 { 
 
    overflow: hidden; 
 
    text-overflow: ellipsis; 
 
}
<div id="test1"> 
 
    <div id="test2">long text standing in here</div> 
 
    <div id="test3">more important text in here</div> 
 
</div>

1

如果您有支持的瀏覽器可以支持flexbox,去Flexbox的。

我做了一些示例html給你,告訴你它做了什麼。 這裏神奇的CSS屬性:display: flexflex-direction: rowflex: 1,和flex-basis: 200px

.container 
 
{ 
 
    display: flex; 
 
    flex-direction: row; 
 
} 
 
.container > .item 
 
{ 
 
    flex: 1; 
 
    
 
    -o-text-overflow: ellipsis; /* Opera */ 
 
    text-overflow: ellipsis; /* IE, Safari (WebKit) */ 
 
    overflow:hidden;    /* don't show excess chars */ 
 
    white-space:nowrap;   /* force single line */ 
 
    
 
    /* this just added for better view */ 
 
    border: 1px solid black; 
 
    padding: 5px; 
 
    box-sizing: border-box; 
 
} 
 
.container > .item-2 
 
{ 
 
    flex: 2; 
 
    flex-basis: 200px; 
 
}
<div class="container"> 
 
    <div class="item item-1"> 
 
    long text standing in here 
 
    </div> 
 
    <div class="item item-2"> 
 
    more important text in here 
 
    </div> 
 
</div>

+0

非常感謝那個偉大的答案!不幸的是,如果我將容器更改爲flex,我將殺死父級的css:/! 它有點棘手,因爲我使用的是MDL-框架(我濫用MDL芯片位) – naja08

+0

@ naja08你可以在#物品1,然後充當容器內加一個孩子。那應該把#item1作爲普通的容器(就像現在這樣),但只是在其中添加一個flexbox「容器」。這會起作用嗎? – DoXicK