2016-06-13 62 views
0

我正在创建一个仅在需要时才会显示的横幅。我正在研究设计过程,并想知道是否有一种技术可以实现目标。如果短语/句子不跳转到下一行,我该如何设置文本左对齐 - 离开?

所以我只需要对齐文本为中心,如果短语/句子可以适合作为一个句子,并不跳转到下一行?我将属性text-align设置为中心,但当文本很长并跳到下一行时,则第2行上的文本会居中,而我不希望发生这种情况。所以我想知道是否有像使用CSSHTML这样的东西?

只是寻求一些想法或提示。

Here is the code

回答

0

你可以使用CSS属性white-space:nowrap使DIV最初不换行。

然后,您可以确定文本是否使用scrollWidthinnerWidth属性环绕。您可以使用jquery重置nowrap属性,以便该元素按照您希望的方式显示。

CSS

#innerNotification{ 
    font-family: Arial, Times New Roman; 
    font-size: 8pt; 
    font-weight: bold; 
    color: #9F6000; 
    padding-top: 1%; 
    padding-bottom:1%; 
    text-align: center; 
    white-space:nowrap; 
} 

JS

if ($('#innerNotification')[0].scrollWidth > $('#innerNotification').innerWidth()) { 
    $('#innerNotification').css('white-space','normal'); 
    $('#innerNotification').css('text-align','left'); 
} 

这里是一个工作Fiddle

+0

谢谢尼尔。这有帮助! – user3818602

相关问题