Is it possible to wrap indented code on a web page the way it's done in a code editor? See the screenshot comparison below to better understand what I mean:
pre-wrap
on a web page:
Wrapping of indented lines in a code editor:
What I am implying is that the indented lines maintain indentation even after wrapping. This doesn't seem to happen on web pages. Is there a CSS property that does this? (JavaScript would be fine too.)
NOTE: I am not talking about code highlighting here. It's about indentation of wrapped lines.
If this matters — this is how I am showing code blocks on my web pages:
<pre><code>if ( is_page() && $post->post_parent ) {
return $post->post_parent;
} else {
return false;
}
</code></pre>
...and the white-space: pre-wrap;
style is applied on pre
tag.
You can indent elements by moving them two spaces to the right. This will make your code more readable by other developers and shows the relationship between the child and parent HTML elements.
HTML code does not need to be indented, and all browsers and search engines ignore indentation and extra spacing. However, for any human reader, it's a good idea to indent your text because it makes the code easier to scan and read.
Indentation is extremely helpful for a few reasons: Helps us understand what elements are inside other elements. Helps us see validation errors more clearly. Helps maintain our code because it'll be more obvious if we haven't look at it in a while.
<div>
).marginLeft
(or paddingLeft
, if you wish) property to the product of the size of a single space and the number of prefixed spaces./**
* Auto-indent overflowing lines
* @author Rob W http://stackoverflow.com/u/938089
* @param code_elem HTMLCodeElement (or any element containing *plain text*)
*/
function autoindent(code_elem) {
// Grab the lines
var textContent = document.textContent === null ? 'textContent' : 'innerText';
var lines = code_elem[textContent].split(/\r?\n/),
fragment = document.createDocumentFragment(),
dummy, space_width, i, prefix_len, line_elem;
// Calculate the width of white space
// Assume that inline element inherit styles from parent (<code>)
dummy = document.createElement('span');
code_elem.appendChild(dummy);
// offsetWidth includes padding and border, explicitly override the style:
dummy.style.cssText = 'border:0;padding:0;';
dummy[textContent] = ' ';
space_width = dummy.offsetWidth;
// Wipe contents
code_elem.innerHTML = '';
for (i=0; i<lines.length; i++) {
// NOTE: All preceeding white space (including tabs is included)
prefix_len = /^\s*/.exec(lines[i])[0].length;
line_elem = fragment.appendChild(document.createElement('div'));
line_elem.style.marginLeft = space_width * prefix_len + 'px';
line_elem[textContent] = lines[i].substring(prefix_len);
}
// Finally, append (all elements inside) the fragment:
code_elem.appendChild(fragment);
}
white-space:pre-wrap
)code_elem
!) to the prefixed white space, then calculate the width using .offsetWidth
.autoindent
function assumes that the contents of a element is plain text.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With