Replace NOBR Tags with CSS

Written by David Walsh on December 17, 2009 · 27 Comments

This is just a simple tip for those still clinging to NOBR HTML tags. The NOBR tag, for those who haven't seen them before, prevent a selection of text from being "broken" into multiple lines. It looks something like this:

<nobr>Don't Break This Text</nobr>

While NOBR tags still work in major browsers, they've been deprecated and you should try to avoid them when possible. Instead, wrap your text in a SPAN tag with a nobr CSS class and apply the following settings to the nobr CSS class:

.nobr	{ white-space:nowrap; }

A quick reminder for those who've not yet moved onto using CSS to prevent line breaks. Especially useful for placing phone numbers on a website.

Comments

  1. Funny. I knew about the css way, but now the html way.

    I also love { white-space:pre; }

  2. Funny! I knew about the CSS too, but I didn’t know the tag. So much to learn, and unlearn!

  3. I second what Adam and Alex said! :)

  4. I haven’t seen those NOBR tags in quite some time! I don’t even think I saw those when I first studied the HTML! I must have stumbled on them later on…

    Still good to know the two solutions.

  5. Better than class=nobr would be class=telephone, if that’s what you’re marking up, otherwise it’s presentational logic in HTML ;-)

  6. Wow I’ve never used nobr before… if some kind of apocalyptic disaster happens to the Internet forcing us to use deprecated html this will come in handy :)

  7. @James Lin: *Paranoid mode* Don’t talk too loud! THEY are watching! :-)

  8. Michael Bryan December 18, 2009

    When I first started learning html I ran into the problem of having to keep a telephone number on a site from breaking. After googling for a while I finally found the white-space css but never came across the nobr tag.

    I guess it’s for the best as I’d probably still be using it.

  9. Travis Pulley December 20, 2009

    I never knew about the nobr tag. Before I knew the css way, sometimes I’d make a table with one cell just to invoke . I’m not proud of it, but sometimes deadlines are pushy.

  10. Travis Pulley December 20, 2009

    @Travis Pulley: oops – that should read: >td nowrap=”nowrap”<

  11. Google’s default page uses NOBR tags.

  12. Eh? Learn something new every day! :)

  13. Thnx David, Before seeing this post i hardly heard this tag exist. It would be much better if you add the cross browser compatible list both for the Tag and CSS example you have given.

  14. I saw this tag randomly on a site one day and googled-it because I had never heard of it before. Thanks for the new knowledge, hombre!

  15. I am apalled! I learned html 1.0 (from a physical paper book) in 1995 before most anyone had heard of css or it was even a standard, and I still use nobr for everything from phone numbers, as mentioned, to making sure the last line of a dynamically sizable paragraph always contains at least 2 words for proper appearances sake. I didn’t even know nobr was NOT standard anymore until today when I ran a page through the w3 org validator and it came up with a warning. Thanks much for the politically correct alternative though. (just have to look up what a “span” tag is now.)
    [grumble] Bah humbug, KIDS these days! Never heard of nobr. Bet they’ve never played a 45rpm record or an 8-track either. lol

  16. Dang, been working with HTML for years and never heard of this tag. Awesome to have learned this one.

    For what I read, although is “deprecated” it’s actually supported by all browsers: http://reference.sitepoint.com/html/nobr — So technically it’s not really “deprecated” :p

    Everywhere I read though, do say not to use it.

    There’s a similar tag:</code. It means "but you can break HERE if you want":
    http://www.htmlcodetutorial.com/linepar/_WBR.html

  17. I’m a nub and iv been working to long on this today. Now what i don’t know is how you word* or type out .nobr { white-space:nowrap; } b/c when i put CSS in my HTML I put it in my and between ? It doesn’t work and i tried making a new save with a file named Style.css and doing a in the .
    Iv been up for 24 hours sry if that doesn’t make sense.
    Pls show me where to place .nobr { white-space:nowrap; } thanks

  18. LOL, the guys who are saying they knew about white-space, but not nobr, must be really new to this. nobr was so useful way back when dinosaurs ruled the world. Impossible not to have heard about it.

  19. OdinVonTogan May 15, 2013

    Mike, some of them never even heard of FORTRAN, and some not even unix or dtksh, sed, or awk. Had a professor in 1990 claim that FORTRAN was going away – 10 yrs later I was still programming it at major aerospace industries – billed at $250/hr. I never heard of NOBR until today (May 5th 2013), and it is being generated by SharePoint 2007, and, I’m going to use JavaScript to test and maybe highlight the contents between the NOBR tags – also told in 1998 that JavaScript was going away – and, I’m using JavaScript because SharePoint will not allow me to compare TODAY with the date between the NOBR tags.

  20. Randy June 8, 2013

    Browsers support NOBR, but many ebook readers don’t (e.g. Kindle).

    It strikes me as pretty lame to be deprecating functions that a lot of people think are standard. It’s as if they’re trying to prepare us for a semi-apocalypse when we’ll need to conserve bytes.

  21. Making no-breaks with css requires so much more effort – and I’m lazy so…. What’s next? Deprecation of tags with .br { white-space:dobreak; }? Just because something becomes deprecated by the so called web “authority” does not mean you should stop using it or it will stop rendering in any modern web browser available today. IMO, is incredibly more consise in both in its intent and its use. Its also very hard to believe that so many have never heard of nor used this tag at some point.

  22. Making no-breaks with css requires so much more effort – and I’m lazy so…. What’s next? Deprecation of “” tags with “.br { white-space:dobreak; }”? Just because something becomes deprecated by the so called web “authority” does not mean you should stop using it or it will stop rendering in any modern web browser available today. IMO, “” is incredibly more consise both in its intent and its use. Its also very hard to believe that so many have never heard of nor used this tag at some point.

  23. @Randy,Aaron: Precisely. It’s not even “to conserve bytes” – the CSS rule alone is bigger than a pair of nobr + close-nobr tags, not to mention having a span that needs its class attribute specified! It’s like all browsers already have that CSS rule hardcoded into them for nobr.

  24. [...] Replace NOBR Tags with CSS [...]

  25. <nobr> and </nobr> still work fine in IE 11.0.9600.16518, Firefox 27.0.1, Chrome 33.0.1750.146 m, and Opera 12.16.

Be Heard

Tip: Wrap your code in <pre> tags or link to a GitHub Gist!

Use Code Editor
Older
Add Events to the PHP Calendar
Newer
Create a Simple Slideshow Using MooTools