HTML Guides for required attribute
Learn how to identify and fix common HTML validation errors flagged by the W3C Validator — so your pages are standards-compliant and render correctly across every browser. Also check our Accessibility Guides.
The HTML specification mandates src on the <img> element because an image without a source has nothing to render. Omitting it produces invalid markup and unpredictable browser behavior — some browsers display a broken image icon, while others render nothing at all.
This issue commonly occurs in a few scenarios:
- Templating placeholders — A developer adds an
<img>tag intending to populate thesrcdynamically but forgets to set a default value. - Lazy loading implementations — Some lazy-loading scripts store the real URL in a
data-srcattribute and omitsrcentirely, which results in invalid HTML. - Incomplete markup — The attribute is simply forgotten during development.
How to fix it
- Add a
srcattribute with a valid URL pointing to your image. - If you're using lazy loading and want to defer the actual image source, set
srcto a lightweight placeholder (such as a tiny transparent image or a low-quality preview) and use the nativeloading="lazy"attribute instead of removingsrc.
Examples
❌ Missing src attribute
<img alt="A sunset over the ocean">
This triggers the validation error because src is absent.
❌ Source stored only in a data- attribute
<img data-src="/images/photo.jpg" alt="A sunset over the ocean">
While data-src is a valid custom data attribute, it does not satisfy the requirement for src. The validator will still report the error.
✅ Correct usage with src
<img src="/images/photo.jpg" alt="A sunset over the ocean">
✅ Lazy loading with a placeholder src
<img
src="/images/photo-placeholder.jpg"
data-src="/images/photo-full.jpg"
alt="A sunset over the ocean"
loading="lazy">
Here, src points to a small placeholder image so the markup stays valid, while data-src holds the full-resolution URL for your lazy-loading script to swap in.
✅ Native lazy loading (no JavaScript needed)
<img src="/images/photo.jpg" alt="A sunset over the ocean" loading="lazy">
Modern browsers support the loading="lazy" attribute natively, so you can keep a valid src and still defer off-screen images without any custom scripting.
Required attributes exist because they provide information that is fundamental to the element's purpose. For example, an <img> element without a src attribute has no image to display, and without an alt attribute, assistive technologies have no fallback text to describe the image. A <link> element without rel gives the browser no way to understand the relationship of the linked resource. When you omit a required attribute, several things can go wrong:
- Accessibility issues: Screen readers and other assistive technologies depend on specific attributes to convey meaning. A missing
alton<img>leaves visually impaired users without any description of the content. - Broken functionality: Some elements simply won't work without their required attributes. A
<form>with an<input>that's missing atypeattribute may still render (browsers default totype="text"), but other elements like<map>withoutnamewon't function as intended. - Standards non-compliance: Omitting required attributes produces invalid HTML, which can lead to unpredictable rendering across different browsers.
To fix this issue, identify which attribute is missing from the element flagged by the validator, then add it with an appropriate value. Here are common elements and their required attributes:
| Element | Required Attribute(s) |
|---|---|
<img> | src, alt |
<link> | rel |
<script> | src (if no inline content) |
<input> | type (recommended) |
<meta> | charset, name, or http-equiv (at least one) |
<map> | name |
<bdo> | dir |
<style> | None in HTML5, but type was required in HTML4 |
Examples
Missing alt attribute on <img>
This is one of the most common validation errors. The alt attribute is required on every <img> element.
❌ Incorrect — missing alt attribute:
<img src="photo.jpg">
✅ Correct — alt attribute provided:
<img src="photo.jpg" alt="A sunset over the ocean">
If the image is purely decorative and carries no meaningful content, use an empty alt attribute:
<img src="decorative-border.png" alt="">
Missing rel attribute on <link>
The rel attribute tells the browser what the linked resource is for.
❌ Incorrect — missing rel attribute:
<link href="styles.css">
✅ Correct — rel attribute provided:
<link rel="stylesheet" href="styles.css">
Missing name attribute on <map>
The <map> element requires a name attribute so it can be referenced by an <img> element's usemap attribute.
❌ Incorrect — missing name attribute:
<map>
<area shape="rect" coords="0,0,100,100" href="/section" alt="Section">
</map>
✅ Correct — name attribute provided:
<map name="navigation">
<area shape="rect" coords="0,0,100,100" href="/section" alt="Section">
</map>
<img src="nav.png" alt="Site navigation" usemap="#navigation">
Missing dir attribute on <bdo>
The <bdo> (bidirectional override) element exists specifically to override text direction, so the dir attribute is essential.
❌ Incorrect — missing dir attribute:
<p>The word in Arabic is <bdo>مرحبا</bdo>.</p>
✅ Correct — dir attribute provided:
<p>The word in Arabic is <bdo dir="rtl">مرحبا</bdo>.</p>
When you encounter this validation error, read the validator message carefully — it will tell you exactly which element and which attribute is missing. Add the attribute with a meaningful value appropriate to your content, and revalidate to confirm the issue is resolved.
Validate at scale.
Ship accessible websites, faster.
Automated HTML & accessibility validation for large sites. Check thousands of pages against WCAG guidelines and W3C standards in minutes, not days.
1K Weekly
1,000 monthly checks. Cancel anytime.
Get 1K Weekly →Join teams across 40+ countries