| 1 | # PostCSS Logical Properties and Values [<img src="https://postcss.github.io/postcss/logo.svg" alt="PostCSS Logo" width="90" height="90" align="right">][postcss]
|
|---|
| 2 |
|
|---|
| 3 | [![NPM Version][npm-img]][npm-url]
|
|---|
| 4 | [![CSS Standard Status][css-img]][css-url]
|
|---|
| 5 | [![Build Status][cli-img]][cli-url]
|
|---|
| 6 | [<img alt="Discord" src="https://shields.io/badge/Discord-5865F2?logo=discord&logoColor=white">][discord]
|
|---|
| 7 |
|
|---|
| 8 | [PostCSS Logical Properties and Values] lets you use logical, rather than
|
|---|
| 9 | physical, direction and dimension mappings in CSS, following the
|
|---|
| 10 | [CSS Logical Properties and Values] specification.
|
|---|
| 11 |
|
|---|
| 12 | [](https://caniuse.com/#feat=css-logical-props)
|
|---|
| 13 |
|
|---|
| 14 | ```pcss
|
|---|
| 15 | .banner {
|
|---|
| 16 | color: #222222;
|
|---|
| 17 | inset: logical 0 5px 10px;
|
|---|
| 18 | padding-inline: 20px 40px;
|
|---|
| 19 | resize: block;
|
|---|
| 20 | transition: color 200ms;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | /* becomes */
|
|---|
| 24 |
|
|---|
| 25 | .banner:dir(ltr) {
|
|---|
| 26 | padding-left: 20px;
|
|---|
| 27 | padding-right: 40px;
|
|---|
| 28 | }
|
|---|
| 29 | .banner:dir(rtl) {
|
|---|
| 30 | padding-right: 20px;
|
|---|
| 31 | padding-left: 40px;
|
|---|
| 32 | }
|
|---|
| 33 | .banner {
|
|---|
| 34 | color: #222222;
|
|---|
| 35 | top: 0;
|
|---|
| 36 | left: 5px;
|
|---|
| 37 | bottom: 10px;
|
|---|
| 38 | right: 5px;
|
|---|
| 39 | resize: vertical;
|
|---|
| 40 | transition: color 200ms;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | /* or, when used with { dir: 'ltr' } */
|
|---|
| 44 |
|
|---|
| 45 | .banner {
|
|---|
| 46 | color: #222222;
|
|---|
| 47 | top: 0;
|
|---|
| 48 | left: 5px;
|
|---|
| 49 | bottom: 10px;
|
|---|
| 50 | right: 5px;
|
|---|
| 51 | padding-left: 20px;
|
|---|
| 52 | padding-right: 40px;
|
|---|
| 53 | resize: vertical;
|
|---|
| 54 | transition: color 200ms;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /* or, when used with { preserve: true } */
|
|---|
| 58 |
|
|---|
| 59 | .banner:dir(ltr) {
|
|---|
| 60 | padding-left: 20px;
|
|---|
| 61 | padding-right: 40px;
|
|---|
| 62 | }
|
|---|
| 63 | .banner:dir(rtl) {
|
|---|
| 64 | padding-right: 20px;
|
|---|
| 65 | padding-left: 40px;
|
|---|
| 66 | }
|
|---|
| 67 | .banner {
|
|---|
| 68 | color: #222222;
|
|---|
| 69 | top: 0;
|
|---|
| 70 | left: 5px;
|
|---|
| 71 | bottom: 10px;
|
|---|
| 72 | right: 5px;
|
|---|
| 73 | inset: logical 0 5px 10px;
|
|---|
| 74 | padding-inline: 20px 40px;
|
|---|
| 75 | resize: vertical;
|
|---|
| 76 | resize: block;
|
|---|
| 77 | transition: color 200ms;
|
|---|
| 78 | }
|
|---|
| 79 | ```
|
|---|
| 80 |
|
|---|
| 81 | These shorthand properties set values for physical properties by default.
|
|---|
| 82 | Specifying the `logical` keyboard at the beginning of the property value will
|
|---|
| 83 | transform the flow-relative values afterward into both physical LTR and RTL
|
|---|
| 84 | properties:
|
|---|
| 85 |
|
|---|
| 86 | #### Logical Borders
|
|---|
| 87 |
|
|---|
| 88 | - `border`, `border-block`, `border-block-start`, `border-block-end`,
|
|---|
| 89 | `border-inline`, `border-inline-start`, `border-inline-end`, `border-start`,
|
|---|
| 90 | `border-end`, `border-color`, `border-block-color`,
|
|---|
| 91 | `border-block-start-color`, `border-block-end-color`, `border-inline-color`,
|
|---|
| 92 | `border-inline-start-color`, `border-inline-end-color`, `border-start-color`,
|
|---|
| 93 | `border-end-color`, `border-style`, `border-block-style`,
|
|---|
| 94 | `border-block-start-style`, `border-block-end-style`, `border-inline-style`,
|
|---|
| 95 | `border-inline-start-style`, `border-inline-end-style`, `border-start-style`,
|
|---|
| 96 | `border-end-style`, `border-width`, `border-block-width`,
|
|---|
| 97 | `border-block-start-width`, `border-block-end-width`, `border-inline-width`,
|
|---|
| 98 | `border-inline-start-width`, `border-inline-end-width`, `border-start-width`,
|
|---|
| 99 | `border-end-width`, `border-start-start-radius`, `border-start-end-radius`,
|
|---|
| 100 | `border-end-start-radius`, `border-end-end-radius`
|
|---|
| 101 |
|
|---|
| 102 | #### Logical Offsets
|
|---|
| 103 |
|
|---|
| 104 | - `inset`, `inset-block`, `inset-block-start`, `inset-block-end`,
|
|---|
| 105 | `inset-inline`, `inset-inline-start`, `inset-inline-end`, `inset-start`,
|
|---|
| 106 | `inset-end`
|
|---|
| 107 |
|
|---|
| 108 | #### Logical Margins
|
|---|
| 109 |
|
|---|
| 110 | - `margin`, `margin-block`, `margin-block-start`, `margin-block-end`,
|
|---|
| 111 | `margin-inline`, `margin-inline-start`, `margin-inline-end`, `margin-start`,
|
|---|
| 112 | `margin-end`
|
|---|
| 113 |
|
|---|
| 114 | #### Logical Paddings
|
|---|
| 115 |
|
|---|
| 116 | - `padding`, `padding-block`, `padding-block-start`, `padding-block-end`,
|
|---|
| 117 | `padding-inline`, `padding-inline-start`, `padding-inline-end`,
|
|---|
| 118 | `padding-start`, `padding-end`
|
|---|
| 119 |
|
|---|
| 120 | #### Logical Sizes
|
|---|
| 121 |
|
|---|
| 122 | - `block-size`, `max-block-size`, `min-block-size`, `inline-size`, `max-inline-size`, `min-inline-size`
|
|---|
| 123 |
|
|---|
| 124 | #### Flow-Relative Values
|
|---|
| 125 |
|
|---|
| 126 | - `clear: inline-start`, `clear: inline-end`, `float: inline-start`,
|
|---|
| 127 | `float: inline-end`, `text-align: start`, `text-align: end`
|
|---|
| 128 |
|
|---|
| 129 | ---
|
|---|
| 130 |
|
|---|
| 131 | By default, [PostCSS Logical Properties and Values] creates fallback selectors
|
|---|
| 132 | which require at least one `[dir]` attribute in your HTML. If you don’t have
|
|---|
| 133 | any `[dir]` attributes, consider using the following JavaScript:
|
|---|
| 134 |
|
|---|
| 135 | ```js
|
|---|
| 136 | // force at least one dir attribute (this can run at any time)
|
|---|
| 137 | document.documentElement.dir = document.documentElement.dir || 'ltr';
|
|---|
| 138 | ```
|
|---|
| 139 |
|
|---|
| 140 | Otherwise, consider using the `dir` option to transform all logical properties
|
|---|
| 141 | and values to a specific direction.
|
|---|
| 142 |
|
|---|
| 143 | ```js
|
|---|
| 144 | require('postcss-logical')({
|
|---|
| 145 | dir: 'ltr'
|
|---|
| 146 | });
|
|---|
| 147 | ```
|
|---|
| 148 |
|
|---|
| 149 | ## Usage
|
|---|
| 150 |
|
|---|
| 151 | Add [PostCSS Logical Properties and Values] to your project:
|
|---|
| 152 |
|
|---|
| 153 | ```bash
|
|---|
| 154 | npm install postcss-logical --save-dev
|
|---|
| 155 | ```
|
|---|
| 156 |
|
|---|
| 157 | Use [PostCSS Logical Properties and Values] as a [PostCSS] plugin:
|
|---|
| 158 |
|
|---|
| 159 | ```js
|
|---|
| 160 | const postcss = require('postcss');
|
|---|
| 161 | const postcssLogical = require('postcss-logical');
|
|---|
| 162 |
|
|---|
| 163 | postcss([
|
|---|
| 164 | postcssLogical(/* pluginOptions */)
|
|---|
| 165 | ]).process(YOUR_CSS /*, processOptions */);
|
|---|
| 166 | ```
|
|---|
| 167 |
|
|---|
| 168 | [PostCSS Logical Properties and Values] runs in all Node environments, with
|
|---|
| 169 | special instructions for:
|
|---|
| 170 |
|
|---|
| 171 | | [Node](INSTALL.md#node) | [PostCSS CLI](INSTALL.md#postcss-cli) | [Webpack](INSTALL.md#webpack) | [Create React App](INSTALL.md#create-react-app) | [Gulp](INSTALL.md#gulp) | [Grunt](INSTALL.md#grunt) |
|
|---|
| 172 | | --- | --- | --- | --- | --- | --- |
|
|---|
| 173 |
|
|---|
| 174 | ## Options
|
|---|
| 175 |
|
|---|
| 176 | ### dir
|
|---|
| 177 |
|
|---|
| 178 | The `dir` option determines how directional fallbacks should be added to CSS.
|
|---|
| 179 | By default, fallbacks replace the logical declaration with nested `:dir`
|
|---|
| 180 | pseudo-classes. If `dir` is defined as `ltr` or `rtl` then only the left or
|
|---|
| 181 | right directional fallbacks will replace the logical declarations. If
|
|---|
| 182 | `preserve` is defined as `true`, then the `dir` option will be ignored.
|
|---|
| 183 |
|
|---|
| 184 | ### preserve
|
|---|
| 185 |
|
|---|
| 186 | The `preserve` option determines whether directional fallbacks should be added
|
|---|
| 187 | before logical declarations without replacing them. By default, directional
|
|---|
| 188 | fallbacks replace logical declaration. If `preserve` is defined as `true`, then
|
|---|
| 189 | the `dir` option will be ignored.
|
|---|
| 190 |
|
|---|
| 191 | ## :dir() pseudo class
|
|---|
| 192 |
|
|---|
| 193 | This plugin transforms using the `:dir(ltr)` pseudo class.
|
|---|
| 194 | Use [postcss-dir-pseudo-class](https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-dir-pseudo-class#readme) to further transform to `[dir="ltr"]`.
|
|---|
| 195 |
|
|---|
| 196 | Currently `:dir(ltr)` doesn't have great browser support but this will improve over time.
|
|---|
| 197 |
|
|---|
| 198 | [css-img]: https://cssdb.org/images/badges/logical-properties-and-values.svg
|
|---|
| 199 | [css-url]: https://cssdb.org/#logical-properties-and-values
|
|---|
| 200 | [cli-img]: https://github.com/csstools/postcss-plugins/workflows/test/badge.svg
|
|---|
| 201 | [cli-url]: https://github.com/csstools/postcss-plugins/actions/workflows/test.yml?query=workflow/test
|
|---|
| 202 | [discord]: https://discord.gg/bUadyRwkJS
|
|---|
| 203 | [npm-img]: https://img.shields.io/npm/v/postcss-logical.svg
|
|---|
| 204 | [npm-url]: https://www.npmjs.com/package/postcss-logical
|
|---|
| 205 |
|
|---|
| 206 | [CSS Logical Properties and Values]: https://drafts.csswg.org/css-logical/
|
|---|
| 207 | [Gulp PostCSS]: https://github.com/postcss/gulp-postcss
|
|---|
| 208 | [Grunt PostCSS]: https://github.com/nDmitry/grunt-postcss
|
|---|
| 209 | [PostCSS]: https://github.com/postcss/postcss
|
|---|
| 210 | [PostCSS Loader]: https://github.com/postcss/postcss-loader
|
|---|
| 211 | [PostCSS Logical Properties and Values]: https://github.com/csstools/postcss-plugins/tree/main/plugins/postcss-logical
|
|---|