# Bad value for attribute “src” on element “img”: Illegal character in fragment: space is not allowed.

> Canonical HTML version: https://rocketvalidator.com/html-validation/bad-value-for-attribute-src-on-element-img-illegal-character-in-fragment-space-is-not-allowed
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

A space character in the fragment portion of a URL (the part after `#`) is not allowed in the `src` attribute of an `img` element.

URLs follow the syntax defined in [RFC 3986](https://www.rfc-editor.org/rfc/rfc3986), which does not permit literal spaces anywhere in the URI, including the fragment identifier. Browsers may silently handle spaces by percent-encoding them, but the raw HTML remains invalid. The W3C validator flags this because the `src` value must be a valid URL before the browser ever processes it.

To fix the issue, replace each space in the URL with `%20`, which is the percent-encoded form of a space character. This applies to all parts of the URL: the path, query string, and fragment.

A common cause is copying a URL from a browser address bar or a document system where filenames or anchors contain spaces. Another cause is linking to a named anchor (`id`) on a page where the `id` value itself contains a space, which is also invalid HTML — `id` values must not contain spaces.

## HTML examples

### Invalid: space in fragment

```html
<img src="diagram.svg#my section" alt="Diagram of section layout">
```

### Valid: percent-encoded space

```html
<img src="diagram.svg#my%20section" alt="Diagram of section layout">
```

If you control the target resource, a better fix is to remove the space from the fragment identifier entirely:

```html
<img src="diagram.svg#my-section" alt="Diagram of section layout">
```

Using hyphens or underscores instead of spaces in fragment identifiers and file names avoids this class of problem altogether.
