Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove leading whitespace from whitespace: pre element

Tags:

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.

Output

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?

like image 291
Masked Man Avatar asked Jun 28 '13 13:06

Masked Man


People also ask

How do I remove a space from a pre tag?

white-space: normal; removes all the spaces. I had to use white-space: pre-line; as that removed only the spaces from the beginning.

What is whitespace pre wrap?

pre-wrap. Whitespace is preserved by the browser. Text will wrap when necessary, and on line breaks.

What does white space Nowrap do?

nowrap : Multiple whitespaces are collapsed into one, but the text doesn't wrap to the next line.


2 Answers

Check out the answer to this very similar question:

.mystyle:first-line {     line-height: 0px; } 

Might require a modern-ish browser, though.

like image 76
jwd Avatar answered Sep 27 '22 19:09

jwd


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.

like image 44
tom10271 Avatar answered Sep 27 '22 19:09

tom10271