Best way to encode Degree Celsius symbol into web page?

HtmlSymbols

Html Problem Overview


How should I encode special characters into web pages? For instance I need this symbol ℃, which I used just by copying and pasting the character as I can see it now. This worked for the desktop browsers I checked with and also on iPad and iPhone but nothing is displayed on a Blackberry I used for testing. Is there a standard best practice for this?

http://www.fileformat.info/info/unicode/char/2103/browsertest.htm

Html Solutions


Solution 1 - Html

Try to replace it with °, and also to set the charset to utf-8, as Martin suggests.

°C will get you something like this:

Degrees Celsius

Solution 2 - Html

If you really want to use the DEGREE CELSIUS character “℃”, then copy and paste is OK, provided that your document is UTF-8 encoded and declared as such in HTTP headers. Using the character reference ℃ would work equally well, and would work independently of character encoding, but the source would be much less readable.

The problem with Blackberry is most probably a font issue. I don’t know about fonts on Blackberry, but the font repertoire might be limited. There’s nothing you can do about this in HTML, but you can use CSS, possibly with @font face.

But there is seldom any reason to use the DEGREE CELSIUS. It is a compatibility character, included in Unicode due to its use in East Asian writing. The Unicode Standard explicitly says in Chapter 15 (section 15.2, page 497):

“In normal use, it is better to represent degrees Celsius “°C” with a sequence of U+00B0 degree sign + U+0043 latin capital letter c, rather than U+2103 degree celsius.”

The degree sign “°” can be entered in many ways, including the entity reference `°, but normally it is best to insert it as a character, via copy and paste or otherwise. On Windows, you can use Alt 0176.

Caveat: Some browsers may treat the degree sign as allowing a line break after it even when no space intervenes, putting “°” and the following “C” on separate lines. There are different ways to prevent this. A simple and effective method is this: <nobr>42 °C</nobr>.

Solution 3 - Html

Using sup on the letter "o" and a capital "C"

<sup>o</sup>C

Should work in all browsers and IE6+

Solution 4 - Html

I'm not sure why this hasn't come up yet but why don't you use &#8451; (℃) or &#8457; (℉) for Celsius and Fahrenheit respectively!

Solution 5 - Html

Add a metatag to your header

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

This expands the amount of characters you can use.

Solution 6 - Html

  1. The degree sign belongs to the number, and not to the "C". You can regard the degree sign as a number symbol, just like the minus sign.
  2. There shall not be any space between the digits and the degree sign.
  3. There shall be a non-breaking space between the degree sign and the "C".

Solution 7 - Html

If using Java-JSP, What worked for me is to paste below in JSP page

<%@ page contentType="text/html; charset=UTF-8" %>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEvanssView Question on Stackoverflow
Solution 1 - HtmlNixView Answer on Stackoverflow
Solution 2 - HtmlJukka K. KorpelaView Answer on Stackoverflow
Solution 3 - HtmlPersijnView Answer on Stackoverflow
Solution 4 - HtmlSaba AhangView Answer on Stackoverflow
Solution 5 - HtmlMartin Risell LiljaView Answer on Stackoverflow
Solution 6 - HtmlJohan HoffstenView Answer on Stackoverflow
Solution 7 - HtmlveritasView Answer on Stackoverflow