I want to use a custom style for code snippets in my blog. I defined the following style:
mystyle { background: #C3FFA5; border: solid 1px #19A347; color: #191919; display: block; font-family: monospace; font-size: 12px; margin: 8px; padding: 4px; white-space: pre; }
I use it as follows:
<mystyle> int main() { cout << "Hello World" << endl; } </mystyle>
This gives the following output. I have tried on Firefox and Google Chrome.
I want to remove the extra line at the start of the block. Obviously, I understand where the newline comes from, and that I can use <mystyle>int main() {
instead. If I use <pre>
instead of <mystyle>
, there is no extra newline, so is it possible to do this with my custom style too?
white-space: normal; removes all the spaces. I had to use white-space: pre-line; as that removed only the spaces from the beginning.
pre-wrap. Whitespace is preserved by the browser. Text will wrap when necessary, and on line breaks.
nowrap : Multiple whitespaces are collapsed into one, but the text doesn't wrap to the next line.
Check out the answer to this very similar question:
.mystyle:first-line { line-height: 0px; }
Might require a modern-ish browser, though.
Adjust margin-top to whatever line-height you have set.
.text { margin-top: -1em; white-space: pre-line; }
This works for FF too, which :first-line hack can't fix.
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