2017-06-14 49 views
1

我使用flexbox和margin:auto將元素在頁面中水平和垂直居中放置。是否可以保持這個元素的內容在兩個軸中居中,但允許文本在展開時換行?允許Flexbox中的中心項目包裝

在下面的演示中,我一直使用flexbox添加文本以顯示元素很快超過瀏覽器窗口的寬度。我想包裝文本並將所有內容保存在屏幕上。建議?

// \ \ \ \ \ \ \ \ \ TYPE WRITER EFFECT////////////////// /// 
 

 
function type(i, t, ie, oe){ 
 
    input = document.getElementById(ie).textContent; 
 
    document.getElementById(oe).textContent += input.charAt(i); 
 
    setTimeout( 
 
     function(){ 
 
     (
 
      (i < input.length - 1) ? type(i+1, t, ie, oe) : false 
 
     ); 
 
     }, 
 
     t 
 
    ); 
 
} 
 

 
type(0, 100, 'input', 'output');
/* \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ INITIAL /////////////////////////////// */ 
 

 
html, 
 
body { 
 
    height: 100%; 
 
} 
 

 
body { 
 
    display: flex; 
 
    flex-wrap: wrap; /* also...this isn't working */ 
 
    margin: 0; 
 
    font-family: sans-serif; 
 
} 
 

 
p { 
 
    margin: auto; 
 
    text-align: center; 
 
} 
 

 
p:nth-child(2) { 
 
    display: none; 
 
}
<p id="output"></p> 
 

 
<!-- \\ \ VERY LONG STRING OF TEXT TO EXTEND PAST SCREEN /////////// // --> 
 

 
<p id="input"> 
 
So&nbsp;I&nbsp;have&nbsp;a&nbsp;long&nbsp;string&nbsp;of&nbsp;text&nbsp;with&nbsp;no&nbsp;spaces.&nbsp;Here&nbsp;I'm&nbsp;using&nbsp;non&#8209;breaking&nbsp;space&nbsp;character&nbsp;entities&nbsp;to&nbsp;simulate&nbsp;that.&nbsp;The&nbsp;problem&nbsp;is&nbsp;I'm&nbsp;using&nbsp;flexbox&nbsp;to&nbsp;center&nbsp;the&nbsp;effected&nbsp;element&nbsp;here,&nbsp;but&nbsp;I&nbsp;don't&nbsp;want&nbsp;all&nbsp;the&nbsp;text&nbsp;to&nbsp;stay&nbsp;on&nbsp;one&nbsp;line&nbsp;like&nbsp;this.&nbsp;There's&nbsp;no&nbsp;spaces,&nbsp;I'm&nbsp;using&nbsp;flexbox,&nbsp;and&nbsp;I&nbsp;want&nbsp;it&nbsp;to&nbsp;wrap.&nbsp;Help? 
 
</p>

PS:flex-wrap: wrap似乎並沒有做的伎倆。 (它在我的代碼中)。

回答

3

使用word-break: break-all因爲這只是一個非常長的內聯字符集,字符之間沒有實際的空格。

// \ \ \ \ \ \ \ \ \ TYPE WRITER EFFECT////////////////// /// 
 
function type(i, t, ie, oe){ 
 
    input = document.getElementById(ie).textContent; 
 
    document.getElementById(oe).textContent += input.charAt(i); 
 
    setTimeout( 
 
     function(){ 
 
     (
 
      (i < input.length - 1) ? type(i+1, t, ie, oe) : false 
 
     ); 
 
     }, 
 
     t 
 
    ); 
 
} 
 

 
type(0, 100, "input", "output");
/* \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ INITIAL /////////////////////////////// */ 
 

 
html, 
 
body { 
 
    height: 100%; 
 
} 
 

 
body { 
 
    display: flex; 
 
    flex-wrap: wrap; /* also...this isn't working */ 
 
    margin: 0; 
 
    font-family: sans-serif; 
 
} 
 

 
p { 
 
    margin: auto; 
 
    text-align: center; 
 
    word-break: break-all; 
 
} 
 

 
p:nth-child(2) { 
 
    display: none; 
 
}
<p id="output"></p> 
 

 
<!-- \\ \ VERY LONG STRING OF TEXT TO EXTEND PAST SCREEN /////////// // --> 
 
<p id="input"> 
 
So&nbsp;I&nbsp;have&nbsp;a&nbsp;long&nbsp;string&nbsp;of&nbsp;text&nbsp;with&nbsp;no&nbsp;spaces.&nbsp;Here&nbsp;I'm&nbsp;using&nbsp;non&#8209;breaking&nbsp;space&nbsp;character&nbsp;entities&nbsp;to&nbsp;simulate&nbsp;that.&nbsp;The&nbsp;problem&nbsp;is&nbsp;I'm&nbsp;using&nbsp;flexbox&nbsp;to&nbsp;center&nbsp;the&nbsp;effected&nbsp;element&nbsp;here,&nbsp;but&nbsp;I&nbsp;don't&nbsp;want&nbsp;all&nbsp;the&nbsp;text&nbsp;to&nbsp;stay&nbsp;on&nbsp;one&nbsp;line&nbsp;like&nbsp;this.&nbsp;There's&nbsp;no&nbsp;spaces,&nbsp;I'm&nbsp;using&nbsp;flexbox,&nbsp;and&nbsp;I&nbsp;want&nbsp;it&nbsp;to&nbsp;wrap.&nbsp;Help? 
 
</p>

+0

嗯......我知道這一點。大聲笑。謝謝。 – savant

+0

@savant哈哈,你打賭:) –

+0

我嘗試過'換行:斷字'。 – savant