How to Write Degree Celsius Symbol in HTML

  1. Using HTML Character Entities
  2. Using Unicode
  3. Using CSS for Styling
  4. Conclusion
  5. FAQ
How to Write Degree Celsius Symbol in HTML

Writing the degree Celsius symbol (°C) in HTML might seem like a small detail, but it’s crucial for any web developer or content creator who wants to present temperature data accurately. Whether you’re building a weather application, a scientific blog, or just need to display temperatures in your content, knowing how to represent this symbol correctly can enhance the professionalism and clarity of your work.

In this tutorial, we will explore various methods to write the degree Celsius symbol in HTML. From using character entities to employing CSS, we will cover all the essential techniques. By the end of this article, you’ll have a solid understanding of how to include the degree Celsius symbol in your HTML documents seamlessly.

Using HTML Character Entities

One of the simplest ways to include the degree Celsius symbol in HTML is by using character entities. Character entities are special codes that represent characters that might otherwise be difficult to include in your HTML code. For the degree Celsius symbol, you can use either ° or °. Here’s how you can implement it in your HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Degree Celsius Example</title>
</head>
<body>
    <p>The temperature today is 25 &deg;C.</p>
    <p>The boiling point of water is 100 &#176;C.</p>
</body>
</html>

Using character entities is straightforward and ensures that the degree symbol displays correctly across all browsers. The &deg; entity is widely recognized and is the preferred method for most developers. The alternative &#176; is also valid and can be used interchangeably. Both methods are effective for ensuring the degree Celsius symbol appears as intended in your HTML content.

Using Unicode

Another effective way to include the degree Celsius symbol in your HTML is through Unicode. Unicode provides a universal character set that includes the degree symbol. You can directly insert the Unicode character for the degree symbol in your HTML code. The Unicode for the degree symbol is U+00B0. Here’s how you can do it:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Degree Celsius Unicode Example</title>
</head>
<body>
    <p>The optimal room temperature is 22° C.</p>
</body>
</html>

In this example, you simply type the degree symbol directly in your HTML document. Most modern text editors support UTF-8 encoding, which means the degree symbol will be displayed correctly. This method is particularly useful for those who prefer a more straightforward approach without using character entities. Just ensure your HTML document is saved with UTF-8 encoding to avoid any display issues.

Using CSS for Styling

If you want to separate your content from your styling, you can also use CSS to add the degree Celsius symbol. This method is particularly useful when you want to maintain a consistent style across your website. You can use the ::after pseudo-element in CSS to append the degree symbol after a temperature value. Here’s how to implement it:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Degree Celsius CSS Example</title>
    <style>
        .temperature::after {
            content: " °C";
        }
    </style>
</head>
<body>
    <p class="temperature">25</p>
</body>
</html>

In this example, the temperature value of 25 is styled with CSS to include the degree Celsius symbol. The ::after pseudo-element allows you to add content after the specified element, which in this case is the paragraph with the class temperature. This method is particularly advantageous for maintaining a clean HTML structure while still achieving the desired output. It also allows for easy adjustments in styling without altering the HTML content directly.

Conclusion

In conclusion, writing the degree Celsius symbol in HTML can be accomplished through various methods, including character entities, Unicode, and CSS styling. Each method has its advantages, allowing you to choose one that best fits your project needs. By mastering these techniques, you can enhance your web content and ensure accurate representation of temperature information.

Whether you’re a seasoned developer or just starting, understanding how to represent special characters like the degree Celsius symbol is a valuable skill. Implement these methods in your projects and see how they improve the clarity and professionalism of your web pages.

FAQ

  1. How do I write the degree Celsius symbol in HTML?
    You can use character entities like ° or °, or use the Unicode directly.

  2. Is it necessary to use character entities for the degree Celsius symbol?
    No, you can also use the Unicode directly, provided your document is saved in UTF-8 encoding.

  3. Can I use CSS to add the degree Celsius symbol?
    Yes, you can use the ::after pseudo-element in CSS to append the degree symbol after a temperature value.

  4. What is the Unicode for the degree Celsius symbol?
    The Unicode for the degree symbol is U+00B0.

  5. Will the degree Celsius symbol display correctly on all browsers?
    Yes, if you use character entities or Unicode, the symbol should display correctly across all modern browsers.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn

Related Article - HTML Symbol