2016-07-25 76 views

回答

3

您應該使用等寬字體,然後。

比如,ADD font-family:"Menlo";(在一個堆棧溢出使用代碼塊:)

body{ 
 
    font-family: Menlo, monospace; 
 
}
iiiii iiiii iiiii<br> 
 
wkerh werce kjhvg

+0

嗯,我認爲這實際上是一個非常糟糕的例子。在任何Windows機器上默認情況下都不知道「Menlo」。而且你絕對不應該使用不帶引號的自定義字體名稱,而不是單獨使用。總是爲通用字體類型添加回退,如:'font-family:「Menlo」,monospace;'。 – eisbehr

+0

@eis我會說它已經非常通用,但可以。 – nicael

3

我會知道的唯一方法是將monospace字體添加到您的輸出元素:

div { 
    font-family: monospace; 
} 

Working example.

0
<div class="test"> 
    chelsea chelsea chelsea chelsea chelsea chelsea chelsea 
</div> 

<div class="test"> 
    iiiii iiiii iiiii iiiii iiiii iiiii iiiii iiiii 
</div> 

在CSS:

.test { 
    width: 100px; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
} 

我更新了你的提琴:https://jsfiddle.net/Lwpv1frd/1/

刪除的JavaScript。我認爲這可以幫助你。取決於你將如何使用它。乾杯!

0

我看到這張貼在另一個SO post,我認爲這可能會有所幫助。 你可以使用任何你喜歡的字體。

$.fn.strech_text = function(){ 
 
    var elmt   = $(this), 
 
     cont_width = elmt.width(), 
 
     txt   = elmt.html(), 
 
     one_line  = $('<span class="stretch_it">' + txt + '</span>'), 
 
     nb_char  = elmt.text().length, 
 
     spacing  = cont_width/nb_char, 
 
     txt_width; 
 
    
 
    elmt.html(one_line); 
 
    txt_width = one_line.width(); 
 
    
 
    if (txt_width < cont_width){ 
 
     var char_width  = txt_width/nb_char, 
 
      ltr_spacing = spacing - char_width + (spacing - char_width)/nb_char ; 
 
    
 
     one_line.css({'letter-spacing': ltr_spacing}); 
 
    } else { 
 
     one_line.contents().unwrap(); 
 
     elmt.addClass('justify'); 
 
    } 
 
}; 
 

 

 
$(document).ready(function() { 
 
    $('.stretch').each(function(){ 
 
     $(this).strech_text(); 
 
    }); 
 
});
p { 
 
    background:gold; 
 
    width:300px; 
 
    padding: 10px 0; 
 
} 
 
a{text-decoration:none;} 
 
.stretch_it{ 
 
    white-space: nowrap; 
 
} 
 
.justify{ 
 
    text-align:justify; 
 
} 
 

 
.one{font-size:10px;} 
 
.two{font-size:20px;} 
 
.three{font-size:30px;} 
 
.four{font-size:40px;} 
 
.arial{font-family:arial;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
<p class="stretch one">Stretch me</p> 
 
<p class="stretch two">Stretch me</p> 
 
<p class="stretch three">Stretch <a href="#">link</a></p> 
 
<p class="stretch two">I am too slong, an I would look ugly if I was displayed on one line so let me expand to several lines and justify me.</p> 
 
<p class="stretch arial one">Stretch me</p> 
 
<p class="stretch arial three">Stretch me</p> 
 
<p class="stretch arial four">Stretch me</p>

相關問題