Home » Html » change <br> height using css

change <br> height using css

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Answers:

You can’t change the height of the br tag itself, as it’s not an element that takes up space in the page. It’s just an instruction to create a new line.

You can change the line height using the line-height style. That will change the distance between the text blocks that you have separated by empty lines, but natually also the distance between lines in a text block.

For completeness: Text blocks in HTML is usually done using the p tag around text blocks. That way you can control the line height inside the p tag, and also the spacing between the p tags.

Questions:
Answers:

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.

Questions:
Answers:

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>

Questions:
Answers:

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%; }

Questions:
Answers:

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

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

Questions:
Answers:

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

Questions:
Answers:

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>

Questions:
Answers:

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:

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

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

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

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. A work-around for things such as margins and paddings is to instead style a span with a br in it like so.

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