views:

11217

answers:

8

Hi, wondering if anyone knows if it is possible to target line-break tag with css. Basically all I want to do is 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. - Thanks!

A: 

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
Inline elements *do* have borders, paddings and margins -- only behave other way.
Török Gábor
Just what I was going to suggest.
Joe Philllips
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
Grant Wagner
+38  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.

Update

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.

Török Gábor
brilliant - thanks so much for clearing that up. I can't change the html unfortunately. never-mind - it was just a small thing I was wondering about.
dc3
@dc3, I would definitely accept this post as the answer to your question. Very well explained, Gábor.
Peter Perháč
@dc3 I updated my answer. Pelms` tests are showing a little chance that it will work in future browsers.
Török Gábor
+1 Great answer -@dc3 Why not accept this answer?
gt
+6  A: 

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
:before does not exist without content. Add `content:""; display:block` to the second rule.
porneL
+4  A: 

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>');
});
Roy Rico
A: 

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

br {

  border-bottom: 1px dashed #000000;
  background-color: #ffffff;
  display: block;
  }
+2  A: 
pelms
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?
Török Gábor
Links added - Win7 uses a slightly different version of IE8 in case that makes any difference.
pelms
@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.
Török Gábor
+2  A: 

My own tests conclusively show that br tags do not like to be targeted for css.

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 )
{
    elems = element.getElementsByTagName( 'br' );
    for ( var i = 0; i < elems.length; i ++ )
    {
        br = elems.item( i );
        hr = document.createElement( 'hr' );
        br.parentNode.replaceChild( hr, br );
    }
}

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

Kris
‘I can't change the html unfortunately’ dc3 wrote.
Török Gábor
@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
A: 

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
works not for all use cases cause hr is a block element - therefor styling is limited
Thariama
Apparently he wants to use clear:all, see Gabor's excellent reply. So your fork metaphor makes no sense whatsoever.
Dr. Hfuhruhurr