2014-10-08 57 views
0

我有一個有趣的挑戰,讓兩個內聯文本元素以特定方式運行。這裏是我的參數:內聯divs:收縮一個,隱藏另一個的溢出

  • 左側將在寬度不一,從父寬度的約30-60%的正文
  • 右側的補充文本與父寬度的20-120%差異很大。
  • 父寬度是響應式的,可以收縮到全寬的75%。絕對定位不會。
  • 每當這兩個項目不適合,我想正文保持完全可見(沒有省略號)和補充文本隱藏溢出:
    • 溢出:隱藏
    • 文本溢出:省略號

兩個CSS和HTML是完全可以修改,但我寧願如果HTML保持語義。如果有這樣一個好方法,請隨意使用JS/jQuery。

http://jsfiddle.net/gksfwozz/1/

.outer { 
 
    width: 200px; 
 
    background-color: skyblue; 
 
    border: 3px solid skyblue; 
 
    display: flex; 
 
    justify-content: space-between; 
 
} 
 
.left { 
 
    background-color: salmon; 
 
    white-space: nowrap; 
 
    overflow: hidden; 
 
    text-overflow: ellipsis; 
 
} 
 
.right { 
 
    background-color: blanchedalmond; 
 
    white-space: nowrap; 
 
    overflow: hidden; 
 
    text-overflow: ellipsis; 
 
}
<div class="outer"> 
 
    <div class="left"> 
 
     MAIN TEXT 
 
    </div> 
 
    <div class="right"> 
 
     additional info sdjkfhsdf 
 
    </div> 
 
</div>

回答

1

我改變你的CSS一點點。這裏是一個fiddle

對於.left我添加了浮動和最大寬度(因爲你將它推到容器的60%),我刪除了overflow:hidden因爲你總是想要它。

.left { 
background-color: salmon; 
white-space: nowrap; 
max-width: 60%; 
float: left; 
} 

我刪除了顯示:flex from .outter。我認爲這是你想要的。

+0

非常好!它的確回答了我的問題,儘管你的微小修改不是我想要的。但是,他們既不是你的錯,也不是相關的。感謝您解決我的問題!我會回答其他人回答2分鐘前你:( – 2014-10-08 20:39:29

+0

沒關係,重要的是你得到你想要的東西,但我是在另一個人前2分鐘回答!)現在我的回答是16分鐘前回答,但他的回答在14分鐘前回答,所以我是第一個,但無論如何! @ jonas.ninja;) – 2014-10-08 20:44:35

+0

你是對的!我弄糊塗了我的號碼。授予你! – 2014-10-08 20:55:24

1

.outer { 
 
    width: 200px; 
 
    background-color: skyblue; 
 
    border: 3px solid skyblue; 
 
    overflow:hidden; 
 
} 
 
.left { 
 
    float:left; 
 
    background-color: salmon; 
 
    white-space: nowrap; 
 
    overflow: hidden; 
 
    text-overflow: ellipsis; 
 
} 
 
.right { 
 
    background-color: blanchedalmond; 
 
    white-space: nowrap; 
 
    overflow: hidden; 
 
    text-overflow: ellipsis; 
 
}
What I have: 
 
<div class="outer"> 
 
    <div class="left"> 
 
     MSGDFGDFGDFDFGDFGDFGDFG 
 
    </div> 
 
    <div class="right"> 
 
     adfhdfgh 
 
    </div> 
 
</div>

http://jsfiddle.net/gksfwozz/2/

從這個鏈接How to get these two divs side-by-side?

+0

優秀!你的嵌入式代碼看起來不太好,但是你鏈接的JSfiddle就是這個技巧。有趣的是,你的答案與其他人的答案几乎相同。 – 2014-10-08 20:40:16

相關問題