I have some code inside an HTML document. The code itself is not important – I've used lorem ipsum to make this clear.
<pre><code>Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed sit amet diam sit amet sem accumsan faucibus ac in arcu.
Quisque varius, erat vel euismod ornare, libero orci laoreet velit, at lobortis sem nisl et eros.</code></pre>
I've applied white-space: pre-wrap to the code block to force long lines to wrap as necessary. I'd like to know whether it's possible to indent the wrapped portion of the wrapped lines, to give something like this:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed sit amet diam sit amet sem accumsan faucibus ac in arcu.
Quisque varius, erat vel euismod ornare, libero orci laoreet velit,
at lobortis sem nisl et eros.
6 Answers 6
It is kind of possible... I'm not using using the <pre> and <code> tags and I'm not sure how important these tags are to you... but I've been able to get the style you're looking for and mimick the formatting as best as I could. Check it out.
CSS
div {
margin-left:24px;
width:400px;
}
p {
font-family: "Courier New", Courier, monospace;
font-weight: normal;
font-style: normal;
font-size: 13px;
margin:0 28px;
text-indent: -28px;
}
HTML
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Sed sit amet diam sit amet sem accumsan faucibus ac in arcu.</p>
<p>Quisque varius, erat vel euismod ornare, libero orci laoreet velit, at lobortis sem nisl et eros.</p>
</div>
Take a look at this SO question and some solutions that have come from it. It is relevant to your question. It might be worth your time to take a look :)
I hope this helps!
11 Comments
Unfortunately after much searching I've come to believe that this is currently impossible using CSS alone. What's required is a pseudo-element for each "line" (text matching /^.*$/m), which would enable the indentation of lines beyond the first to be controlled via CSS.
I raised this issue on the www-style mailing list. fantasai's responses are promising, particularly the suggestion that the text-indent property could be extended to allow text-indent: 2em hanging each-line.
3 Comments
<l>line</l> as a replacement for <br /> but it fizzled out for want of an upgrade path.text-indent: 2em hanging each-line seems to only apply to <p> tags, and not <code> elements. Maybe someday though!text-indent: -2em;
padding-left: 2em;
2 Comments
I'm not sure if this works in <pre>, but it looks promising.
2 Comments
Not currently possible just with CSS, but with the help of a scripting language ...
PHP
echo '<pre id="the_pre_id"><div>'.str_replace("\n",'</div><div>',$text).'</div>';
or JavaScript
var el = document.getElementById('the_pre_id');
el.innerHTML='<div>'+el.innerHTML.replace(/\n/g, '</div><div>')+'</div>';
Note, you only need to choose one of the above snippets. Both accomplish the same thing.
We pollute the markup (non-semantic tags), but it allows us to create per-line style rules:
CSS
pre{
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
pre > div {
padding-left: 1em;
text-indent: -1em;
}
And we have exactly the effect you're looking for ...
Result
Lorem ipsum dolor sit amet, consectetur
adipiscing elit.
De malis autem et bonis ab iis animalibus,
quae nondum depravata sint, ait optime
iudicari.
Quae cum praeponunt, ut sit aliqua rerum
selectio, naturam videntur sequi; Quasi
ego id curem, quid ille aiat aut neget.
1 Comment
that is forced into this.This article has a solution using the first-of-type pseudo selector that seems to work so far for me: http://thenewcode.com/50/Classic-Typography-Effects-in-CSS-Hanging-Indent
html{
margin-left: 100px;
}
p {
margin: 6em inital;
width: 300px;
}
p:first-of-type {
text-indent: -4em;
}
<p>Leverage agile frameworks to provide a robust synopsis for high level
overviews. Iterative approaches to corporate strategy foster collaborative
thinking to further the overall value proposition.</p>
<p>Bring to the table win-win survival strategies to ensure proactive domination.
At the end of the day, going forward, a new normal that has evolved from
generation X is on the runway heading towards a streamlined cloud solution.
User generated content in real-time will have multiple touchpoints for offshoring.</p>