Skip to main content

How to format a long <pre> HTML block suitable for width limited device such as iPhone


I have a long pre block, I want it to be view in mobile browsers, such as iPhone



e.g.




a very long pre block start here here here here here here here here here here
2nd line with intent
3rd line with intent



How to make make the wordwrap, but keep the indentations?




e.g.
a very long pre block start here here here here here here
here here here here
2nd line with intent
3rd line with intent



I don't want to have scrollbar for mobile device, so better to have some way to auto word-wrap the sentence.



The most similar method I have tried is to use CSS




pre {
white-space: pre-line;
}



But not exactly as I want as demonstrated above.



Updated: Link: http://pastehtml.com/view/bisxytioa.html


Source: Tips4all
Source: Tips4allSource: CCNA FINAL EXAM

Comments

  1. Here's a way to do it with Javascript. This goes through the <pre> and wraps each section in a <div> with padding-left equal to the number of spaces of indentation. In the demo I made the size of the <pre> the size of an iPhone screen to demonstrate the wrapping.

    Demo: http://jsfiddle.net/ThinkingStiff/v2JTR/

    Script:

    var pre = document.getElementById( 'pre' ),
    sections = pre.textContent.trim().split( '\n' ),
    paddingPerChar = 9;

    for( var index=0, html='', padding=0; index < sections.length; index++ ) {
    padding = ( sections[index].length - sections[index].trim().length ) * paddingPerChar;
    html += '<div style="padding-left:' + padding + 'px;">' + sections[index].trim() + '</div>';
    };

    pre.innerHTML = html;


    HTML:

    <pre id="pre">
    1. a very long pre block start here here here here here here here here here here
    A. 2nd line with indent long pre block start here here here here here here here here here
    a. 3rd line with indent
    B. 4th line th indent long pre block start here here here here here here here her
    C. 5th line
    2. 6th Line
    </pre>


    CSS:

    pre {
    border: 1px solid black;
    height: 460px;
    width: 320px;
    white-space:pre-wrap;
    }


    Output:

    ReplyDelete
  2. Not the most ideal solution, and probably not the solution you were looking for, but it's a solution none the less that does the job. It uses jQuery to replace the pre block with a list, as lists preserve indents on text wrapping.

    http://pastehtml.com/view/bj4d0az5r.html

    ReplyDelete
  3. Or you could use PHP (or anything like that). For example:

    Detect what browser / platform has been used:

    $browser = get_browser(null, true);
    print_r($browser);


    And for mobile OS's you could use an if/else statement with a wordwrap function to break and display your content. For example:

    <?php
    $text = "A very long woooooooooooord.";
    $newtext = wordwrap($text, 8, "\n", true);

    echo "$newtext\n";
    ?>

    ReplyDelete
  4. Wordwrap the content of the pre so that you do not exceed the fixed with.

    pre {
    width:100%;
    word-wrap:break-word;
    }


    this may fix your problem.

    you could also change width to a fixed with no wider than the iphone browser. The key is to wrap the text though so that it does not force the width of the housing element to be larger than the screen.

    ReplyDelete
  5. This works in every mobile browser I have tried them all on iOS & Android if you want screen shots let me know. All that is needed is one css tag

    pre {
    word-wrap:break-word;
    }


    PRE will fill its container automatically. As long as your container is sized to the width of the screen and has no overflow this works perfectly (preserves indents across word-wrapped line-breaks).

    ReplyDelete
  6. I’m afraid there’s no way to do it CSS, because CSS has no “knowledge” of things like the actual textual content of an element, like the leading spaces. So some different approach is needed. If you have control over the markup, you could replace the pre element by a div element containing single-line div elements with class attributes corresponding to the desired indentation levels. Then you just specify suitable left margins. Demo:

    http://pastehtml.com/view/bjbgnb1v3.html

    (The demo sets the left margin in ch units but uses a setting in em units as a reasonable backup. For a monospace font, one em is close to the width of two characters.)

    This doesn’t handle all the features of pre so if multiple whitespace inside a line needs to be preserved, add white-space: pre-wrap.

    An even more logical approach would be to use div elements nested so that the depth of nesting corresponds to the level of indentation. This would make the stylesheet simple but the markup would look awkward.

    If you cannot control the HTML markup, you may need to do things client-side (convert the pre element to other elements), similarly to the code in AncientMan’s suggestion.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex