Reserved Characters
In HTML, certain characters are reserved for specific purposes and have special meanings. These reserved characters include:
- Less than (<): Used to define HTML tags.
- Greater than (>): Used to define HTML tags.
- Ampersand (&): Used to define HTML entities.
- Double quotes ("): Used to define attribute values.
- Single quote ('): Used to define attribute values.
To display these reserved characters on a web page, you need to use their corresponding HTML entities. This prevents the browser from interpreting them as part of the HTML syntax.
Here are the entity names and numbers for the reserved characters:
- Less than: < or <
- Greater than: > or >
- Ampersand: & or &
- Double quotes: " or "
- Single quote: ' or '
Example
<p>If x < 5, then x is less than 5.</p>
<p>The company's motto is "Quality & Innovation".</p>
In the first example, the less than symbol is represented using < to prevent it from being interpreted as the start of an HTML tag. In the second example, the double quotes and ampersand are represented using their respective entities to ensure proper rendering.
Other Characters
Apart from reserved characters, HTML entities can be used to represent various other characters, symbols & special characters that may not be easily accessible on a standard keyboard or that have special meanings in different contexts. These include:
- Currency symbols: € (€), £ (£), ¥ (¥)
- Mathematical symbols: + (+), − (-), × (×), ÷ (÷)
- Punctuation marks: – (–), — (—), ‘ ('), ’ ('), “ ("), ” (")
- Accented characters: à (à), é (é), ñ (ñ), ü (ü)
- Arrows: ← (←), → (→), ↑ (↑), ↓ (↓)
Using HTML entities for these characters ensures that they are displayed consistently across different browsers & devices, regardless of the user's keyboard layout or character encoding settings.
Example
<p>The price is €49.99 + tax.</p>
<p>The restaurant is open from 5:00 PM – 11:00 PM.</p>
<p>The Spanish word for "year" is “año”.</p>
In this example, the euro symbol (€), plus sign (+), en dash (–), double quotes, and the accented character "ñ" are represented using their respective HTML entities to ensure proper rendering.
Advantages and Disadvantages of HTML Entity
Using HTML entities has several advantages & disadvantages that web developers should consider when creating web pages.
Advantages
- Consistency: HTML entities ensure that special characters & symbols are displayed consistently across different browsers & devices, regardless of the user's system settings or character encoding.
- Readability: Using entity names instead of entity numbers can make the HTML code more readable & easier to understand, especially for commonly used symbols.
- Accessibility: HTML entities can be used to provide alternative text for images or icons, making the content more accessible to users with assistive technologies like screen readers.
- Compatibility: HTML entities are widely supported by browsers & have been a part of the HTML specification for a long time, ensuring compatibility with older browsers.
Disadvantages
- Complexity: Using HTML entities can make the code more complex & harder to read, especially if there are many entities used throughout the document.
- Memorization: Developers need to memorize or refer to the entity names or numbers for the characters they want to use, which can be time-consuming.
- Limited support: Some newer or less common characters may not have a corresponding HTML entity, requiring the use of other methods like CSS or Unicode to display them.
- Potential confusion: Mixing different types of entities (named vs. numeric) or using incorrect entity names/numbers can lead to confusion & display issues.
Non-breaking Space in HTML
A non-breaking space, represented as or  , is a unique type of HTML entity used to control text spacing in web content. Unlike a regular space, a non-breaking space prevents an automatic line break at its position. This is particularly useful in web design for a couple of reasons:
- Preventing Orphan Words: It keeps related words or elements together on the same line. For instance, ensuring that a person’s title stays with their name, like in "Dr. Smith," prevents "Dr." and "Smith" from being split into separate lines.
- Spacing and Alignment: Non-breaking spaces can be used to create visual consistency in the layout, especially in places where standard spaces might collapse in HTML, such as in navigation menus or layout designs.
Here are a few examples where non-breaking spaces can be used:
Keeping numbers & units together:
<p>The temperature is 25 °C.</p>
Keeping names or titles together:
<p>The event will be attended by Mr. John Smith, CEO of the company.</p>
Preventing awkward line breaks:
<p>The company's motto is "Quality & Innovation".</p>
In these examples, the non-breaking space ensures that the numbers & units (25 °C), the person's name & title (Mr. John Smith), & the company's motto (Quality & Innovation) stay together on the same line, improving readability & preventing awkward line breaks.
Note -: It's important to note that overusing non-breaking spaces can lead to layout issues, especially on smaller screens or when the content is resized. Use them sparingly & only when necessary to maintain the integrity of specific phrases or elements.
Combining Diacritical Marks
Combining diacritical marks, also known as combining diacritics or combining marks, are special characters in HTML that can be added to a base letter to create accented characters or modify their appearance. These marks are placed directly after the base letter & do not take up additional space.
HTML provides a wide range of combining diacritical marks that can be used to create various accented characters, such as those found in languages like French, Spanish, German & Vietnamese.
Here are a few examples of combining diacritical marks:
- Acute accent (´): ´ or ́
- Grave accent (`): ` or ̀
- Circumflex (^): ˆ or ̂
- Tilde (~): ˜ or ̃
- Umlaut/Diaeresis (¨): ¨ or ̈
To use combining diacritical marks, you place them immediately after the base letter. Here's an example:
<p>The French word for "école" is école.</p>
<p>The Spanish word for "año" is año.</p>
In the first example, the acute accent (́) is added to the letter "e" to create "é". In the second example, the tilde (̃) is added to the letter "n" to create "ñ".
Note -: It's important to note that not all browsers support combining diacritical marks, & some may display them incorrectly. In such cases, it's better to use the precomposed characters (e.g., é for "é" or ñ for "ñ") to ensure better compatibility.
Additionally, using combining diacritical marks can make the HTML code harder to read & maintain. It's often more convenient to use the precomposed characters or store the content in a character encoding that supports the required accented characters, such as UTF-8.
Frequently Asked Questions
What are HTML entities used for?
HTML entities are used to display characters that have special meanings in HTML or are not readily available on a keyboard, ensuring correct display & functionality in web documents.
Can I use HTML entities for all special characters?
Yes, HTML supports entities for a wide range of special characters, including symbols, foreign characters, & mathematical notations, ensuring they display correctly regardless of the user's system.
Do HTML entities affect website performance?
While entities slightly increase processing requirements, the impact on performance is minimal & should not significantly affect website load times.
Conclusion
In this article, we have learned about HTML entities, their importance in web development, & how they enhance the functionality & appearance of web content. We covered the use of entity names & numbers, reserved characters, other special characters, & combining diacritical marks. Understanding & utilizing these entities correctly ensures that web content is accessible & displayed as expected on different browsers and other platforms.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.