最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make text in paraghapghs in div overflow instead of new lines - Stack Overflow

programmeradmin3浏览0评论

Say I have several pretty longs lines in a paragraph:

<div style="overflow: auto"> 
    <p> 
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccccccc
    </p> 
    <p> 
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccccccc
    </p> 
</div>

and window width less that width of the pahagraph (a's + b's + c's). Currently scroll will appear only if width of any of ponents (a, b, or c) is more that width of the window and over ponents are carried to the new lines. I want contents of each paragraph appear on the same line with a scroll. How can I treat each paraghaph as a string, so contents of the paragraph is treated like a single line?

Say I have several pretty longs lines in a paragraph:

<div style="overflow: auto"> 
    <p> 
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccccccc
    </p> 
    <p> 
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccccccc
    </p> 
</div>

and window width less that width of the pahagraph (a's + b's + c's). Currently scroll will appear only if width of any of ponents (a, b, or c) is more that width of the window and over ponents are carried to the new lines. I want contents of each paragraph appear on the same line with a scroll. How can I treat each paraghaph as a string, so contents of the paragraph is treated like a single line?

Share Improve this question asked Apr 3, 2013 at 9:06 Sergei GSergei G 1,5704 gold badges24 silver badges44 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 14

With CSS you can do:

p {
    white-space: nowrap;
    overflow: auto
}

This make the spaces non-breaking, so the paragraph will all be on one line.

You can set white-space:nowrap and overflow:auto property of css. You can assign a class to paragraph tag for which you want text on single line.

CSS

.className {
    white-space: nowrap;
    overflow: auto;
}

Html

<div style="overflow: auto"> 
    <p class="className"> 
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccccccc
    </p> 
    <p class="className"> 
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccccccc
    </p> 
</div>

Either add styles as below

p{
    overflow:auto;
    white-space: nowrap;
}

or use jQuery as below

$('p').css('overflow','auto');
$('p').css('white-space','nowrap');

Here is a live fiddle example http://jsfiddle/mayooresan/qmCwL/

发布评论

评论列表(0)

  1. 暂无评论