184

Is it possible to target the line-break <br/> tag with CSS?

I would like to have a 1px dashed line every time there is a line-break. I am customising a site with my own CSS and cannot change the set HTML, otherwise I would use some other way.

I don't think it is possible but maybe there is a way someone knows about.

Hidden Hobbes
  • 13,893
  • 3
  • 38
  • 64
dc3
  • 1,841
  • 2
  • 12
  • 3

17 Answers17

178

Technically, yes, you can target a <br> element with CSS directly or by applying a class to it and targeting the class. That being said, a <br> generates a line-break and it is only a line-break. As this element has no content, there are only few styles that make sense to apply on it, like clear or position. You can set <br>'s border but you won't see it as it has no visual dimension.

If you like to visually separate two sentences, then you probably want to use the horizontal ruler which is intended for this goal. Since you cannot change the markup, I'm afraid using only CSS you cannot achieve this.

It seems, it has been already discussed on other forums. Extract from Re: Setting the height of a BR element using CSS:

[T]his leads to a somewhat odd status for BR in that on the one hand it is not being treated as a normal element, but instead as an instance of \A in generated content, but on the other hand it is being treated as a normal element in that (a limited subset of) CSS properties are being allowed on it.

I also found a clarification in the CSS 1 specification (no higher level spec mentions it):

The current CSS1 properties and values cannot describe the behavior of the ‘BR’ element. In HTML, the ‘BR’ element specifies a line break between words. In effect, the element is replaced by a line break. Future versions of CSS may handle added and replaced content, but CSS1-based formatters must treat ‘BR’ specially.

Grant Wagner's tests show that there is no way to style BR as you can do with other elements. There is also a site online where you can test the results in your browser.

pelms made some further investigations, and pointed out that IE8 (on Win7) and Chrome 2/Safari 4b allows you to style BR somewhat. And indeed, I checked the IE demo page with IE Net Renderer's IE8 engine, and it worked.

c69 made some further investigations, and it turns out you can style the marker for br quite heavily (though, not cross-browser), yet this will not affect the line-break itself, because it seem to belong to parent container.

TylerH
  • 20,799
  • 66
  • 75
  • 101
viam0Zah
  • 25,949
  • 8
  • 77
  • 100
  • 1
    Great answer. If it's not cross-browser, it's not worth trying to style it unless you are targetting solely the one browser (e.g. mobile device only, perhaps). Otherwise, seems to make sense to modify your HTML to allow styling (

    tags, anyone?)

    – razzed Mar 16 '16 at 17:54
68

Try the following, I put it together using Update 2 from another answer with high votes, and it worked perfectly for me:

br
{   content: "A" !important;
    display: block !important;
    margin-bottom: 1.5em !important;
}
Rok
  • 862
  • 7
  • 9
  • 8
    Doesn't seem to work on Edge and IE, but they're not real browsers. – Jester Jun 24 '20 at 19:23
  • It works on the latest version of Edge. I think it is because they based it on Chromium now. – Kade Jan 21 '21 at 19:32
  • `!important` should not be necessary, and should only ever be used if absolutely necessary. – Jake Apr 23 '21 at 00:30
  • … Not an answer to the question? – Maëlan Apr 29 '21 at 17:25
  • 1
    @Maëlan It is an answer of "yes" on most browsers if you try it, and since it is dependent on the browser in most cases, there is no universal yes or no answer to that question. – Rok Jul 13 '21 at 03:38
  • @Jake These are usually nested, thus would inherit rules unless told otherwise. Also, due to the unpredictability of how some browsers treat this style, it was partially an exercise in prudence to include !important so that the style works on as many browsers as possible. – Rok Jul 13 '21 at 03:41
  • It somehow answers the question in title, “can you target a
    with CSS”, except that your example falls in the very limited subset of CSS properties which actually work, and that the answer would rather be “no“ in general. It does not answer what the OP is actually asking, “have a 1px dashed line every time there is a line-break”.
    – Maëlan Jul 14 '21 at 12:26
  • @Maëlan Well, if you wanna be pedantic about it, I can totally entertain you. Watch this: Questions always end with a question mark (?). Consequently, if you look above, you shall see that the only phrases that end with a question mark have to do with targeting
    , and nothing else. Thus, my answer actually does answer the question(s). As for the question's [details], which, it seems, you are more concerned about, the vast majority of those who ask or search for a question like this know quite well how to use my answer to do whatever they want to
    , dashed line or margin-bottom.
    – Rok Oct 29 '21 at 17:42
  • Please don’t be rude. I wasn’t. “the vast majority of those who ask or search for a question like this know quite well how to use my answer to do whatever they want to
    , dashed line or margin-bottom” --> They don’t since, as I was saying, this solution only applies to a very limited subset of CSS (which doesn’t extend much further than the few properties demonstrated in this answer).
    – Maëlan Oct 29 '21 at 18:59
  • 1
    Good answer. Sometimes it's not possible (or practical) to replace or remove existing `
    ` tags. This approach works. `content:` and `display: block` are key. `font-size:` also works if you need to adjust the height.
    – MQuiggGeorgia Feb 19 '22 at 10:42
35

There is one good reason you would need to style a <br> tag.

When it is part of code you don't want to (or can't) change and you want this particular <br> not to be displayed.

.xxx br {display:none}

Can save a lot of time and sometimes your day.

potterbm
  • 125
  • 7
Bernard Sfez
  • 1,329
  • 2
  • 15
  • 18
  • Another good reason is to add space after it on certain websites that are very badly styled. You would be utterly shocked to know how many websites and forums have their text showing as "walls" with no spaces between paragraphs because, for whatever reasons,
    was consistently used instead of

    ...

    , especially 12 years ago, at the time this question was asked.
    – Rok Jul 13 '21 at 03:46
23

For the benefit of any future visitors who may have missed my comments:

br {
    border-bottom:1px dashed black;
}

does not work.

It has been tested in IE 6, 7 & 8, Firefox 2, 3 & 3.5B4, Safari 3 & 4 for Windows, Opera 9.6 & 10 (alpha) and Google Chrome (version 2) and it didn't work in any of them. If at some point in the future someone finds a browser that does support a border on a <br> element, please feel free to update this list.

Also note that I tried a number of other things:

br {
    border-bottom:1px dashed black;
    display:block;
}

br:before { /* and :after */
    border-bottom:1px dashed black;
    /* content and display added as per porneL's comment */
    content: "";
    display: block;
}

br { /* and :before and :after */
    content: url(a_dashed_line_image);
}

Of those, the following does works in Opera 9.6 and 10 (alpha) (thanks porneL!):

br:after {
    border-bottom:1px dashed black;
    content: "";
    display: block;
}

Not very useful when it is only supported in one browser, but I always find it interesting to see how different browsers implement the specification.

Grant Wagner
  • 25,263
  • 7
  • 54
  • 64
  • 3
    :before does not exist without content. Add `content:""; display:block` to the second rule. – Kornel May 22 '09 at 19:51
11
br { padding: 1px 8px; border-bottom: 1px dashed #000 }

renders as below in IE8... not a lot of use in just one browser though.

IE 8 screenshot

(N.B. I'm using IE 8.0.7100 (on Win7 RC) if that makes any difference)

Also,

br:after { content: "..." }  
br { content: "" }`

or,

br:after {
    border: 1px none black;
    border-bottom-style: dashed;
    content: "";
    padding: 0 6px 0;
}

br { content: "" }

gives a dashed line in Chrome 2 / Safari 4b but loses the line break which (unless anyone can come up with a way to reintroduce that) makes it less than useless.

e.g.
IE8 test, Chrome/Safari test and another

pelms
  • 1,212
  • 1
  • 12
  • 21
  • That's interesting. I cannot reproduce that under IE8. Can you put up an example online that looks like in your IE8 as your embedded image? – viam0Zah May 24 '09 at 19:29
  • Links added - Win7 uses a slightly different version of IE8 in case that makes any difference. – pelms May 25 '09 at 16:34
  • 1
    @pelms Thanks, I checked the IE test page with IENetRenderer and it really shows the border under `BR`. Thanks, I updated my answer with your testing results. – viam0Zah May 26 '09 at 16:47
10

I know you can't edit the HTML, but if you can modify the CSS, can you add javascript?

if so, you can include jquery, then you could do

<script language="javascript">
$(document).ready(function() {
    $('br').append('<span class="myclass"></span>');
});
</script>
Roy Rico
  • 3,683
  • 6
  • 35
  • 36
5

UPDATED 9/13/2019:

The purpose of styling the <br> tag like this is to make a "bigger" line break (i.e., with a bit of extra space between the lines).

The "oldbig" class (below) was tested and worked properly in Chrome 66.0.3359.181, Firefox Quantum 60.0.2, Edge 42.17134.1.0 and IE 11.48.17134.0, to style <br>. Unfortunately, it broke in Chrome version 76 and its derivatives (circa August 2019). The symptom is that the extra space between lines is no longer displayed.

The "newbig" class is tested and working properly in current versions of Chrome (76.0.3809.132), Opera, Firefox, Edge, IE, Brave, and Palemoon:

br.oldbig {line-height:190%;vertical-align:top;}
br.newbig {display:block;content:"";margin-top:0.5em;line-height:190%;vertical-align:top;}

Here's a demo:

br.oldbig {line-height:175%;vertical-align:top;}
br.newbig {display:block;content:"";margin-top:0.5em;line-height:190%;vertical-align:top;}
<p style="max-width:20em">This is a paragraph which demonstrates the difference 
 between a line-break which occurs when the text exceeds the max-width, and a 
 line-break forced by a &lt;br&gt; tag here:<br>
 and a line break forced by a &lt;br class=oldbig&gt; tag here:<br class=oldbig>
 and a line break forced by a &lt;br class=newbig&gt; tag here:<br class=newbig>
 This is the next line after the &lt;br class=newbig&gt; tag (but still 
 part of the same paragraph).</p>
<p style="max-width:20em">And this is another paragraph, entirely.</p>

This is the result, displayed in Chrome v.76:

screenshot

Dave Burton
  • 2,960
  • 29
  • 19
5

My own tests conclusively show that br tags do not like to be targeted for css. (still true in Safari in 2023)

But if you can add style then you can probably also add a scrip tag to the header of the page? Link to an external .js that does something like this:

function replaceLineBreaksWithHorizontalRulesInElement( element )
{
    const brs = element.querySelectorAll( 'br' );
    for ( let br of brs )
    {
        const hr = document.createElement( 'hr' );
        br.parentNode.replaceChild( hr, br );
    }
}

So in short, it's not optimal, but here is my solution.

Kris
  • 40,604
  • 9
  • 72
  • 101
  • ‘I can't change the html unfortunately’ dc3 wrote. – viam0Zah May 27 '09 at 06:45
  • 2
    @Török: The answer i posted is not purely for the OP but for everyone ever finding this page with a similar question. – Kris May 27 '09 at 07:49
  • 1
    The collection returned by `getElementsBy` will update automatically, which will cause the ordinal in `elems` to change and miss parts of the `br`, so I used `querySelectorAll`. – hrdom Feb 14 '23 at 09:32
2

BR is an inline element, not a block element.

So, you need:

 br.Underline{
    border-bottom:1px dashed black;
    display: block;
  }

Otherwise, browsers that are a little pickier about such things will refuse to apply borders to your BR elements, since inline elements don't have borders, padding, or margins.

richardtallent
  • 34,724
  • 14
  • 83
  • 123
  • Inline elements *do* have borders, paddings and margins -- only behave other way. – viam0Zah May 22 '09 at 18:49
  • I am trying things out in Fx with Firebug. I had tried that too but it didn't work for me. I notice that the slash in the line break tag is not floating (if that is the correct terminology), ie..there is no space between the br and the /. is this a factor. I had thought that there had to be a space. I edited the html anyway in firebug to fix this and still nothing. anyway thanks for all the help people - never used this site before and I am very surprised how fast I got a response. great community you got here. – dc3 May 22 '09 at 18:54
  • 2
    @richard: Have you actually tested this, if so, which browser(s)? I tried it in IE 6, 7 & 8, Firefox 2, 3 & 3.5B4, Safari 3 & 4 for Windows, Opera 9.6 & 10 (beta) and Google Chrome (version 1) and it didn't work in any of them. – Grant Wagner May 22 '09 at 18:58
1

this seems to solve the problem:

<!DOCTYPE html>

<style type="text/css">
#someContainer br { display:none }
#someContainer br + a:before { content:"|"; color: transparent; letter-spacing:-100px; border-left: 1px dashed black; margin:0 5px; }
</style>

<div id="someContainer"><a>link</a><br /><a>link</a><br /><a>link</a></div>
y34h
  • 1,631
  • 1
  • 11
  • 17
1

old question but this is a pretty neat and clean fix, might come in use for people who are still wondering if it's possible :):

br{
    content: '.';
    display: inline-block;
    width: 100%;
    border-bottom: 1px dashed black;
}

with this fix you can also remove BRs on websites ( just set the width to 0px )

0

I placed a <br> tag into a <span> tag and was able to use display:none; on the <span> to control when not to use the <br> tag using Media Queries.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

The most practical and short solution is to use not so popular HTML tag .

<style>
blockquote {margin: 7px 0px 0px 0px;}
</style>

This is my first line
<blockquote/>
Second line with a half height break
Gleno
  • 85
  • 4
0

Use <p></p> to skip a fixed height. The semantics are right. Style the p for the height you wish: p {margin-bottom: 1rem}. .5rem works like <br>, 1rem looks good for normal line spacing. Increase from there as desired. Reserve <br> for line breaks only.

David Spector
  • 1,520
  • 15
  • 21
0

This will work, but only in IE. I tested it in IE8.

br {

  border-bottom: 1px dashed #000000;
  background-color: #ffffff;
  display: block;
  }
-1

This is CSS 101.

Why make simple things so difficult???? Why use
in the first place?

Isn't it just much easier to press enter/return while writing, and just start a new paragraph after each line of text?

You can still make it look like you used < /br> by limiting the padding and margins. Then set a bottom border style to the p selector in CSS.

If you want your last line of text not to have a dashed line under it, only in between the text lines (as a visual separation), simply add an id to the

tag of the last line of text only, and write the CSS for it.

View here:

p {
  padding-bottom: 10px;
  margin-top: 0px;
  margin-bottom: 10px;
  border-bottom: dashed 2px #777;
  }
  
 p#lastLine {
 border-bottom: none;
 }
<p>
This is a line of some very nice text. And mind you, this is no joke!
</p>
<p >
And this is another line of some very nice text. 
</p>
<p >
And this is another line of some very nice text. 
</p>
<p >
And this is another line of some very nice text. 
</p>
<p id="lastLine">
And this THE LAST LINE line of some very nice text. This is great!
</p>
Leontine
  • 9
  • 1
-2

Why not just use the HR tag? It's made exactly for what you want. Kinda like trying to make a fork for eating soup when there's a spoon right in front of you on the table.

natas
  • 183
  • 9
  • works not for all use cases cause hr is a block element - therefor styling is limited – Thariama Jun 09 '10 at 09:15
  • 1
    Apparently he wants to use clear:all, see Gabor's excellent reply. So your fork metaphor makes no sense whatsoever. – eddy147 Aug 11 '10 at 20:26