• Stars
    star
    147
  • Rank 251,347 (Top 5 %)
  • Language
    CSS
  • License
    Creative Commons ...
  • Created almost 7 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A PostCSS plugin to emulate flex gap using margins

Flex Gap Polyfill

NPM Version Linux Build Status Gitter Chat

This is a pure CSS polyfill using PostCSS to emulate flex gap using margins.

Known limitations

  • Must use a wrapper div when using margin: auto or background.
  • Percentage gaps aren't reliable if the container is not full width of parent container
  • Width of flex items with percentages vary slightly from spec because of negative margin on container.

View the demo page for various test cases of the polyfill in action.

Example

.container {
    display: flex;
    gap: 40px;
}

Becomes:

/* Output simplified for purpose of example */

.container > * {
    --fgp-parent-gap-row: 40px;
    --fgp-parent-gap-column: 40px;
    --fgp-margin-top: calc(var(--fgp-gap-row) + var(--orig-margin-top, 0px));
    --fgp-margin-left: calc(var(--fgp-gap-column) + var(--orig-margin-left, 0px));
    margin-top: var(--fgp-margin-top);
    margin-left: var(--fgp-margin-left);
}

.container {
    --fgp-gap: var(--has-fgp, 40px);
    --fgp-gap-row: 40px;
    --fgp-gap-column: 40px;
    --fgp-margin-top: calc(var(--fgp-parent-gap-row, 0px) - var(--fgp-gap-row) + var(--orig-margin-top, 0px));
    --fgp-margin-left: calc(var(--fgp-parent-gap-column, 0px) - var(--fgp-gap-column) + var(--orig-margin-left, 0px));
    display: flex;
    gap: var(--fgp-gap, 0px);
    margin-top: var(--fgp-margin-top, var(--orig-margin-top));
    margin-left: var(--fgp-margin-left, var(--orig-margin-left));
}

The polyfill emulates flex gap by adding margins to each child element and applying a negative margin to the container.

  • NEW Now works regardless of whether display: flex and gap are used in the same rule (see Options for ways to optimise)
  • Works with unlimited nested elements with any combination of units, px > px, px > %, % > %, etc
  • No additional class names or divs needed (except when using margin: auto or background)
  • Works even when margin already exists on element (inline styles not supported)
  • Style margin, borders and padding as normal
  • Supports gap, row-gap and column-gap

Browser support

Supports all current modern browsers, Edge, Firefox, Chrome, Safari, Opera (any browser that supports calc() and var()).

Usage

Add Flex Gap Polyfill to your project:

npm install flex-gap-polyfill postcss --save-dev

Use Flex Gap Polyfill to process your CSS:

const flexGapPolyfill = require('flex-gap-polyfill');

flexGapPolyfill.process(YOUR_CSS /*, processOptions, pluginOptions */);

Or use it as a PostCSS plugin:

const postcss = require('postcss');
const flexGapPolyfill = require('flex-gap-polyfill');

postcss([
    flexGapPolyfill(/* pluginOptions */)
]).process(YOUR_CSS /*, processOptions */);

Options

  • only

    Type: Array Default: undefined

    When true polyfill will only apply when display: flex | inline-flex and gap are used in the same rule. Provide an array of selectors to match additional rules. Can be strings or regexes. Inlcude /* apply fgp */ within a rule to manually apply the polyfill.

  • flexGapNotSupported

    Type: String Default: false

    Manually specify a selector to use when flex gap is not supported by detection via JavaScript, eg flexGapNotSupported: '.flex-gap-not-supported'.

  • webComponents

    Type: Boolean Default: false

    When true polyfill will also target slotted elements

More Repositories

1

plugma

CLI that simplifies creating Figma plugins
TypeScript
42
star
2

figma-node-decoder

Converts any Figma layer/node to Plugin API compatible code.
HTML
40
star
3

mole

A tool for managing design decision outputs for different platforms
JavaScript
33
star
4

gridlover-mixin

A mixin to generate modular scale and vertical rhythm for your typography
Less
30
star
5

postcss-help-media-queries

This tool help you to easily recognize your active media query and display additional screen info
CSS
26
star
6

postcss-custom-values

Create custom CSS values such as keywords and units
JavaScript
20
star
7

stylup-deprecated

A preprocessor that allows you to write class names in an easier manner when using utility classes for responsive designs
JavaScript
20
star
8

postcss-magic-token

References design tokens in CSS intelligently based on the current property and selector rules
JavaScript
17
star
9

figma-table-creator

A simple Figma plugin for creating flexible and editable tables.
Svelte
9
star
10

typolize

Normalizes type sizing and spacing.
CSS
9
star
11

stancy

A simple API for delivering content from static files and folders.
JavaScript
8
star
12

thumbquote-config-2022

JavaScript
7
star
13

figma-widget-table

Create an import table data in FigJam files
CSS
7
star
14

figma-layer-styles

Svelte
5
star
15

postcss-sqrt

Calculate the square root of a number dynamically in CSS
JavaScript
5
star
16

postcss-negative-padding

Use negative values with padding to increase the hit area of a target
JavaScript
4
star
17

twig-perch-extension

A Twig extension that allows you to integrate Perch CMS into your templates.
PHP
4
star
18

rollup-plugin-theo

A rollup plugin which transforms design tokens using Theo
JavaScript
4
star
19

stylup

Write utility classes conveniently while optimising your CSS
JavaScript
3
star
20

postcss-border-align

A PostCSS plugin which lets you create borders which do not affect the layout of the document
JavaScript
3
star
21

figma-puma

A simple plugin that links your slides together in the shape of a PUMA
JavaScript
3
star
22

postcss-pow

Calculate the power of a number dynamically in CSS
JavaScript
3
star
23

postcss-fiber

Modular and composable library of CSS properties to manage scale, proportion, layout, typography, colour and animation for any device.
JavaScript
2
star
24

personal-site

Marko
2
star
25

postcss-sequence

Manage consistent scale and proportion of your design using custom units based on numerical sequences.
JavaScript
2
star
26

plugma-deprecated

A small framework and CLI for developing and maintaining Figma plugins
TypeScript
2
star
27

figma-vite-svelte-codemirror-example

Svelte
1
star
28

writing-a-better-readme

Guidelines for writing better readmes and documenting code
1
star
29

postcss-exact-width

Gives you the exact width and height desired when applied to flex items
JavaScript
1
star
30

postcss-proportional-spacing

Apply proportional horizontal and vertical spacing with ratios using a unitless quantity
JavaScript
1
star
31

plugma-tsserver

Svelte
1
star
32

figma-icon-preview

A Figma plugin to preview icons at different sizes for web, iOS and Android.
HTML
1
star