[html] Change <br> height using CSS

I have seen a question here about the same, but I can't get any of the answers to work (at least on Chrome).

This question is only for <br>, I know plenty of other techniques to change the height but in this case I can't change the HTML.

bla<BR><BR>bla<BR>bla<BR><BR>bla

CSS:

br {
    display: block;
    margin-bottom: 2px;
    font-size:2px;
    line-height: 2px;
}

Desired effect: smaller inter-line height.

The only thing I can get to work is display:none, but then all line break are removed.

Here's a fiddle for it using some of the techniques, but see that it renders the exact same as without any CSS.

This question is related to html css

The answer is


The line height of the <br> can be different from the line height of the rest of the text inside a <p>. You can control the line height of your <br> tags independently of the rest of the text by enclosing two of them in a <span> that is styled. Use the line-height css property, as others have suggested.

<p class="normalLineHeight">
  Lots of text here which will display on several lines with normal line height if you put it in a narrow container...
  <span class="customLineHeight"><br><br></span>
  After a custom break, this text will again display on several lines with normal line height...
</p>

The BR is anything but 'extra-special': it is still a valid XML tag that you can give attributes to. For example, you don't have to encase it with a span to change the line-height, rather you can apply the line height directly to the element.

You could do it with inline CSS:

_x000D_
_x000D_
This is a small line_x000D_
<br />_x000D_
break. Whereas, this is a BIG line_x000D_
<br />_x000D_
<br style="line-height:40vh"/>_x000D_
break!
_x000D_
_x000D_
_x000D_

Notice how two line breaks were used instead of one. This is because of how CSS inline elements work. Unfourtunately, the most awesome css feature possible (the lh unit) is still not there yet with any browser compatibility as of 2019. Thus, I have to use JavaScript for the demo below.

_x000D_
_x000D_
addEventListener("load", function(document, getComputedStyle){"use strict";_x000D_
  var allShowLineHeights = document.getElementsByClassName("show-lh");_x000D_
  for (var i=0; i < allShowLineHeights.length; i=i+1|0) {_x000D_
    allShowLineHeights[i].textContent = getComputedStyle(_x000D_
      allShowLineHeights[i]_x000D_
    ).lineHeight;_x000D_
  }_x000D_
}.bind(null, document, getComputedStyle), {once: 1, passive: 1});
_x000D_
.show-lh {padding: 0 .25em}_x000D_
.r {background: #f77}_x000D_
.g {background: #7f5}_x000D_
.b {background: #7cf}
_x000D_
This is a small line_x000D_
<span class="show-lh r"></span><br /><span class="show-lh r"></span>_x000D_
break. Whereas, this is a BIG line_x000D_
<span class="show-lh g"></span><br /><span class="show-lh g"></span>_x000D_
<span class="show-lh b"></span><br style="line-height:40vh"/><span class="show-lh b"></span>_x000D_
break!
_x000D_
_x000D_
_x000D_

You can even use any CSS selectors you want like ID's and classes.

_x000D_
_x000D_
#biglinebreakid {_x000D_
  line-height: 450%;_x000D_
  // 9x the normal height of a line break!_x000D_
}_x000D_
.biglinebreakclass {_x000D_
  line-height: 1em;_x000D_
  // you could even use calc!_x000D_
}
_x000D_
This is a small line_x000D_
<br />_x000D_
break. Whereas, this is a BIG line_x000D_
<br />_x000D_
<br id="biglinebreakid" />_x000D_
break! You can use any CSS selectors you want for things like this line_x000D_
<br />_x000D_
<br class="biglinebreakclass" />_x000D_
break!
_x000D_
_x000D_
_x000D_

You can find our more about line-height at the W3C docs.

Basically, BR tags are not some void in world of CSS styling: they still can be styled. However, I would recommend only using line-height to style BR tags. They were never intended to be anything more than a line-break, and as such they might not always work as expected when using them as something else. Observe how even after applying tons of visual effects, the line break is still invisible:

_x000D_
_x000D_
#paddedlinebreak {_x000D_
  display: block;_x000D_
  width: 6em;_x000D_
  height: 6em;_x000D_
  background: orange;_x000D_
  line-height: calc(6em + 100%);_x000D_
  outline: 1px solid red;_x000D_
  border: 1px solid green;_x000D_
}
_x000D_
<div style="outline: 1px solid yellow;margin:1em;display:inline-block;overflow:visible">_x000D_
This is a padded line_x000D_
<br id="paddedlinebreak" />_x000D_
break._x000D_
</div>
_x000D_
_x000D_
_x000D_

A work-around for things such as margins and paddings is to instead style a span with a br in it like so.

_x000D_
_x000D_
#paddedlinebreak {_x000D_
  background: orange;_x000D_
  line-height: calc(6em + 100%);_x000D_
  padding: 3em;_x000D_
}
_x000D_
<div style="outline: 1px solid yellow;margin:1em;display:inline-block;overflow:visible">_x000D_
This is a padded line_x000D_
<span id="paddedlinebreak"><br /></span>_x000D_
break._x000D_
</div>
_x000D_
_x000D_
_x000D_

Notice how the orange blob above is the span that contains the br.


Take a look at the line-height property. Trying to style the <br> tag is not the answer.

Example:

<p id="single-spaced">
    This<br>
    text<br>
    is<br>
    single-spaced.
</p>
<p id="double-spaced" style="line-height: 200%;">
    This<br>
    text<br>
    is<br>
    double-spaced.
</p>

<font size="4"> <font color="#ffe680">something here</font><br>

I was trying all these methods but most didn't work properly for me, eventually I accidentally did this and it works great, it works on chrome and safari (the only things I tested on). Replace the colour code thing with your background colour code and the text will be invisible. you can also adjust the font size to make the line break bigger or smaller depending on your desire. It is really simple.


You can control the <br> height if you put it inside a height limited div. Try:

<div style="height:2px;"><br></div>

This feels very hacky, but in chrome 41 on ubuntu I can make a <br> slightly stylable:

br {
  content: "";
  margin: 2em;
  display: block;
  font-size: 24%;
}

I control the spacing with the font size.


Update

I made some test cases to see how the response changes as browsers update.

_x000D_
_x000D_
*{outline: 1px solid hotpink;}
div {
  display: inline-block;
  width: 10rem;
  margin-top: 0;
  vertical-align: top;
}

h2 {
  display: block;
  height: 3rem;
  margin-top:0;
}

.old br {
  content: "";
  margin: 2em;
  display: block;
  font-size: 24%;
  outline: red;
}

.just-font br {
  content: "";
  display: block;
  font-size: 200%;
}
.just-margin br {
  content: "";
  display: block;
  margin: 2em;
}

.brbr br {
  content: "";
  display: block;
  font-size: 100%;
  height: 1em;
  outline: red;
  display: block;
}
_x000D_
<div class="raw">
  <h2>Raw <code>br</code>rrrrs</h2>
  bla<BR><BR>bla<BR>bla<BR><BR>bla
</div>
  
<div class="old">
  <h2>margin & font size</h2>
  bla<BR><BR>bla<BR>bla<BR><BR>bla
</div>
  
<div class="just-font">
  <h2>only font size</h2>
  bla<BR><BR>bla<BR>bla<BR><BR>bla
</div>

 <div class="just-margin">
  <h2>only margin</h2>
  bla<BR><BR>bla<BR>bla<BR><BR>bla
</div>
   
<div class="brbr">
  <h2><code>br</code>others vs only <code>br</code>s</h2>
  bla<BR><BR>bla<BR>bla<BR><BR>bla
</div>
_x000D_
_x000D_
_x000D_

They all have their own version of strange behaviour. Other than the browser default, only the last one respects the difference between one and two brs.


Use the content property and style that content. Content behavior is then adjusted using pseudo elements. Pseudo elements ::before and ::after both work in Mac Safari 10.0.3.

Here element br content is used as the element anchor for element br::after content. Element br is where br spacing can be styled. br::after is the place where br::after content can be displayed and styled. Looks pretty, but not a 2px <br>.

br { content: ""; display: block; margin: 1rem 0; }
br::after { content: "› "; /* content: " " space ignored */; float: left; margin-right: 0.5rem; }

The br element line-height property is ignored. If negative values are applied to either or both selectors to give vertical 'lift' to br tags in display, then correct vertical spacing occurs, but display incrementally indents display content following each br tag. The indent is exactly equal to the amount that lift varies from actual typographic line-height. If you guess the right lift, there is no indent but a single pile-up line exactly equal to raw glyph height, jammed between previous and following lines.

Further, a trailing br tag will cause the following html display tag to inherit the br:after content styling. Also, pseudo elements cause <br> <br> to be interpreted as a single <br>. While pseudo-class br:active causes each <br> to be interpreted separately. Finally, using br:active ignores pseudo element br:after and all br:active styling. So, all that's required is this:

br:active { }

which is no help for creating a 2px high <br> display. And here the pseudo class :active is ignored!

br:active { content: ""; display: block; margin: 1.25em 0; }
br { content: ""; display: block; margin: 1rem; }
br::after { content: "› "; /* content: " " space ignored */; float: left; margin-right: 0.5rem; }

This is a partial solution only. Pseudo class and pseudo element may provide solution, if tweaked. This may be part of CSS solution. (I only have Safari, try it in other browsers.)

Learn web development: pseudo classes and pseudo elements

Pay attention to global elements - BR at Mozilla.org


_x000D_
_x000D_
#biglinebreakid {_x000D_
  line-height: 450%;_x000D_
  // 9x the normal height of a line break!_x000D_
}_x000D_
.biglinebreakclass {_x000D_
  line-height: 1em;_x000D_
  // you could even use calc!_x000D_
}
_x000D_
This is a small line_x000D_
<br />_x000D_
break. Whereas, this is a BIG line_x000D_
<br />_x000D_
<br id="biglinebreakid" />_x000D_
break! You can use any CSS selectors you want for things like this line_x000D_
<br />_x000D_
<br class="biglinebreakclass" />_x000D_
break!
_x000D_
_x000D_
_x000D_


As the 'margin' doesn't work in Chrome, that's why I used 'border' instead.

br {
  display: block;
  content: "";
  border-bottom: 10px solid transparent; // Works in Chrome/Safari
}
@-moz-document url-prefix() {
  br {
    margin-bottom: 10px; // As 'border-bottom' doesn't work in firefox and 'margin-bottom' doesn't work in Chrome/Safari.
  }
}

The line height of the br tag can be different from the line height of the rest of the text inside a paragraph text by setting font-size for br tag.

Example: br { font-size: 200%; }