Home >Web Front-end >HTML Tutorial >How do you use character entities in HTML (e.g., , <, >)?
Character entities in HTML are used to display reserved characters or symbols that cannot be directly included in the HTML source code. These are typically represented with an ampersand (&) followed by either a mnemonic or a numeric reference, and concluded with a semicolon (;). Here’s how you use some common character entities:
&lt;p&gt;Hello World&lt;/p&gt;
ensures that "Hello" and "World" stay on the same line. in text, you would write &lt;p&gt;&lt;/p&gt;
.
as text, you would use
.These entities help browsers render text and symbols correctly that would otherwise be interpreted as HTML code.
Here are some common HTML character entities and their uses:
©
to display a copyright symbol.© 2023 John Doe
.® BrandName
.&lt;a title='&quot;Quote&quot;'&gt;Link&lt;/a&gt;
.&lt;a title=&quot;It's great&quot;&gt;Link&lt;/a&gt;
.These entities ensure that special characters are displayed correctly and prevent them from being interpreted as HTML code.
Character entities play a crucial role in preventing HTML injection attacks, such as cross-site scripting (XSS). HTML injection occurs when malicious code is inserted into a website through user input. By converting special characters into their corresponding HTML entities, you can prevent browsers from interpreting these characters as code. Here's how it helps:
, &lt;code&gt;&gt;
, &
, '
and "
into , &lt;code&gt;&gt;
, &
, '
, and "
respectively, ensures that any injected HTML or JavaScript code is rendered as plain text instead of being executed.
<script>alert('XSS')</script>
, by converting it to <script>alert(&amp;apos;XSS&amp;apos;)</script>
, the browser displays the text rather than executing the script.This practice is known as "escaping" and is a standard security measure in web development to safeguard against HTML injection vulnerabilities.
A comprehensive list of HTML character entities can be found in several authoritative sources:
These resources provide detailed information about HTML character entities, including their numeric and named representations, making them invaluable for web developers and designers.
The above is the detailed content of How do you use character entities in HTML (e.g., , <, >)?. For more information, please follow other related articles on the PHP Chinese website!