Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change <br> height using CSS

Tags:

html

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.

like image 546
ariel Avatar asked Sep 30 '11 19:09

ariel


People also ask

Can you style a BR tag?

The <br> element has a single, well-defined purpose — to create a line break in a block of text. As such, it has no dimensions or visual output of its own, and there is very little you can do to style it.

What is the height of Br?

<br> is a line break. It doesn't have a height, it just breaks the line. Doesn't replacing mean, removing the element?

How do you give Br in CSS?

A line-break can be added in HTML, using only CSS, by employing the pseudo-class ::after or ::before . In the stylesheet, we use these pseudo-classes, with the HTML class or id, before or after the place where we want to insert a line-break. In myClass::after : Set the content property to "\a" (the new-line character).

What is line-height CSS?

The line-height CSS property sets the height of a line box. It's commonly used to set the distance between lines of text. On block-level elements, it specifies the minimum height of line boxes within the element. On non-replaced inline elements, it specifies the height that is used to calculate line box height.


2 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.


Update

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

*{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; }
<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>

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.

like image 51
Ben Avatar answered Sep 16 '22 18:09

Ben


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.

like image 35
Guffa Avatar answered Sep 16 '22 18:09

Guffa