How to convert a GIF to Base64
- Load the GIF. Drop it on the box above, paste it, or pick a file, or look at the built-in sample first. The facts panel states the file's real format, read from its own first bytes, plus its size and, for a GIF, its dimensions, frame count and looping.
- Read the verdict. The readout states the string's exact length and the overhead percentage for your file, and gives a plain verdict: small enough to inline, inline with care, or too heavy to inline. That verdict is computed from your file, not copied from a rule of thumb.
- Copy the form you need. The data URI for most jobs, the raw string for an API or a template that adds its own prefix, or the finished img tag, CSS rule or JSON value. Each has its own copy button and character count.
- Or download the string. The download saves the full data URI as a plain text file, which is the safer route for a long string: no risk of a chat window or a form field quietly cutting it in half.
What Base64 does to the size, exactly
Base64 writes binary data in a 64-character alphabet, 6 bits at a time, so every 3 bytes of a file become 4 characters of text. The encoded length is not an estimate: it is 4 x ceil(bytes / 3), a third more than the file, every time. The data URI form adds the prefix data:image/gif;base64, on top, 22 characters for a GIF. A 30 KB spinner becomes a string of about 40,000 characters; a 1 MB GIF becomes about 1.4 million. Nothing is compressed and nothing is lost in either direction: decode the string and you have your file back, byte for byte.
The size is only half the cost. An inlined GIF cannot be cached separately: it is part of the HTML, CSS or JSON file that holds it, so a returning visitor downloads those characters again every time that file is fetched, and the browser cannot start showing the image until it has parsed its way through the string. A hosted GIF file, by contrast, is downloaded once and reused from the cache on every page that references it. That is the real trade behind the verdict thresholds on this page: inlining buys you one fewer request, and charges you a third more bytes, paid again on every uncached load.
When inlining a GIF makes sense
- A tiny image that would otherwise cost a whole request: a loading spinner, a small icon, a one-frame badge. Under about 50 KB, the saved request is worth more than the extra third.
- A single-file deliverable: a demo, a report or a template that must travel as one file, where there is nowhere to host the GIF separately.
- Data that is already text: a JSON config, an API payload or a database field that expects a string, where the GIF is small and the alternative is a second system to fetch it from.
- An image used exactly once, on one page, that is needed the moment the page renders and would visibly lag as a separate request.
When it does not
- The GIF is used on more than one page. Inlined, it is downloaded with every one of those pages; hosted, it is downloaded once and cached.
- The GIF is big. Above a few hundred KB the string bloats the host file, delays its parsing, and pushes everything after it later. Shrink it first, or host it.
- The page already loads fast. Inlining removes a request that modern HTTP already handles cheaply, and pays a third more bytes for the privilege.
- You need the image indexed or shared on its own. A data URI has no URL of its own: nothing can link to it, bookmark it or find it.
Base64 GIFs in email: the honest version
Embedding a GIF in an HTML email as a data URI is the use case this conversion is most often sold on, and it is the one to be careful with. Support for data URI images across email clients is inconsistent: many webmail and mobile clients render them, but some widely used desktop clients, Outlook on Windows among them historically, do not render data URI images at all, and a recipient on such a client sees a blank box where your animation should be. The string also inflates the email's HTML by a third more than the GIF, and some sending systems clip long messages. So: use a data URI in email only for a small GIF, only after sending yourself a test in the clients your audience actually uses, and keep a hosted image as the plan anywhere the animation carries the message.
How to use each string
In HTML
The data URI goes where the file's address would go. The img tag this tool hands you looks like this, with your GIF's string where the dots are:
<img src="data:image/gif;base64,R0lGODlh..." alt="">
The opening R0lGODlh is not a coincidence: every GIF89a file starts with the same six bytes, so every GIF data URI starts with those same characters (a GIF87a starts R0lGODdh). If your string starts with anything else, it is not a GIF, whatever its name said.
In CSS
.spinner { background-image: url("data:image/gif;base64,R0lGODlh..."); }
The rule this tool hands you is the background-image line on its own, ready to drop into your own selector. Keep the quotes: a Base64 string can contain characters a browser would rather see inside them.
In JSON and JavaScript
{ "image": "data:image/gif;base64,R0lGODlh..." }
A data URI is a plain string, so it drops into a JSON value, a JavaScript variable or a template without escaping: the Base64 alphabet contains no quotes and no backslashes. That is one reason the format survives in configs and APIs.
Doing it in code instead
If the GIFs keep coming, encode them where your code already is. The same three lines exist in every stack:
const bytes = await (await fetch('spinner.gif')).arrayBuffer(); let bin = ''; new Uint8Array(bytes).forEach(b => { bin += String.fromCharCode(b); }); const dataUri = 'data:image/gif;base64,' + btoa(bin);
import base64 data = open('spinner.gif', 'rb').read() data_uri = 'data:image/gif;base64,' + base64.b64encode(data).decode()
$dataUri = 'data:image/gif;base64,' . base64_encode(file_get_contents('spinner.gif'));
One warning that applies to the JavaScript version: building the binary string byte by byte is fine for small GIFs, but for a file of several MB, convert it in chunks, as this page does, or use a library that does. The encoding is the same either way; only the memory manners differ.
Common mistakes
- Pasting the raw string without the prefix. A browser cannot tell raw Base64 is an image. In an img tag or a CSS rule you need the full data URI, prefix and all; the raw string is only for places that add the prefix themselves.
- Inlining everything. One small spinner is a good trade; every image on the page inlined is a page that can cache nothing and parses slowly. Inline the few, host the rest.
- Encoding a multi-megabyte GIF. The string will be a third bigger again and the host file becomes unusable. Compress the GIF first, then encode what is left, or keep it a file.
- Trusting the file name. A PNG named .gif encodes fine but needs the image/png prefix to render. This tool reads the bytes and uses the right prefix; a hand-rolled snippet that hardcodes image/gif does not.
- Losing characters in transit. A long string pasted through a chat window, a form or a spreadsheet cell can be silently cut or wrapped. For anything long, use the text-file download and move the file, not the paste.
A worked example, with real numbers
These figures come from encoding this page's built-in sample file through this tool. The sample is a fixed GIF file, the bouncing-ball scene the MotionGIF tools share (320 x 200 px, 12 frames, looping forever), so its bytes are the same in every browser and the same numbers come out every time.
- The file: 15,357 bytes (15.0 KB).
- The raw Base64 string: 20,476 characters, which is exactly 4 x ceil(15,357 / 3), as the formula requires.
- The data URI: 20,498 characters, the string plus its 22-character prefix.
- The overhead: 33.3% more characters than the file has bytes, the third-again cost every Base64 string carries.
- The verdict: at that size the sample lands in this page's smallest band, small enough to inline, which is the honest shape of this tool's work: the GIFs worth inlining are the small ones.
Every file you load reports its own real numbers the same way, so your GIF's verdict is never a guess from this example: the readout states its bytes, its string lengths and its overhead before you copy anything. If yours comes back too heavy, the GIF Compressor can take the GIF down first, and the smaller file will encode to a shorter string.
Questions people ask about turning a GIF into Base64
Do animated GIFs still animate after Base64 conversion?
Yes. Base64 does not change the GIF, it re-writes the same bytes as text: every 3 bytes of the file become 4 characters. When a browser decodes the string it gets your file back byte for byte, with every frame, every frame delay and the loop count intact, so the animation plays exactly as it did. The preview on this page is playing from the data URI itself, which is the proof: if the string were damaged, the animation would be too.
Is there a file size limit for Base64 encoding?
The encoding itself has no size limit: any file can be turned into a string, and the string is always exactly 4 x ceil(bytes / 3) characters. This tool accepts files up to 10 MB, because past that the string runs past 13 million characters and is far too heavy to inline anywhere. The practical limit is much lower: this page calls a GIF small enough to inline up to 50 KB, worth care up to 300 KB, and too heavy to inline above that.
How do I use the Base64 string in HTML?
Paste the data URI form where you would normally put the file's address, as the src of an img tag. The browser reads the image straight out of the attribute, so no separate file is requested. The tool above hands you the finished img tag with your GIF already inside it, ready to paste. Keep the whole prefix: an img tag whose src is the raw string without data:image/gif;base64, in front of it will not render.
Can I use Base64 GIFs in CSS?
Yes. A Base64 GIF goes in a CSS rule as the address inside background-image: url(...), with the full data URI between the brackets, and the tool above hands you that rule ready to paste. Remember the string then lives inside your CSS file, so the CSS grows by the string's full length, and the image is cached only as part of that file, never on its own. That trade is worth it for a small spinner or icon, and rarely for anything bigger.
How do I turn a GIF into Base64?
Drop the GIF on the box at the top of this page, or pick it, or paste it. The conversion happens in your browser the moment the file lands: you get the raw Base64 string, the full data URI, and the same string already wrapped as an HTML img tag, a CSS rule and a JSON value, each with a copy button, plus a download of the data URI as a text file. The readout above the strings tells you the file's size, the string's length and whether inlining this particular GIF is a good idea.
Are my GIF files uploaded anywhere during conversion?
No. The file is read and encoded inside your browser tab, by your own device, and nothing is sent to a server at any point. That is also why there is no account, no queue and no watermark, and why the tool works offline once the page has loaded.
Related tools
Encoding a GIF is usually the last step of a longer job. These MotionGIF tools cover the steps that tend to come before it:
GIF Compressor
Shrink a GIF before you encode it: a smaller file makes a shorter string, and the verdict on this page will often send you here first.
Open the tool →GIF Frame Viewer
Step through a GIF frame by frame and inspect the timing and details of every frame, without changing the file.
Open the tool →PNG to GIF
Turn one image into a still GIF, or a sequence of separate image files into an animated GIF, when the thing you need to encode does not exist as a GIF yet.
Open the tool →