2016-05-17 29 views
1

我使用一些文本構建絕對位置容器,最大寬度爲200px,所以文本應該包裝到200px,但它不起作用。在絕對位置容器中將文本包裝爲最大寬度

我無法設置固定的寬度,因爲如果文字較短,應該更小。

容器的相對位置父級爲「display:inline;」風格,(這種風格造成了問題)。

如何讓文字較長的容器填充200px?

這是小提琴:https://jsfiddle.net/m2wr9m06/3/

.showlist { 
 
    box-shadow: 5px 5px 20px #191919; 
 
    padding: 5px; 
 
    max-width: 200px; 
 
    position: absolute; 
 
    z-index: 1; 
 
    left: -5px; 
 
    font-family: "Century Gothic", Century, "Arial Black"; 
 
    font-size: 14px; 
 
} 
 
.flag { 
 
    color: #FF9900; 
 
    position: relative; 
 
    display: inline-block; /* remove this rule to see the expected result */ 
 
    margin: 5px; 
 
    font-size: 16px; 
 
}
<div class="flag"> 
 
    <div class="showlist" > 
 
    <div>asdf 4444 555555 666666 7 8 8888 9999 555 444</div> 
 
    <div>4 8 15 16 23 42</div> 
 
    <div>asdf foo bar </div> 
 
    </div> 
 
</div>

(刪除 「dispay:內聯;」 治看到預期的結果)

+0

呀......你不能這樣做,有絕對定位的div。 –

回答

1

我找到了一個解決方案,我希望它可以幫助別人,我增加了一個額外的容器爲200px;寬度:

<div class="flag"> 
<div style='width:200px;'> 
    <div class="showlist" > 
    <div>asdf 4444 555555 666666 7 8 8888 9999 555 444</div> 
    <div>4 8 15 16 23 42</div> 
    <div>asdf foo bar </div> 
    </div> 
</div> 
</div> 

小提琴:https://jsfiddle.net/m2wr9m06/8/

+0

我遇到了同樣的問題。我希望有另一個解決方案,除了添加一個div,但感謝您分享! – Alesana

0

上.flag更換display:inlinedisplay:inline-block

.flag { color:#FF9900; position:relative; display:inline-block; margin:5px; font-size:16px; }

+0

我需要「display:inline;」樣式,因爲其旁邊有一些其他容器 – stramin

+0

「display:inline-block」如何?然後? – nicolesmithweb

+0

不客氣!很高興能提供幫助:D只有原始的海報可以將答案標記爲解決方案。我的答案應該有一個灰色的複選標記供您選擇。 – nicolesmithweb

-1

相反的max-width:200px,將其更改爲width:200px;

+0

我無法設置固定寬度,因爲如果文字較短,應該較小。 – stramin

相關問題