Mike
Mike

Reputation:

br line-height in safari and chrome leaving gap

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!

Upvotes: 8

Views: 19655

Answers (5)

PriyankaNM
PriyankaNM

Reputation: 11

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

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

Upvotes: 1

user2881770
user2881770

Reputation: 49

Try this:

br { line-height: 1em; }

or:

br { margin-top: 2em; }

Upvotes: 1

SamuelC
SamuelC

Reputation: 219

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: " "} 

Upvotes: 1

anushr
anushr

Reputation: 3400

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

br {
  content: " ";
  display: block;
  margin: 1em;
}

Upvotes: 17

Jeremy Sullivan
Jeremy Sullivan

Reputation: 1005

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>

Upvotes: 3

Related Questions