2017-06-14 68 views
0

比方說,我有以下的div:JSFIDDLECSS文本流

<div> 
    <div class="first">Hello My name is </div> 
    <div class="second">Something else</div> 
</div> 

下一個最大寬度時,first部分和second部分斷裂客場挑戰下一行。有沒有辦法讓這些詞彼此相鄰?

+0

是否需要使用'display:flex'?因爲據我所見,將flex更改爲block或inline塊可以修復它 – Sagar

+0

https://jsfiddle.net/m800pthq/4/ – Aslam

回答

1

看答案下面

只需添加display:table;.genraldisplay:table-cell;white-space:nowrap;.first, .second

.general { 
 
    background: #ebeff1; 
 
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); 
 
    margin-bottom: 16px; 
 
    max-width: 205px; 
 
    display: table; /*Changed*/ 
 
} 
 
.first { 
 
    margin-right: 7px; 
 
} 
 

 
/*New Added*/ 
 
.first, .second{ 
 
    display: table-cell; 
 
    white-space: nowrap; 
 
} 
 
    
 
p.pp { 
 
    font-size: 24px; 
 
}
<p class="pp"> 
 
    When overflow, the words go to next line: Not good. 
 
</p> 
 
    <div class="first_sample general"> 
 
     <div class="first">Hello My name is </div> 
 
     <div class="second">something else</div> 
 
    </div> 
 
    
 
<p class="pp"> 
 
    If there is a space, then words simply flow to next each other: Good. 
 
</p> 
 
    <div class="second_sample general"> 
 
     <div class="first">Hello My name is </div> 
 
     <div class="second">something</div> 
 
    </div> 
 
    
 
<p class="pp"> 
 
    The look I am trying to achieve: 
 
</p> 
 
    <div class="third_sample general"> 
 
     <div class="first">Hello My name is something else </div>  
 
    </div>

1

只需在您的firstsecond divs中使用display: inlineinline-block

.general { 
 
    max-width: 200px; 
 
} 
 

 
.first, 
 
.second { 
 
    display: inline; 
 
}
<div class="first_sample general"> 
 
    <div class="first">Hello My name is </div> 
 
    <div class="second">something else</div> 
 
</div>

1

可以使用white-space:nowrap;屬性。

.general { 
 
    background: #ebeff1; 
 
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); 
 
    margin-bottom: 16px; 
 
    max-width: 205px; 
 
    display: flex; 
 
} 
 
.first { 
 
    margin-right: 7px; 
 
    white-space:nowrap; 
 
} 
 

 
.second { 
 
    white-space:nowrap; 
 
} 
 

 
p.pp { 
 
    font-size: 24px; 
 
}
<p class="pp"> 
 
    When overflow, the words go to next line: Not good. 
 
</p> 
 
    <div class="first_sample general"> 
 
     <div class="first">Hello My name is </div> 
 
     <div class="second">something else</div> 
 
    </div> 
 
    
 
<p class="pp"> 
 
    If there is a space, then words simply flow to next each other: Good. 
 
</p> 
 
    <div class="second_sample general"> 
 
     <div class="first">Hello My name is </div> 
 
     <div class="second">something</div> 
 
    </div> 
 
    
 
<p class="pp"> 
 
    The look I am trying to achieve: 
 
</p> 
 
    <div class="third_sample general"> 
 
     <div class="first">Hello My name is something else </div>  
 
    </div>

1
.general { 
    background: #ebeff1; 
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); 
    margin-bottom: 16px; 
    max-width: 405px; 
    display: inline-block; 
}