Table of Contents
Data URI Popular Science
A preliminary study on Data URI
Data URI Compatibility
Data URI best practice
DIY generation tool
Home Web Front-end HTML Tutorial data uri_html/css_WEB-ITnose

data uri_html/css_WEB-ITnose

Jun 24, 2016 am 11:51 AM

The author's positioning of this article is practical, so I only touch on some theoretical foundations. First, what is a Data URI? Quoting the explanation on Wikipedia:

Data URI 是一种提供让外置资源的直接内嵌在页面中的方案。这种技术允许我们只需单次 HTTP 请求即可获取所有需要引用的图片与样式资源,并因无需多次请求资源而变的高效。
Copy after login

Its format specification is defined in RFC2397 (http://tools.ietf.org/html/rfc2397):

data:[<mime type>][;charset=<charset>][;base64],<encoded data>
Copy after login

A preliminary study on Data URI

It seems that the format specification is not very friendly. Let’s take inline images as an example:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
Copy after login

data:image/png;base64, are fixed Format, image/png is the MIME type of the image, and base64 is the data encoding method. It is also necessary to point out here that the Base64 encoded data will be about 4/3 larger than the original data, which is related to the Base64 encoding algorithm. If used in email, according to RFC 822, a carriage return and line feed must be added for every 76 characters. At this time, the encoded data length is approximately 135.1% of the original length.

To verify the theory, we did the following tests, Base64 encoding images of different sizes, which was consistent with our expectations and increased by 1/3 compared to the original data:

图片尺寸 | 原始大小 | Base64大小 | 增长率 |:-----------|:------------|:-------------|:-------------|16*16 | 618 | 824 | 34.2%24*24 | 1,063 | 1,420 | 33.6%32*32 | 1,615 | 2,156 | 33.5%42*42 | 2,510 | 3,348 | 33.4%48*48 | 2,892 | 3,856 | 33.3%96*96 | 8,217 | 10,956 | 33.3%350*350 | 49,899 | 66,532 | 33.3%
Copy after login

Images are the most common scenario for using Data URI, but Data URI is a specification independent of resource type. You can also use Data URI to embed other resources:

var cvs = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv);var html = 'data:text/html;charset=utf-8,' + encodeURIComponent(html);
Copy after login

Data URI Compatibility

On the front end, browser compatibility is a topic that almost every technology cannot escape. Check the compatibility of Data URI:

  • Firefox 2
  • Opera 7.2
  • Chrome
  • Safari
  • Internet Explorer 8
  • Most mainstream browsers have already supported Data URI. Although IE8 supports it, its maximum length cannot exceed 32K data. This has been lifted under IE9 For detailed IE documentation, please read Microsoft's official documentation. The rest is still facing IE 6/7, which still has Class A views in China. We can use MHTML similar to Data URI (the author thinks that its design is better than Data URI, taking into account the reuse of embedded data ). A detailed introduction to MHTML is beyond the scope of this article, but it should be pointed out that Microsoft released a patch in 2011 for vulnerabilities in MHTML that may allow information leakage, which will cause the problem that MHTML cannot be referenced, so using MHTML in IE This solution involves great risks and is not recommended when expanding your knowledge.

  • http://technet.microsoft.com/zh-CN/security/advisory/2501696
  • http://www.microsoft.com/china/security/bulletins/ms03-014 .mspx
  • Data URI best practice

    We do not merge images before Data URI conversion, but use small images directly, which saves the trouble of combined image positioning,

    background-image:url("data:image/png;base64,iVBORw0KGgoAAA...ElFTkSuQmCC");*background-image:url(http://cdn.example.com/foo.gif);
    Copy after login

    Comparison data:

    图片尺寸 | 原始大小 | Base64大小 | Gzip大小 |  Gzip压缩率 | 增长率 |:-----------|:------------|:-------------|:-------------|:-------------|:-------------|16*16 | 618 | 824 | 668 | 81.1% | 108.8%24*24 | 1,063 | 1,420 | 1,119 | 78.8% | 5.3%32*32 | 1,615 | 2,156 | 1,670 | 77.5% | 3.9%42*42 | 2,510 | 3,348 | 2,568 | 76.7% | 2.3%48*48 | 2,892 | 3,856 |  2940 | 76.2% | 1.7%96*96 | 8,217 | 10,956 | 8304 | 75.8% | 1.1%350*350 | 49,899 | 66,532 | 50,095 | 75.3% | 0.4%
    Copy after login

    DIY generation tool

    Node is a veritable assistant for front-end development. It only uses 3 lines of code to convert an image into Base64. Encoding:

    var body = fs.readFileSync('./foo.png', 'binary');  // 输入var image = new Buffer(body, 'binary').toString('base64'); // base64编码var base64 = 'data:image/png;base64,' + image;  // 输出
    Copy after login

    Datauri format data requires more CPU calculations to render the image. Perhaps on a PC with weaker performance, the additional image request solution may render the image earlier, especially on mobile terminals where limited In the case of CPU, it must be used with caution.

    The compromise here is to limit the conversion conditions. Only small images smaller than 2K will be converted into Base64 encoding. 2K is the recommended threshold

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌
    Will R.E.P.O. Have Crossplay?
    1 months ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

    The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

    What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

    The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

    What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

    The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

    What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

    The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

    What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

    The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

    Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

    HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

    The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

    HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

    What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

    AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

    See all articles