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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...