ScreenshotNeo

BlogHow-to

How to Put a PNG in HTML

Embed a PNG with the right path, accessible alt text, responsive sizing, troubleshooting steps, and a ScreenshotNeo option for generated screenshots.

By the ScreenshotNeo team30 September 20268 min read

How to Put a PNG in HTML

Use the HTML <img> element and set its src attribute to the PNG’s path or URL:

<img src="images/photo.png" alt="A red kite flying over a field">

The img element is a void element, so it has no closing tag. The src value must point to an image the browser can reach. It may be a relative path inside your site or an absolute HTTPS URL. Add useful alternative text, and include intrinsic dimensions when you know them. This is the core guidance in MDN’s img reference and its HTML images tutorial.

1. Add a PNG with a relative path

A relative path is resolved from the page URL context. Suppose your project looks like this:

A relative image path is resolved from the page URL to the PNG file.
A relative image path is resolved from the page URL to the PNG file.
site/
├── index.html
└── images/
    └── logo.png

Use:

<img src="images/logo.png" alt="Company logo">

If the PNG sits beside the HTML file, omit the directory:

<img src="logo.png" alt="Product screenshot">

For a file in a parent directory, use ..:

<img src="../images/logo.png" alt="Company logo">

These paths are relative to the URL of the document, which can differ from the physical folder you are viewing in an editor. A page at /docs/start.html resolving images/diagram.png requests /docs/images/diagram.png.

2. Use an absolute URL for a hosted PNG

If the image is hosted on a server or CDN, place its complete URL in src:

<img
  src="https://static.example.com/assets/diagram.png"
  alt="Request flow diagram"
  width="1200"
  height="675"
>

Do not use a local filesystem path from your computer such as C:\Users\Sam\Desktop\photo.png or /Users/sam/photo.png as a public website URL. Visitors’ browsers cannot access your computer’s files. Upload the file to your web host, object storage, or CDN and use the resulting reachable URL.

3. Write useful alternative text

The alt attribute is read by screen readers and appears when an image cannot load. Describe the image’s content or purpose in context:

<img src="team.png" alt="The support team at the 2025 off-site">

Avoid values such as image, photo, or the raw filename. If nearby text already conveys the same information and the image is purely decorative, use an empty value:

<img src="ornament.png" alt="">

Do not omit alt on a meaningful image. The MDN image guidance covers choosing text that communicates the image’s relevant content rather than its file name.

4. Reserve layout space with width and height

When you know the PNG’s intrinsic dimensions, include both attributes:

<img
  src="hero.png"
  alt="Dashboard showing monthly revenue"
  width="1600"
  height="900"
>

The browser can reserve the correct aspect-ratio box before downloading the file, reducing layout movement. CSS still controls the displayed size:

.hero {
  display: block;
  width: 100%;
  max-width: 960px;
  height: auto;
}

Keep the width-to-height ratio consistent with the actual file. Distorting a PNG usually means one dimension was forced without the other being calculated automatically.

5. Offer responsive PNG variants with srcset

A single large PNG works everywhere but can waste bandwidth on a small screen. Provide candidates with srcset and describe the rendered slot with sizes:

<img
  src="chart-800.png"
  srcset="chart-400.png 400w,
          chart-800.png 800w,
          chart-1600.png 1600w"
  sizes="(max-width: 700px) 100vw, 800px"
  width="1600"
  height="900"
  alt="Quarterly revenue chart"
>

The width descriptors (400w, 800w) tell the browser each candidate’s intrinsic width. sizes tells it how wide the image is expected to appear, so it can select an appropriate file. Keep the plain src as the fallback and as a useful default for older or simpler clients.

6. Use picture for viewport or format choices

Use picture when the source should change for a media condition or when you want to offer another format while retaining a PNG fallback:

<picture>
  <source media="(max-width: 700px)" srcset="diagram-mobile.png">
  <source type="image/webp" srcset="diagram.webp">
  <img
    src="diagram.png"
    width="1200"
    height="675"
    alt="Architecture diagram"
  >
</picture>

The nested img is required fallback behavior. The browser evaluates matching source elements and uses the fallback when none applies or when a format is unsupported. See MDN’s image format guide for format-selection details.

7. Complete HTML example

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>PNG example</title>
  <style>
    .article-image {
      display: block;
      width: min(100%, 900px);
      height: auto;
    }
  </style>
</head>
<body>
  <main>
    <h1>Release notes</h1>
    <img
      class="article-image"
      src="images/release.png"
      srcset="images/release-480.png 480w,
              images/release-900.png 900w,
              images/release-1800.png 1800w"
      sizes="(max-width: 900px) 100vw, 900px"
      width="1800"
      height="1000"
      alt="New release dashboard with a blue status panel"
    >
  </main>
