October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
accessibility

How to Import Images in HTML: Paths, URLs, Responsive Images, and Accessibility

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use an <img> element with a valid src and meaningful alt text:

<img src="images/photo.jpg" alt="A red bicycle beside a brick wall" width="800" height="600">

The src value can be a relative path to a file in your site or an absolute URL on another server. Add intrinsic dimensions to prevent layout shifts, and use srcset, sizes, or picture when the browser must choose among image files.

The basic HTML image

For one image resource, HTML uses the img element and its src attribute. The element is void, so it does not need a closing tag.

<img src="images/photo.jpg" alt="A red bicycle beside a brick wall" width="800" height="600">
  • src identifies the image resource.
  • alt supplies an accessible text equivalent and appears when the image cannot load.
  • width and height describe the intrinsic aspect ratio and let the browser reserve space before downloading.

The URL must resolve to a non-empty, valid image resource. Static or animated bitmap formats and a single-page vector image can be used. Scripted SVG, multipage PDF, HTML, and plain-text documents are not conforming image resources for img.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Import a local image with a relative path

A relative URL is resolved from the URL of the HTML document, not from your computer’s source-project window. A simple site might be arranged like this:

project/
├── index.html
└── images/
    └── photo.jpg

Because index.html and images are siblings, use:

<img src="images/photo.jpg" alt="Mountain reflected in a lake">

Nested pages

If the document is at articles/travel/index.html and the image remains at images/photo.jpg in the project root, go up two directories:

<img src="../../images/photo.jpg" alt="Mountain reflected in a lake">

A leading slash such as /images/photo.jpg is root-relative to the website’s domain. It is useful when every page serves from the same site root, but it can break when the application is deployed under a subdirectory. A relative path is usually safer for portable static projects.

Paths that commonly fail

  • Use forward slashes, even on Windows: images/photo.jpg.
  • Match spelling and capitalization exactly. A file named Photo.JPG is different from photo.jpg on many servers.
  • Do not point at a file that exists only on your laptop; copy it into the deployed site or use a reachable remote URL.
  • URL-encode spaces and special characters, or preferably use simple names such as hero-summer.jpg.

Import an image from another website

Use the complete HTTPS URL when the image is hosted on a CDN or another domain:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<img src="https://cdn.example.com/products/lamp.webp" alt="White desk lamp" width="1200" height="900">

The remote server must be reachable and return an actual image with an appropriate content type. Hotlink protection, authentication, a redirect to an HTML error page, or a removed file can make a syntactically correct tag appear broken. You generally do not need CORS merely to display an image, but a cross-origin image used by canvas requires the server’s permission and an appropriate crossorigin setup.

Write useful alternative text

alt is not a caption or a filename. Describe the information the image contributes to someone who cannot see it.

Informative image

<img src="chart.png" alt="Sales rise from January through June, then level off in July">

Functional image

If an image is the content of a link or button, describe its action:

<a href="/home"><img src="logo.svg" alt="Home"></a>

Decorative or redundant image

Use an empty value when the image adds no information or repeats text already present:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<img src="ornament.svg" alt="">

Never omit alt accidentally and never use a filename such as IMG_4821.jpg as a substitute for a description.

Make images responsive with srcset and sizes

When the same image is available at several resolutions, list candidates with width descriptors. Tell the browser how wide the image slot will be with sizes:

<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w"
  sizes="(max-width: 600px) 100vw, 800px"
  alt="A red bicycle beside a brick wall"
  width="800"
  height="533">

The browser combines the viewport, device pixel ratio, network conditions, and the declared slot width to select a candidate. With w descriptors, sizes is required. A density descriptor can be used when the displayed size is fixed:

<img src="logo.png" srcset="logo.png 1x, [email protected] 2x" alt="Example company" width="200" height="80">

Keep the fallback src usable for browsers or situations where candidate selection is unavailable.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use picture for formats and art direction

Use picture when you need different crops for different layouts or want to offer modern formats with a fallback:

<picture>
  <source media="(max-width: 600px)" srcset="portrait-crop.jpg">
  <source type="image/avif" srcset="photo.avif">
  <source type="image/webp" srcset="photo.webp">
  <img src="photo.jpg" alt="A red bicycle beside a brick wall" width="1200" height="800">
