8

In my HTML I have a <div>, and inside the <div> I need different vertical spacing between lines of text. I'm achieving this by using <br /> elements with a CSS class that corresponds to the amount of spacing I want.

For instance, for a 5px gap, I use a <br> with the height5 class:

<br /><br class="height5" />

Similarly, height2 and height10 and whatever exist for the same purpose.

The CSS classes are defined like so:

br.height2 {line-height:2px;}
br.height5 {line-height:5px;}

This is working in IE6+, FF2+, and Opera but for some reason there are huge gaps in Safari and Chrome (as if those two browsers are ignoring it and just applying regular breaks). I tried testing with larger line-heights like 20px or 30px and Safari and Chrome recognize those. They seem to be ignoring anything under 5-10 pixels.

Help? Thanks!

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48

5 Answers5

17

This worked on Chrome (the content attribute did the trick):

br {
  content: " ";
  display: block;
  margin: 1em;
}
anushr
  • 3,342
  • 3
  • 29
  • 50
  • Thanks for this. Styling the br was necessary because a rich-text editor I use would leave large gaps between texts. – Vael Victus Jan 12 '15 at 18:31
3

Those browsers might be reading your white space (carriage return, etc) and propping it open with a &nbsp;sort of value. I suggest using multiple div tags and style the divs with margin-bottom attributes of the space you want.

<div style="margin-bottom: 2px">content</div>
<div style="margin-bottom: 5px">content</div>
<div>content</div>
Jeremy Sullivan
  • 995
  • 1
  • 14
  • 25
  • 1
    yes,
    is almost like goto :) Some smart browsers can ignore font-sizes smaller than 9px to prevent users' eyes damage. And spaces between elements must be discribed in CSS as spaces, not as text line-brakes.
    – Sergei Kovalenko Jun 09 '09 at 08:30
1

Try this:

br { line-height: 1em; }

or:

br { margin-top: 2em; }
micnic
  • 10,915
  • 5
  • 44
  • 55
user2881770
  • 45
  • 1
  • 9
1

This worked for me for both firefox and chrome. Got the idea from @SamuelC and @anushr.

br{ 
    display: block;
    line-height: 0.1; 
    content: " ";
}
1

I know this is old, but my answer here is cross-browser without turning br into a block

/* line height can be set to whatever you want*/
br {line-height: 0.1; content: " "} 
SamuelC
  • 219
  • 1
  • 3
  • 13