</body>
</html>

8. Common errors and fixes

Symptom Likely cause Fix
Broken-image icon Wrong relative path Resolve the path from the page URL, not from your editor’s project tab. Check every directory name.
Works locally, fails after deployment Filename case differs Match capitalization exactly. A server can treat Logo.png and logo.png as different files.
404 in DevTools File was not uploaded or URL is wrong Open the exact src URL in a browser and verify the deployed file exists there.
Image appears stretched Only one dimension is forced or ratio is incorrect Set height: auto, or provide matching intrinsic width and height.
Alt text is unhelpful Filename or generic phrase used Describe the visible content or purpose. Use alt="" only for decorative images.
Image still does not render at a valid URL Empty source, access/network failure, corrupted data, or unsupported format Inspect the network response and file bytes, confirm the server returns an image content type, and try a known-good PNG. MDN lists these failure classes in its reference.

In browser developer tools, check the Network panel for the request status, final URL, response headers, and whether a redirect or permission error occurred. An HTML error page saved with a .png name is not a valid PNG.

9. Performance and reliability checklist

  • Export only the dimensions you need. A huge PNG costs more bandwidth and decoding time.
  • Use srcset for several display widths and sizes for width-descriptor candidates.
  • Set width and height to reserve space and reduce layout shifts.
  • Keep important images discoverable in normal HTML. Lazy loading can help below-the-fold images, but do not defer the main above-the-fold image unnecessarily.
  • Serve the file from a stable HTTPS URL and retain the correct PNG content type.
  • Use CSS for presentation; do not edit the bitmap repeatedly just to change its displayed width.
  • Test a production build, because case sensitivity, rewritten paths, and CDN rules often differ from a local server.

10. When the PNG is generated from a webpage

If your goal is to place a screenshot of a live webpage into HTML, you first need a capture step that produces a PNG (or another image format), then use the resulting file exactly as shown above. A browser automation workflow must load the page, wait for its content, handle consent dialogs, capture the viewport or full page, save the image, and publish it at a URL your HTML can reach. The output HTML remains simple:

<img src="/screenshots/pricing.png" alt="Pricing page captured on September 30, 2026">

11. Or skip the browser setup

ScreenshotNeo is a website screenshot API. One GET request returns a PNG, JPEG, WebP, or PDF. Cookie and consent banners, newsletter popups, and chat widgets are removed before the shot. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers identify the page verdict and whether it was billed. It also provides an MCP server for AI agents, including Claude, Cursor, and other MCP clients.

A screenshot service can clean page overlays before returning an image for your HTML.
A screenshot service can clean page overlays before returning an image for your HTML.

Read the complete option list and request details in the ScreenshotNeo documentation. The basic calls below save a PNG-compatible image response; change the target URL as needed.

cURL

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

Python

import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
r.raise_for_status()
open("shot.webp", "wb").write(r.content)

Node.js

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot failed: ${res.status}`);
const fs = await import('node:fs/promises');
await fs.writeFile('shot.webp', Buffer.from(await res.arrayBuffer()));

For production capture, use the available controls for full-page screenshots with lazy images loaded, CSS-element capture, dark mode, device presets or custom viewports, retina scale, custom CSS and JavaScript, click actions, selector hiding, selector or network-idle waits, request blocking, headers, cookies, user agents, authorization, timezone, geolocation, transparent backgrounds, resizing, caching with a chosen TTL, signed image links, asynchronous jobs with signed webhooks, bulk capture of up to 100 URLs per call, usage reporting, and PDF output.

Pricing starts with 1,000 shots per month free without a card. Paid plans start at $5 for 3,000 shots; yearly billing gives two months free, and every feature is included on every plan. If a capture is a bot check, blank page, timeout, failed load, or cache hit, it is not billed. Create a free ScreenshotNeo account and get 1,000 screenshots each month with no card.

12. FAQ

Can I put a PNG outside the HTML folder?

Yes. Use a correct relative path such as ../images/photo.png, or use an absolute URL hosted by a server you control.

Does PNG require a special HTML tag?

No. A normal <img> element is sufficient. Use srcset or picture only when you need responsive candidates or conditional sources.

Should I use a data URL?

A data URL can embed small assets directly, but it makes HTML larger and less cacheable. A normal file URL is usually easier to maintain.

Why is my image request successful but the picture is blank?

Inspect the response bytes and content type. A successful HTTP status can still return corrupted data or an HTML error document instead of a PNG.

Can CSS replace alt text?

No. CSS controls presentation; meaningful alternative text belongs on the HTML image element.