html - Parametrize the style="margin-top:Xem" to set any X -


i want build plaintext read file. file has look:

line1  line3 

line2 totally empty, without space. html file looks like

<!doctype html public><html> <head> <style type='text/css'>#editor { white-space: pre; }</style> </head> <body style="font-family: 'segoe print'; font-size:14pt">  <p id='editor', style=" margin-top:0px; margin-bottom:0px; margin-left:0px">line1</p> <p id='editor', style=" margin-top:0px; margin-bottom:0px; margin-left:0px"></p> <p id='editor', style=" margin-top:0px; margin-bottom:0px; margin-left:0px">line3</p>  </body></html> 

the code lines "p id=..." important. middle line of them ignored, there 2 lines shown. thats because there no text before closing /p, right? of course enter " " (space), thats dirty solution...

i've found half-way solution inserting "margin-top:xem" instead of "margin-top:0px" in lower line. x "1". question is:

how can put other value, e.g. 4, x when program figured out there 4 empty lines?

edit: here tried. nothing changed...

<!doctype html public><html> <head> <style type='text/css'>p.editor { white-space: pre; min-height:1em; }</style> </head> <body style="font-family: 'segoe print'; font-size:14pt">  <p class='editor' style=" margin-top:0em; margin-bottom:0px;">line1</p> <p class='editor' style=" margin-top:0em; margin-bottom:0px;"></p> <p class='editor' style=" margin-top:0em; margin-bottom:0px;">line3</p>  </body></html> 

a few points:

  1. ids must unique. change use classes instead.
  2. you need spaces separate element attributes, not commas.
  3. you should use css rather inline styling.

if want a p element display without text included, give minimum height.

<p class="editor">line 1</p> <p class="editor"></p> <p class="editor">line 3</p> 
p.editor {     white-space: pre;     margin:0;     min-height:16px; } 

by default p elements default height of content within. no content, there no height. specifying minimum height means empty p element have height regardless of whether has content or not.

jsfiddle example.

if want empty line copy-able, however, you'll need modify html use non-breaking space (&nbsp;) instead of leaving elements empty:

<p class="editor">line 1</p> <p class="editor">&nbsp;</p> <p class="editor">line 3</p> 

with wouldn't need minimum height non-breaking space adheres font size of element.

jsfiddle example.


Comments