| 1 | # CSS Blank Pseudo [<img src="http://jonathantneal.github.io/js-logo.svg" alt="" width="90" height="90" align="right">][CSS Blank Pseudo]
|
|---|
| 2 |
|
|---|
| 3 | [![NPM Version][npm-img]][npm-url]
|
|---|
| 4 | [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
|
|---|
| 5 |
|
|---|
| 6 | [CSS Blank Pseudo] lets you style form elements when they are empty, following
|
|---|
| 7 | the [Selectors Level 4] specification.
|
|---|
| 8 |
|
|---|
| 9 | ```css
|
|---|
| 10 | input {
|
|---|
| 11 | /* style an input */
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | input:blank {
|
|---|
| 15 | /* style an input without a value */
|
|---|
| 16 | }
|
|---|
| 17 | ```
|
|---|
| 18 |
|
|---|
| 19 | ## Usage
|
|---|
| 20 |
|
|---|
| 21 | From the command line, transform CSS files that use `:blank` selectors:
|
|---|
| 22 |
|
|---|
| 23 | ```bash
|
|---|
| 24 | npx css-blank-pseudo SOURCE.css --output TRANSFORMED.css
|
|---|
| 25 | ```
|
|---|
| 26 |
|
|---|
| 27 | Next, use your transformed CSS with this script:
|
|---|
| 28 |
|
|---|
| 29 | ```html
|
|---|
| 30 | <link rel="stylesheet" href="TRANSFORMED.css">
|
|---|
| 31 | <script src="https://unpkg.com/css-blank-pseudo/dist/browser-global.js"></script>
|
|---|
| 32 | <script>cssBlankPseudo(document)</script>
|
|---|
| 33 | ```
|
|---|
| 34 |
|
|---|
| 35 | ⚠️ Please use a versioned url, like this : `https://unpkg.com/css-blank-pseudo@3.0.0/dist/browser-global.js`
|
|---|
| 36 | Without the version, you might unexpectedly get a new major version of the library with breaking changes.
|
|---|
| 37 |
|
|---|
| 38 | ⚠️ If you were using an older version via a CDN, please update the entire url.
|
|---|
| 39 | The old URL will no longer work in a future release.
|
|---|
| 40 |
|
|---|
| 41 | That’s it. The script works in most browsers.
|
|---|
| 42 |
|
|---|
| 43 | ## How it works
|
|---|
| 44 |
|
|---|
| 45 | The [PostCSS plugin](README-POSTCSS.md) clones rules containing `:blank`,
|
|---|
| 46 | replacing them with an alternative `[blank]` selector.
|
|---|
| 47 |
|
|---|
| 48 | ```css
|
|---|
| 49 | input:blank {
|
|---|
| 50 | background-color: yellow;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /* becomes */
|
|---|
| 54 |
|
|---|
| 55 | input[blank] {
|
|---|
| 56 | background-color: yellow;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | input:blank {
|
|---|
| 60 | background-color: yellow;
|
|---|
| 61 | }
|
|---|
| 62 | ```
|
|---|
| 63 |
|
|---|
| 64 | Next, the [JavaScript library](README-BROWSER.md) adds a `blank` attribute to
|
|---|
| 65 | elements otherwise matching `:blank` natively.
|
|---|
| 66 |
|
|---|
| 67 | ```html
|
|---|
| 68 | <input value="" blank>
|
|---|
| 69 | <input value="This element has a value">
|
|---|
| 70 | ```
|
|---|
| 71 |
|
|---|
| 72 | ## ⚠️ `:not(:blank)`
|
|---|
| 73 |
|
|---|
| 74 | with option : `preserve` `true`
|
|---|
| 75 |
|
|---|
| 76 | ```css
|
|---|
| 77 | input:not(:blank) {
|
|---|
| 78 | background-color: yellow;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /* becomes */
|
|---|
| 82 |
|
|---|
| 83 | input:not([blank]) {
|
|---|
| 84 | background-color: yellow;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | input:not(:blank) {
|
|---|
| 88 | background-color: yellow;
|
|---|
| 89 | }
|
|---|
| 90 | ```
|
|---|
| 91 |
|
|---|
| 92 | When you do not include the JS polyfill one will always match in browsers that support `:blank` natively.
|
|---|
| 93 | In browsers that do not support `:blank` natively the rule will be invalid.
|
|---|
| 94 |
|
|---|
| 95 | _currently no browsers support `:blank`_
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | [discord]: https://discord.gg/bUadyRwkJS
|
|---|
| 99 | [npm-img]: https://img.shields.io/npm/v/css-blank-pseudo.svg
|
|---|
| 100 | [npm-url]: https://www.npmjs.com/package/css-blank-pseudo
|
|---|
| 101 |
|
|---|
| 102 | [CSS Blank Pseudo]: https://github.com/csstools/postcss-plugins/tree/main/plugins/css-blank-pseudo
|
|---|
| 103 | [PostCSS Preset Env]: https://preset-env.cssdb.org/
|
|---|
| 104 | [Selectors Level 4]: https://drafts.csswg.org/selectors-4/#blank
|
|---|