br line-height in safari and chrome leaving gap

2019-02-09 22:54发布

问题:

In my HTML I have a div and inside the div I have different vertical spacing between lines of text. I achieve this by using breaks and defining the height, i.e. <br /><br class="height5" /> or height2 or height10 or whatever.

In my stylesheet I define it like:

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 (like 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!

回答1:

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

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


回答2:

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>


回答3:

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


回答4:

Try this:

br { line-height: 1em; }

or:

br { margin-top: 2em; }


回答5:

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

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