Prevent Wrapping of <code> Tags in Text

By  on  

Writers of technical documentation (or lowly blog writers like myself) oftentimes put property names in <code> tags within blocks of text. Doing so makes reading the text easier and prevents users from misconstruing explanatory text with property names. CSS uses a dash within its property and value names and English uses dashes to signal a place where the line could break the text...see the problem here? The contents of your <code> tag could wrap and that can be unsightly. To prevent that smudge in your otherwise flawless writing, you can use white-space: nowrap:

code {
    white-space: nowrap;
}

The only downside of modifying white-space is that the code could flow well out of its container if the code text is lengthy. I would contend that length property names would be best placed on their own line (in their own block) but that issue is one to keep in mind. You could use text-overflow: ellipsis and a max-width to temper the element width, but the text could then lose its meaning.

It's up to you to decided if this could be safe for your own use. I just think it's a nice little addition to keep your content from breaking in ways you haven't thought of!

Recent Features

  • By
    6 Things You Didn&#8217;t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

Incredible Demos

  • By
    Multiple File Upload Input

    More often than not, I find myself wanting to upload more than one file at a time.  Having to use multiple "file" INPUT elements is annoying, slow, and inefficient.  And if I hate them, I can't imagine how annoyed my users would be.  Luckily Safari, Chrome...

  • By
    MooTools Clipboard Plugin

    The ability to place content into a user's clipboard can be extremely convenient for the user. Instead of clicking and dragging down what could be a lengthy document, the user can copy the contents of a specific area by a single click of a mouse.

Discussion

  1. As an in between step or when modifying white-space is unwanted, you could use “‑” to show a dash but not break on it.

  2. Little late to the game here but…

    That’s an interesting problem you bring up at the end there. It is rare to have an inline code tag break out of its container, but there is a way around it. Here it is:

    http://jsbin.com/UQUfuXUX/1/edit

    Two things to note:

    – I’m using a non-breaking hyphen character, inserted with its decimal notation.
    – I’ve added word-wrap: break-word in the CSS to prevent long lines from breaking the container.

    Truthfully, it’s not an ideal solution to do this solely on the front-end. Preferrably you would have some sort of back-end function that automatically replaces hyphens inside of ‘code’ tags with the non-breaking hyphen character.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!