Skip to content

08. Case Insensitivity

HTML is case-insensitive, meaning that tag names and attribute names can be written in uppercase, lowercase, or any combination. However, while the HTML specification allows this flexibility, modern web development follows consistent conventions for readability and maintainability. Understanding case insensitivity helps you write clean, professional HTML code.

In HTML, element names and attribute names are case-insensitive. This means all of these are equivalent:

<!-- All of these are valid and equivalent -->
<P>Paragraph</P>
<p>Paragraph</p>
<P>Paragraph</p>
<p>Paragraph</P>

Similarly for attributes:

<!-- All equivalent -->
<div CLASS="container">
<div class="container">
<div Class="container">
<div CLASS="container">

HTML element names are case-insensitive:

<!-- All valid -->
<DIV>Content</DIV>
<div>Content</div>
<Div>Content</Div>
<DIV>Content</div>

Attribute names are also case-insensitive:

<!-- All equivalent -->
<img SRC="image.jpg" ALT="Description">
<img src="image.jpg" alt="description">
<img Src="image.jpg" Alt="Description">

Important: Attribute values are case-sensitive when they have specific meanings:

<!-- Case-sensitive: Different types -->
<input type="text"> <!-- lowercase -->
<input type="TEXT"> <!-- Still works, but inconsistent -->
<input type="email"> <!-- lowercase -->
<input type="Email"> <!-- May not work as expected -->
<!-- Case-sensitive: URLs and IDs -->
<a href="#section1">Link</a> <!-- Works -->
<a href="#Section1">Link</a> <!-- Different target! -->

While HTML is case-insensitive, the modern standard is to use lowercase for all HTML elements and attributes:

<!-- Modern standard: lowercase -->
<div class="container">
<p>This is a paragraph.</p>
<img src="image.jpg" alt="Description">
</div>
<!-- Avoid: Mixed case -->
<DIV CLASS="container">
<P>This is a paragraph.</P>
<IMG SRC="image.jpg" ALT="Description">
</DIV>
  1. Consistency: Industry standard convention
  2. Readability: Easier to read and write
  3. XHTML compatibility: XHTML requires lowercase
  4. Tool compatibility: Many tools expect lowercase
  5. Team standards: Easier for teams to maintain consistency

XHTML (eXtensible HTML) requires lowercase for all elements and attributes:

<!-- XHTML: Must be lowercase -->
<div class="container">
<p>Content</p>
</div>
<!-- XHTML: Invalid -->
<DIV CLASS="container">
<P>Content</P>
</DIV>

Even if you’re writing HTML5, using lowercase ensures compatibility and follows best practices.

CSS is case-sensitive for selectors and property names:

/* Case-sensitive: Class names */
.container { } /* Matches class="container" */
.Container { } /* Different selector! */
/* Case-sensitive: Property names */
color: red; /* Valid */
Color: red; /* Invalid */

JavaScript is case-sensitive:

// Case-sensitive: Variable names
const myVar = "value";
const MyVar = "different value"; // Different variable!
// Case-sensitive: DOM methods
document.getElementById('myId'); // Correct
document.GetElementById('myId'); // Error!

URLs and file paths may be case-sensitive depending on the server:

<!-- Server-dependent: May be case-sensitive -->
<img src="Image.jpg"> <!-- May not work -->
<img src="image.jpg"> <!-- Safer -->

Problem:

<DIV class="container">
<P>Paragraph</P>
<IMG src="photo.jpg">
</DIV>

Solution:

<div class="container">
<p>Paragraph</p>
<img src="photo.jpg">
</div>

Problem:

<input type="Email"> <!-- May not work as expected -->
<a href="#Section1"> <!-- Different target than #section1 -->

Solution:

<input type="email"> <!-- Use lowercase -->
<a href="#section1"> <!-- Be consistent with IDs -->

Problem:

<div class="Container"> <!-- HTML -->
.container { } /* CSS - won't match! */

Solution:

<div class="container"> <!-- Match CSS exactly -->

Always use lowercase for HTML elements and attributes:

<!-- Good -->
<div class="container">
<h1>Title</h1>
<p>Content</p>
</div>
<!-- Avoid -->
<DIV CLASS="container">
<H1>Title</H1>
<P>Content</P>
</DIV>

For attribute values that have meaning (like type, id, class), be consistent:

<!-- Good: Consistent lowercase -->
<input type="email" id="user-email" class="form-input">
<!-- Avoid: Mixed case -->
<input type="Email" id="User-Email" class="Form-Input">

Ensure HTML class and ID names match CSS and JavaScript exactly:

<!-- HTML -->
<div id="mainContent" class="container">
/* CSS - must match exactly */
#mainContent { }
.container { }
// JavaScript - must match exactly
document.getElementById('mainContent');
document.querySelector('.container');

The HTML5 DOCTYPE is case-insensitive but conventionally lowercase:

<!-- All valid, but use lowercase -->
<!DOCTYPE html>
<!doctype html>
<!Doctype Html>

Convention: Use <!DOCTYPE html> (lowercase).

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Web Page</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<header>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
</ul>
</nav>
</header>
<main>
<article>
<h1>Article Title</h1>
<p>Article content.</p>
</article>
</main>
<footer>
<p>&copy; 2024 My Website</p>
</footer>
</body>
</html>
  • HTML is case-insensitive: Element and attribute names can be any case
  • Use lowercase: Modern convention and best practice
  • Attribute values: May be case-sensitive depending on context
  • CSS/JavaScript: Always case-sensitive
  • Be consistent: Use the same casing throughout your project