Code Monkey home page Code Monkey logo

cqfill's Introduction

CQFill

CQFill is a polyfill for CSS Container Queries.

npm install cqfill # yarn add cqfill

Demos

Component Query Card Demo
Article - QC
Balloon G-OPAW

Usage

Add the CQFill polyfill to your page:

<script src="https://unpkg.com/cqfill"></script>

Or, add the CQFill script to your NodeJS project:

import 'cqfill'

Next, add the included PostCSS plugin to your .postcssrc.json file:

{
  "plugins": [
    "cqfill/postcss"
  ]
}

Now, go forth and use CSS container queries:

.container {
  contain: layout inline-size;
}

@container (min-width: 700px) {
  .contained {
    /* styles applied when a container is at least 700px */
  }
}

Tips

You can use PostCSS Nesting to nest @container rules:

{
  "plugins": [
    "postcss-nesting",
    "cqfill/postcss"
  ]
}

You can activate the polyfill manually:

<script src="https://unpkg.com/cqfill/export"></script>

<script>cqfill() /* cqfill(document); cqfill(shadowRoot) */</script>
import { cqfill } from 'cqfill'

cqfill() /* cqfill(document); cqfill(shadowRoot) */

Usage with PostCSS

Use the included PostCSS plugin to process your CSS:

import postcss from 'postcss'
import postcssCQFill from 'cqfill/postcss'

postcss([ postcssCQFill ])

To transform CSS with PostCSS and without any other tooling:

import fs from 'fs'
import postcss from 'postcss'
import postcssCQFill from 'cqfill/postcss'

const from = './test/readme.css'
const fromCss = fs.readFileSync(from, 'utf8')

const to = './test/readme.polyfilled.css'

postcss([ postcssCQFill ]).process(fromCss, { from, to }).then(
  ({ css }) => fs.writeFileSync(to, css)
)

Usage without PostCSS

Add a fallback property to support the CSS contain property.

/* before */
.container {
  contain: layout inline-size;
}

/* after */
.container {
  --css-contain: layout inline-size;
  contain: layout inline-size;
}

Duplicate container queries using a fallback rule.

/* before */
@container (min-width: 700px) {
  .contained {
    /* styles applied when a container is at least 700px */
  }
}

/* after */
@media --css-container and (min-width: 700px) {
  .contained {
    /* styles applied when a container is at least 700px */
  }
}

@container (min-width: 700px) {
  .contained {
    /* styles applied when a container is at least 700px */
  }
}

cqfill's People

Contributors

jonathantneal avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.