49

Is the following valid HTML?

<br class="something"> 

As an HTML element I can't see why it wouldn't be, but I don't think I've ever seen it in use.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Evanss
  • 23,390
  • 94
  • 282
  • 505
  • I would say it is valid html but also pointless. br tags are always empty (ie are not allowed to have content) and just act as a line break. So don't know what useful styling you would want to apply to it. – Max Williams Jun 05 '13 at 15:56
  • http://www.w3.org/TR/html5/text-level-semantics.html#the-br-element – Arpit Jun 05 '13 at 16:01
  • So what is the practical problem you are facing? This looks like a theoretical question, about a construct for which no practical use is suggested or even suspected. – Jukka K. Korpela Jun 05 '13 at 17:08
  • 12
    I sometimes use `
    `, if for example I want to remove a line break in mobile view.
    – b00t Jun 22 '16 at 08:43

11 Answers11

51

Yes.

See the specification.

The class attribute applies to "HTML Elements" (which includes br elements).

Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    More specific : http://www.w3.org/TR/html5/text-level-semantics.html#the-br-element The first line of the page – Arpit Jun 05 '13 at 15:59
6

One application I can think off is adding using it with pseudo-elements, such as :before or :after, for instance adding some text beneath a horizotal line:

hr.something:after  { content: "Some text"  }
John Topley
  • 113,588
  • 46
  • 195
  • 237
Artur Udod
  • 4,465
  • 1
  • 29
  • 58
5

Yes, is valid.

Specification tells that br accepts global and event attributes, and class is a global one.

Note that if you target HTML4 and below, there's an specific attribute for br named clear, which defines where to put the new line, as stated here, but now is deprecated and unless your DOCTYPE lets you, you should not use it anymore.

Jorge Fuentes González
  • 11,568
  • 4
  • 44
  • 64
4

Yes, it's valid statement. you could use something like.

<br style="height: 100px; margin-top: 10000px;">

But instead using div element is recommended.

Vivek T
  • 61
  • 1
  • 5
3

Yes you can, from the w3c specification : http://dev.w3.org/html5/markup/br.html

<br> element can make use of global attributes, and class is one of them.

Laurent S.
  • 6,816
  • 2
  • 28
  • 40
3

It is valid in HTML 4.01, because the class attribute is allowed for the br element. It is also valid in HTML5 in HTML serialization, because the class attribute is allowed for all elements. It is not valid XHTML without a matching </br>.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
1

Yes, you can add a class attribute to a <br> element. id as well if you want.

John Topley
  • 113,588
  • 46
  • 195
  • 237
0

Sure, it's valid. A useful example would be if you wanted to visually add a little space after a line-break, without ending the paragraph. Or perhaps apply a background image. All sorts of styling options.

ThatBlairGuy
  • 2,426
  • 1
  • 19
  • 33
0

a br tag doesn't produce a CSS box and hence little can be done with it.

Only practical case is clear. i believe drupal used it at some time. interestingly CSS 1 spec. refrained from doing anything to br. http://www.w3.org/TR/REC-CSS1/#br-elements. no doubt applying some css on it doesn't invalidate the code but will cause little effect.

this https://stackoverflow.com/a/899359/1043824 is a good discussion on the topic.

Community
  • 1
  • 1
inquisitive
  • 3,549
  • 2
  • 21
  • 47
0

For poetry in single line lyrics you can use br class="hanging" to cause the line to wrap only on a narrow screen such as a phone, but not on wider widths, thus preserving the line breaks of the poet, which really really matters to them, while still allowing text to flow freely, which matters to me

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32264394) – mcky Jul 22 '22 at 19:59
-1

It does come handy when you want to omit the linebreak.e.g in a Headline when having a responsive layout and certain breakpoints.