25

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?

Bojangles
  • 99,427
  • 50
  • 170
  • 208
Masked Man
  • 1
  • 7
  • 40
  • 80

10 Answers10

26

Check out the answer to this very similar question:

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

Might require a modern-ish browser, though.

Community
  • 1
  • 1
jwd
  • 10,837
  • 3
  • 43
  • 67
17

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.

tom10271
  • 4,222
  • 5
  • 33
  • 62
17

When we use white-space: pre-wrap, then when the page renders, it also takes space from your html file. So suppose we have:-

<div style="white-space:pre-wrap"> <!-- New Line -->
   <!-- 2 space --> This is my text
</div>

The text will render in this way:-

<leading line>     
<2 spaces >This is my text

Hence we should HTML like this:-

<div style="white-space:pre-wrap">This is my text</div>
prashantsahni
  • 2,128
  • 19
  • 20
  • Dude, your answer is just awesome. At first I did not understand what you were trying to explain.. you should have explained a bit.. – Thameem Apr 22 '21 at 17:43
8

Add the style to the <pre> tag, using a class. For example (trying to keep it simple here).

<pre class="console">
    // Some code here to be styled.
</pre>

<pre class="some-other-style">
    // Some code here to be styled.
</pre>

Then your CSS looks like this:

pre.console {
    color: #fff;
    background-color: #000;
}
pre.some-other-style {
    color: #f00;
    background-color: #fff;
}

If it doesn't do what you want then I'm confused by your question, just comment and I'll remove the answer.

naththedeveloper
  • 4,503
  • 6
  • 36
  • 44
  • 4
    This doesn’t really address the questuon as asked, since it says: “If I use `
    ` instead of ``, there is no extra newline, so is it possible to do this with my custom style too?” In practical terms, however, the answer points to the right direction. There are special parsing rules for `pre` elements, causing an initial line break to be ignored. Imitating this for other elements is kludgy (it could be done using a negative top margin, but then you would need to assume that the content really begins with a line break).
    – Jukka K. Korpela Jun 28 '13 at 18:54
3

This is for you who want to remove the trailing space only from the first line of the formatted output

instead of writing the code like this

<pre> 
    This is my code  </pre>

write it like this

<pre>This is my code</pre>

or in case you are using a <div> tag as shown below

<div style="white-space:pre-wrap"> 
    This is my code 
</div>

write it like this

<div style="white-space:pre-wrap">This is my code</div>
David Buck
  • 3,752
  • 35
  • 31
  • 35
Chidimma
  • 59
  • 2
2

Code formatting is at the essence here, make sure each line start at the first character of that line:

<pre class="code">
int main() {
    cout << "Hello World" << endl;
}
</pre>

The following CSS will suffice:

pre.code
{
    background: #C3FFA5;  
    border: solid 1px #19A347;  
    color: #191919;  
    display: block;  
    font-family: monospace;  
    font-size: 12px;  
    margin: 8px;  
    padding: 4px;
}

Read this article on whitespace, and the following on how to 'fight it'. Although the last article discusses whitespace between inline elements, the formatting solution relates to your issue.

NeilC
  • 1,380
  • 1
  • 17
  • 36
1

How about

<mystyle>into main() {
    // ...
}</mystyle>

No white space before or after...

rodrigo-silveira
  • 12,607
  • 11
  • 69
  • 123
  • The OP already stated that they know they can do this: _"Obviously, I understand where the newline comes from, and that I can use `int main() {` instead."_. This is not applicable... – War10ck Jun 28 '13 at 13:50
  • 2
    @War10ck Too bad the OP is looking for the impossible. A newline is the first character within the element and `whitespace: pre` is honoring it. – cimmanon Jun 28 '13 at 14:20
1

Just fix your template, man. Thats easiest way:

<mystyle>int main() {
  cout << "Hello World" << endl;
}</mystyle>
Zorin V.
  • 11
  • 2
1

Not a pure CSS solution but works for me:

<script>
  document.querySelectorAll('pre').forEach((e) => {
    e.innerHTML = e.innerHTML.trim();
  })
</script>
A. Genedy
  • 578
  • 4
  • 11
  • This answer really fixes the first line thing instead of hacking the style. ```window.addEventListener('DOMContentLoaded', function() { console.log('La página ha terminado de cargarse!!'); document.querySelectorAll('code').forEach((e) => { e.innerHTML = e.innerHTML.trim(); }) });``` – DiegoJArg Nov 30 '22 at 01:35
0

This solution works for me when dealing with white-space: pre-line.

Add span inside white-space element.

<p class="whitespace-pre-line">
   <span>Your text goes here</span>
</p>
kusiaga
  • 673
  • 1
  • 7
  • 18