Book a call Free rebuild
Home/Glossary/Design/Lossless Compression

Lossless Compression

December 30, 2025 Updated November 16, 2026 3 min read

Design glossaryLossless compression makes a file smaller without losing any data, so the original can be restored exactly. PNG, ZIP, and gzip use it.

Lossless compression shrinks data by storing it more efficiently, not by throwing any of it away. When the file is decompressed, every bit comes back exactly as it was. That makes it the only acceptable choice for text, code, databases, and software, where a single changed byte can break the file, and a good choice for images that need perfect edges, such as logos and screenshots.

On a website, lossless compression is everywhere: in PNG images, in the gzip or Brotli compression servers apply to HTML, CSS, and JavaScript, and in the ZIP files used to install themes and plugins.

How it works

Real data is full of repetition and predictability. Lossless algorithms find it and describe it in fewer bits. The main techniques, often combined:

  • Run-length encoding (RLE): replaces runs of the same value with a count. A row of pixels WWWWWBWWWW becomes 5W1B4W. Simple, and effective on flat areas of color.
  • Dictionary methods (the Lempel-Ziv family): replace repeated sequences with short references to an earlier occurrence. LZW is used in GIF; LZ77 is the basis of DEFLATE.
  • Entropy coding: gives common symbols short codes and rare ones longer codes. Huffman coding is the classic method; arithmetic coding and its relatives squeeze out a little more.
  • Prediction: guesses each value from its neighbors and stores only the small difference. PNG applies filters like this before compressing, and lossless audio formats such as FLAC do the same with sound samples.

DEFLATE, which combines LZ77 and Huffman coding, sits inside ZIP, gzip, and PNG. Brotli and Zstandard are newer general-purpose algorithms that usually compress further.

Lossless vs lossy

Lossy compression permanently removes detail to reach much smaller sizes, which is why photos and video use it. Lossless keeps everything, so files stay larger. For a photograph, a lossless PNG can be several times the size of a good-quality JPEG or WebP that looks the same. For a logo or a screenshot full of text, lossless often wins on both size and sharpness.

Limits

  • No algorithm can shrink everything. Data that is already compressed, encrypted, or random has no patterns left to exploit, and compressing it again can make it slightly larger.
  • Ratios depend on the content. Text and code compress very well; photos and audio compress only modestly without loss.
  • Speed varies. Higher compression levels take longer to encode, which matters for files compressed on the fly by a server. Decompression is usually fast.

Lossless compression on websites

  • Make sure the server or CDN sends HTML, CSS, JavaScript, JSON, and SVG with gzip or Brotli. Most hosts and CDNs, including Cloudflare, do this by default.
  • Use PNG, or lossless WebP, for graphics that need crisp edges or transparency. Lossless WebP is usually smaller than the equivalent PNG; see WebP vs PNG.
  • Run PNGs through an optimizer before upload. Tools that recompress them more thoroughly can cut size without changing a single pixel.
  • Keep master images lossless and export lossy versions for delivery, so you can always go back to the original.

FAQs

What is lossless compression, and why is it important?

Lossless compression makes a file smaller without removing any data, so the original can be restored exactly. It is essential for anything that must stay byte-for-byte correct, such as text, code, databases, software, and archives, and it is the right choice for images that need perfect edges.

How is lossless compression different from lossy compression?

Lossless compression keeps all of the original data; lossy compression permanently discards detail to reach much smaller files. Lossy suits photos, audio, and video, where small losses go unnoticed. Lossless suits text, code, and graphics, where any change would be a defect.

What are the most common lossless compression techniques?

The main techniques are run-length encoding, dictionary methods from the Lempel-Ziv family (such as LZ77 and LZW), entropy coding (such as Huffman and arithmetic coding), and prediction. Formats combine them: DEFLATE, used in ZIP, gzip, and PNG, pairs LZ77 with Huffman coding.

When should you use lossless compression instead of lossy compression?

Use lossless whenever the data must come back exactly: source code, documents, databases, backups, and software. For images, use it for logos, icons, screenshots, and diagrams, and for master copies you will edit again. Photos for the web are usually better served lossy.

What are the limitations of lossless compression?

Lossless files are larger than lossy ones, sometimes by several times for photos. How much a file shrinks depends on its content, and data that is already compressed or encrypted barely shrinks at all. Higher compression settings also take longer to encode.

WP Services

WordPress services at revolutionary pricing.

Productized WordPress services, from builds to care, delivered by Seahawk and tracked in one app. For the WordPress lovers.