</picture>

The browser evaluates source elements in order and uses the nested img as the required fallback. A media condition is for art direction; a type condition is for format support. Do not put the alt on source; it belongs on the fallback img.

Performance and layout stability

Reserve the correct space

Set accurate width and height values, even when CSS later makes the image fluid:

Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers
img {
  max-width: 100%;
  height: auto;
}

The attributes preserve the aspect ratio while the file is loading and reduce cumulative layout movement. Incorrect dimensions can reserve the wrong shape and cause a visible jump when the image arrives.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Lazy-load only suitable images

For images well below the initial viewport, use:

<img src="gallery-01.jpg" alt="Coastal trail" width="1200" height="800" loading="lazy">

Do not lazily load a prominent hero or other image needed immediately; delaying it can hurt perceived loading. The browser fetches a lazy image as it approaches the viewport.

Control decoding and priority when justified

HTML also supports decoding="async", decoding="sync", and decoding="auto", plus fetchpriority, referrerpolicy, and crossorigin. These are tuning controls, not replacements for correctly sized files and an accurate responsive strategy. Use them only when you understand the loading trade-off for that image.

Diagnose a broken image

  1. Open the final image URL directly in a browser. If it fails there, fix the URL, deployment, permissions, or server response first.
  2. Inspect the browser’s Network panel. A 404 usually means a path or case error; a redirect to an HTML page means the endpoint is not an image.
  3. Confirm the response has an image content type and contains image bytes rather than an access-denied message.
  4. Check the document’s actual URL and recompute every ../ segment from that location.
  5. For remote images, test whether hotlink protection, authentication, a firewall, or a temporary outage blocks the request.
  6. If the image loads but appears distorted, compare its intrinsic dimensions with the HTML width and height, then verify your CSS.

When srcset chooses the wrong file

Check that every candidate has the correct descriptor and that sizes describes the rendered slot, including media-query changes. A browser may deliberately choose a smaller or larger file based on device pixel ratio and network conditions; that is expected behavior, not necessarily an error.

When a picture fallback is missing

Ensure the picture contains an img. Without it, there is no universal fallback or location for the required alternative text.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choosing the right element

Need Use Key detail
One fixed resource img src Provide meaningful alt and dimensions.
Same scene at several resolutions img srcset sizes Use width descriptors and required sizes.
Different crops or formats picture source img Keep an img fallback with alt.
Decorative visual img alt="" Do not expose redundant content to assistive technology.

Or skip the browser setup

If you need a rendered screenshot of a page rather than an image embedded in your own HTML, ScreenshotNeo provides a website screenshot API and MCP server. One GET request returns PNG, JPEG, WebP, or PDF; it accepts consent banners before capture and removes more than 60 known consent platforms, newsletter popups, and chat widgets. Bot checks, CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers identify the page verdict and billing status.

Example request (see the ScreenshotNeo API documentation):

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

You can also use an MCP client such as Claude or Cursor with ScreenshotNeo’s take_screenshot, get_page_info, and capture_pdf tools. The service supports full-page and element captures, device presets, custom viewport and retina scale, PDF settings, HTML/CSS rendering, JavaScript and clicks, selector waits, request blocking, headers, cookies, user agents, timezone and geolocation, transparent backgrounds, resizing, TTL caching, signed links, asynchronous webhooks, bulk capture of up to 100 URLs per call, usage data, and an OpenAPI specification.

The Free plan includes 1,000 screenshots per month with no card. Paid plans start at $5 for 3,000 shots; every feature is included on every plan. Create a free ScreenshotNeo account.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Can an HTML image use a local computer file path?

Not reliably on a deployed website. Use a file included in the site’s served directory with a relative or root-relative URL, or use a reachable HTTPS URL.

Is an image URL required to end in .jpg, .png, or another extension?

No. The server response and actual bytes determine whether it is a valid image; an extension is helpful but not decisive.

Should every image have a non-empty alt attribute?

Every image needs an alt value. Use descriptive text for informative or functional images and an empty value (alt="") for purely decorative or redundant images.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read next

Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.