

GitHub - ai/webp-in-css: PostCSS plugin and tiny JS script (128 bytes) to use We...
source link: https://github.com/ai/webp-in-css
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

README.md
WebP in CSS
PostCSS plugin and tiny JS script (128 bytes) to use WebP in CSS background
.
It will make your images 25% smaller for Chrome, Firefox, and Edge. Safari will download the bigger JPEG/PNG image.
You add require('webp-in-css')
to your JS bundle and write CSS like:
.logo { width: 30px; height: 30px; background: url(/logo.png); }
The script will set webp
or no-webp
class on <body>
and PostCSS plugin will generates:
.logo { width: 30px; height: 30px; } body.webp .logo { background: url(/logo.webp); } body.no-webp .logo { background: url(/logo.png); }
Usage
Step 1: convert all your JPEG/PNG images to WebP by Squoosh.
Set checkbox on Lossless
for PNG images and remove it for JPEG.
We recommend Reduce palette
for most of the PNG images.
Save WebP images in the same places of JPEG/PNG images:
img/bg.png
→ img/bg.webp
.
Step 2: use <picture>
to insert WebP images in HTML:
- <img src="/screenshot.jpg" alt="Screenshot"> + <picture> + <source src="/screenshot.webp" type="image/webp"> + <img src="/screenshot.jpg" alt="Screenshot"> + </picture>
Step 3: install webp-in-css
. For npm use:
npm install --save-dev webp-in-css
For Yarn:
yarn add --dev webp-in-css
Step 4: add JS script to your client-side JS bundle:
+ require('webp-in-css')
Since JS script is very small (128 bytes), the best way for landings is to inline it to HTML:
+ <script><%= readFile('node_modules/webp-in-css/index.js') %></script>
</head>
Step 5: check do you use PostCSS already in your bundler.
You can check postcss.config.js
in the project root,
"postcss"
section in package.json
or postcss
in bundle config.
If you don’t have it already, add PostCSS to your bundle:
- For webpack see postcss-loader docs.
- For Parcel create
postcss.config.js
file. It already has PostCSS support. - For Gulp check gulp-postcss docs.
Step 5: Add webp-in-css/plugin
to PostCSS plugins:
module.exports = {
plugins: [
+ require('webp-in-css/plugin'),
require('autoprefixer')
]
}
We also recommend to put all images from CSS to preload content:
+ <link rel="preload" as="image" type="image/webp" href="/logo.webp">
<script><%= readFile('node_modules/webp-in-css/index.js') %></script>
</head>
Recommend
-
128
现在的前端,javascript的发展有目共睹,框架林立。同时,html也是齐头并进,推出了HTML5标准,并且得到了普及。这样的发展却唯独少了一个角色? CSS,就是这个看似不起眼的家伙,却在开发中发挥着和js一样重要的作用。css,是一...
-
29
css-properly PostCSS plugin for people who prefer to write English properly. .foo { colour: black; background-colour: lightgrey; text-align: ce...
-
13
Plugin migration guide — Martian Chronicles, Evil Martians’ team blogPostCSS received a major update with the release of version 8.0 codenamed “President Ose”. Plugin creators can now opt in for a new API that сan increase build speeds and re...
-
13
本文基于 SDWebImage 5.5.2。重读的原因也是由于发现它的 API 在不断迭代,许多结构已经不同与早期版本,同时也是为了做一个记录。整体分析可以查看上一篇文章:浅析 SDWebImage 5.5.2。
-
13
PostCSS Custom CSS Units Define custom css unit and convert them to CSS variable. Input: :root { --cusomt-base-unit: 1vw; } div { width: 100rpx; } Output: div { width: c...
-
10
Grayscale Bytes – A Tiny Generative Code Study Building Universes One Pixel at a Time
-
9
A Gulp build script for Express, PostCSS and JavaScript with BrowserSync 17.06.2020 0 comments 629 days si...
-
26
Forked repository This is a fork from luciad/webp-imageio Changes Native libs are bundled in the jar Published to Maven Central (
-
6
PostCSS Plugins We are happy you're here! This repository uses a monorepo architecture that contains plugins, plugin-packs and CLI tools to help you transform your CSS so every browser can understand it. Our wish is that yo...
-
6
Quick script to convert JPEG/PNG images to WebP and AVIFPublished: 2024.01.21 · 2 minutes readBrowser support for modern image formats like WebP and AVIF is excellent, so you should use them to take advantage of t...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK