{"id":5567,"date":"2020-11-16T18:17:17","date_gmt":"2020-11-16T18:17:17","guid":{"rendered":"https:\/\/shortpixel.com/blog\/?p=5567"},"modified":"2021-04-15T03:31:18","modified_gmt":"2021-04-15T03:31:18","slug":"how-to-get-adaptive-images-on-non-wordpress-websites","status":"publish","type":"post","link":"https:\/\/shortpixel.com\/blog\/how-to-get-adaptive-images-on-non-wordpress-websites\/","title":{"rendered":"How to get adaptive images on non-WordPress websites"},"content":{"rendered":"\n<p>If you are trying to improve your site speed, you don&#8217;t need to go too far to hear or see that you need to use <strong>adaptive images<\/strong>, or <strong>scaled images<\/strong>. What is this?<\/p>\n\n\n\n<p>Adaptive images is what you need to prevent delivering huge images to your clients with small screens. Say you upload a 3000&#215;2000 image, would you want to deliver that huge and heavy photo to a visitor with an iPhone? It doesn&#8217;t make sense. You are just going to make their user experience worse, by slowing down the page loading. That&#8217;s why you need to &#8220;adapt&#8221; or &#8220;scale&#8221; the image according to the device.<\/p>\n\n\n\n<p>We are sure you&#8217;ve heard of <a rel=\"noreferrer noopener\" aria-label=\"ShortPixel Adaptive Images (opens in a new tab)\" href=\"https:\/\/wordpress.org\/plugins\/shortpixel-adaptive-images\/\" target=\"_blank\"><strong>ShortPixel Adaptive Images<\/strong><\/a>: The <strong>best <\/strong>and only plugin you need to serve adaptive images, from our fast CDN and optimized with <a href=\"https:\/\/shortpixel.com\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"ShortPixel (opens in a new tab)\">ShortPixel<\/a>&#8216;s technology.<\/p>\n\n\n\n<p>But what if you don&#8217;t have a non-WordPress website?<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Adaptive Images on any website<\/h2>\n\n\n\n<p>Introducing the <strong>Adaptive Images Standalone JavaScript<\/strong>. With a few lines of code and the power of <a rel=\"noreferrer noopener\" aria-label=\"ShortPixel (opens in a new tab)\" href=\"https:\/\/shortpixel.com\" target=\"_blank\">ShortPixel<\/a> you&#8217;ll be able to serve scaled images to <strong>anybody<\/strong>.<\/p>\n\n\n\n<p>Let&#8217;s see step by step what you need to do in order to serve scaled images, if you don&#8217;t have a WordPress website.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1 &#8211; Add JavaScript code<\/h3>\n\n\n\n<p>You first need to <strong>add the following code snippet in the head of your page<\/strong>, anywhere between the tags <code>&lt;head&gt; ... &lt;\/head&gt;<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script>\n    (function(w, d){\n        var b = d.getElementsByTagName('head')[0];\n        var s = d.createElement(\"script\");\n        var v = (\"IntersectionObserver\" in w) ? \"\" : \"-compat\";\n        s.async = true; \/\/ This includes the script as async.\n        s.src = \"https:\/\/cdn.shortpixel.ai\/assets\/js\/bundles\/spai-lib-bg-webp\" + v + \".0.9.min.js\";\n        w.spaiData = {\n            key: \"jsai\",\n            quality: \"lossy\",\n            sizeFromImageSuffix: true\n        };\n        b.appendChild(s);\n    }(window, document));\n&lt;\/script><\/code><\/pre>\n\n\n\n<p><br>For example:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"600\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2020\/11\/Screenshot-20201112-122620-1024x600.jpg\" alt=\"\" class=\"wp-image-5573\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/11\/Screenshot-20201112-122620-1024x600.jpg 1024w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/11\/Screenshot-20201112-122620-300x176.jpg 300w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/11\/Screenshot-20201112-122620-768x450.jpg 768w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/11\/Screenshot-20201112-122620.jpg 1187w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">2 &#8211; Replace SRC attributes<\/h3>\n\n\n\n<p>Now you&#8217;ll have to <strong>replace all the <\/strong><code><strong>src<\/strong><\/code><strong>&nbsp;attributes<\/strong> of your images HTML markup <strong>with&nbsp;<\/strong><code><strong>data-spai-src<\/strong><\/code>. For example, if you had this URL:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;img class=\"aligncenter\" src=\"https:\/\/shortpixel.com\/img\/robot_lookleft_wink.png\" alt=\"robo winks\" width=\"600\" height=\"933\" \/><\/code><\/pre>\n\n\n\n<p><br>You&#8217;ll have to change it to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;img class=\"aligncenter\" data-spai-src=\"https:\/\/shortpixel.com\/img\/robot_lookleft_wink.png\" alt=\"robo winks\" width=\"600\" height=\"933\" \/><\/code><\/pre>\n\n\n\n<p><br>This step depends a lot on your website and the way it is built. It may require some replacements in the DataBase, or the result may be achieved through a code that replaces <code>src<\/code>&nbsp;with <code>data-spai-src<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3 &#8211; Create an account on ShortPixel<\/h3>\n\n\n\n<p>Just <a rel=\"noreferrer noopener\" aria-label=\"go to our website (opens in a new tab)\" href=\"https:\/\/shortpixel.com\" target=\"_blank\">go to our website<\/a> and click on <strong>SIGN UP<\/strong> on the top right. Once your account is set up, you can purchase a <a rel=\"noreferrer noopener\" aria-label=\"monthly subscription (opens in a new tab)\" href=\"https:\/\/help.shortpixel.com\/article\/26-how-do-monthly-subscriptions-work\" target=\"_blank\">monthly subscription<\/a> or <a rel=\"noreferrer noopener\" aria-label=\"one-time credits (opens in a new tab)\" href=\"https:\/\/help.shortpixel.com\/article\/22-how-does-the-one-time-plan-work\" target=\"_blank\">one-time credits<\/a> to optimize your images. For more information about how your credits will be spent, <a rel=\"noreferrer noopener\" aria-label=\"click here (opens in a new tab)\" href=\"https:\/\/help.shortpixel.com\/article\/96-how-are-the-credits-counted\" target=\"_blank\">click here<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">4 &#8211; Associate your domain<\/h3>\n\n\n\n<p>You&#8217;ll have to associate your domain in order to let ShortPixel know what account your domain should take credits from. Have a look <a rel=\"noreferrer noopener\" aria-label=\"at this article (opens in a new tab)\" href=\"https:\/\/help.shortpixel.com\/article\/94-how-to-associate-a-domain-to-my-account\" target=\"_blank\">at this article<\/a> for more information.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adaptive Background Images as well!<\/h2>\n\n\n\n<p>If you also have <strong>background images<\/strong> on your site, then you&#8217;ll have to tweak a bit the previous steps.<\/p>\n\n\n\n<p>To be clear, background images are those that are added in <code>background-image<\/code> CSS properties, not with regular <code>img<\/code> tags. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;div class=\"exampleclass\" style=\"background-image: url(https:\/\/shortpixel.com\/img\/robot_lookleft_wink.png)\"><\/code><\/pre>\n\n\n\n<p><br>There might be other similar CSS properties, you should proceed in a similar way for all of them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1 &#8211; Add CSS<\/h3>\n\n\n\n<p><strong>Add the following CSS<\/strong> in the head of your code, the same way you did previously with the JavaScript file; anywhere between the tags <code>&lt;head&gt; ... &lt;\/head&gt;<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;style>\nhtml.spai_has_js .my-bkg-class1:not(.spai-bg-lazyloaded),\nhtml.spai_has_js .my-bkg-class2:not(.spai-bg-lazyloaded) { background-image: none !important; }\n&lt;\/style><\/code><\/pre>\n\n\n\n<p><br>You will have to <strong>replace <\/strong><code><strong>my-bkg-class1<\/strong><\/code><strong>, <\/strong><code><strong>my-bkg-class2<\/strong><\/code><strong> and so on<\/strong>, with the classes of the elements having the background. For example, following the example above, you&#8217;ll have:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;style>\nhtml.spai_has_js .exampleclass:not(.spai-bg-lazyloaded) { background-image: none !important; }\n&lt;\/style><\/code><\/pre>\n\n\n\n<p><br>Notice that you can add as many classes as you want, <strong>separated by commas<\/strong>. For instance:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;style>\nhtml.spai_has_js .exampleclass:not(.spai-bg-lazyloaded),\nhtml.spai_has_js .whatever_class:not(.spai-bg-lazyloaded),\nhtml.spai_has_js .another_one:not(.spai-bg-lazyloaded) { background-image: none !important; }\n&lt;\/style><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2 &#8211; Tweak the JavaScript<\/h3>\n\n\n\n<p>Now you will have to <strong>change JavaScript code<\/strong> you added, to <strong>look like the following<\/strong> instead:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;script>\n    document.documentElement.className += \" spai_has_js\";\n    (function(w, d){\n        var b = d.getElementsByTagName('head')[0];\n        var s = d.createElement(\"script\");\n        var v = (\"IntersectionObserver\" in w) ? \"\" : \"-compat\";\n        s.async = true; \/\/ This includes the script as async.\n        s.src = \"https:\/\/cdn.shortpixel.ai\/assets\/js\/spai-lib-bg-webp\" + v + \".0.9.min.js\";\n        w.spaiData = {\n            key: \"cstm\",\n            quality: \"lossy\",\n            backgroundReplaceClasses: ['exampleclass','whatever_class', 'another_one'],\n            watchClasses: [],\n            backgroundLazySelectors: \".exampleclass, .whatever_class, .another_one\",\n            sizeFromImageSuffix: true\n        };\n        b.appendChild(s);\n    }(window, document));\n&lt;\/script><\/code><\/pre>\n\n\n\n<p><br><strong>Notice that we have added the CSS classes mentioned earlier<\/strong> on two different lines:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>...\nbackgroundReplaceClasses: ['exampleclass','whatever_class', 'another_one'],\n...\nbackgroundLazySelectors: \".exampleclass, whatever_class, another_one\",\n...<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3 &#8211; CSS files<\/h3>\n\n\n\n<p>If you have background images in .css files, then you will just need to prefix the URL of them like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;link rel=\"stylesheet\" href=\"https:\/\/cdn.shortpixel.ai\/cstm\/q_lossy+ret_img\/https:\/\/my.domain.com\/my\/style.css\"\/><\/code><\/pre>\n\n\n\n<p>The compression setting is done by the q_ parameter, which can have one of the following three values: <strong>q_lossy, q_glossy and q_lossless<\/strong>.<\/p>\n\n\n\n<p><strong>Notice that you need to use the full URL of the .css file<\/strong>, including the domain.<\/p>\n\n\n\n<p><br>And that&#8217;s all! Feel fry to try it out and <a rel=\"noreferrer noopener\" aria-label=\"reach out (opens in a new tab)\" href=\"https:\/\/shortpixel.com\/contact\" target=\"_blank\">reach out<\/a> if you have any questions.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are trying to improve your site speed, you don&#8217;t need to go too far to hear or see that you need to use adaptive images, or scaled images. What is this? Adaptive images is what you need to prevent delivering huge images to your clients with small screens. Say you upload a 3000&#215;2000 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":5595,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5567","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-world-of-wordpress"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5567","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/comments?post=5567"}],"version-history":[{"count":17,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5567\/revisions"}],"predecessor-version":[{"id":5768,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5567\/revisions\/5768"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/media\/5595"}],"wp:attachment":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/media?parent=5567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/categories?post=5567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/tags?post=5567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}