Code Monkey home page Code Monkey logo

tailwindcss-container-queries's Introduction

@tailwindcss/container-queries

A plugin for Tailwind CSS v3.2+ that provides utilities for container queries.

Installation

Install the plugin from npm:

npm install @tailwindcss/container-queries

Then add the plugin to your tailwind.config.js file:

// tailwind.config.js
module.exports = {
  theme: {
    // ...
  },
  plugins: [
    require('@tailwindcss/container-queries'),
    // ...
  ],
}

Usage

Start by marking an element as a container using the @container class, and then applying styles based on the size of that container using the container variants like @md:, @lg:, and @xl::

<div class="@container">
  <div class="@lg:underline">
    <!-- This text will be underlined when the container is larger than `32rem` -->
  </div>
</div>

By default we provide container sizes from @xs (20rem) to @7xl (80rem).

Named containers

You can optionally name containers using a @container/{name} class, and then include that name in the container variants using classes like @lg/{name}:underline:

<div class="@container/main">
  <!-- ... -->
  <div class="@lg/main:underline">
    <!-- This text will be underlined when the "main" container is larger than `32rem` -->
  </div>
</div>

Arbitrary container sizes

In addition to using one of the container sizes provided by default, you can also create one-off sizes using any arbitrary value:

<div class="@container">
  <div class="@[17.5rem]:underline">
    <!-- This text will be underlined when the container is larger than `17.5rem` -->
  </div>
</div>

Removing a container

To stop an element from acting as a container, use the @container-normal class.

With a prefix

If you have configured Tailwind to use a prefix, make sure to prefix both the @container class and any classes where you are using a container query modifier:

<div class="tw-@container">
  <!-- ... -->
  <div class="@lg:tw-underline">
    <!-- ... -->
  </div>
</div>

Configuration

By default we ship with the following configured values:

Name CSS
@xs @container (min-width: 20rem /* 320px */)
@sm @container (min-width: 24rem /* 384px */)
@md @container (min-width: 28rem /* 448px */)
@lg @container (min-width: 32rem /* 512px */)
@xl @container (min-width: 36rem /* 576px */)
@2xl @container (min-width: 42rem /* 672px */)
@3xl @container (min-width: 48rem /* 768px */)
@4xl @container (min-width: 56rem /* 896px */)
@5xl @container (min-width: 64rem /* 1024px */)
@6xl @container (min-width: 72rem /* 1152px */)
@7xl @container (min-width: 80rem /* 1280px */)

You can configure which values are available for this plugin under the containers key in your tailwind.config.js file:

// tailwind.config.js
module.exports = {
  theme: {
    extend: {
      containers: {
        '2xs': '16rem',
      },
    },
  },
}

tailwindcss-container-queries's People

Contributors

adamwathan avatar basarozcan avatar just-moh-it avatar reinink avatar robinmalfait avatar saibotk avatar sarimarton avatar smebberson avatar ti777777 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

tailwindcss-container-queries's Issues

The container-queries plugin does not work in a Ruby on rails application

What version of @tailwindcss/container-queries are you using?

v0.1.0

What version of Node.js are you using?

N/A

What browser are you using?

Chrome

What operating system are you using?

MacOs

Reproduction repository

N/A

Describe your issue

I am trying to include the container queries plugin in the tailwind.config.js file in a ruby on rails application.

  plugins: [
    require("@tailwindcss/forms"),
    require("@tailwindcss/aspect-ratio"),
    require("@tailwindcss/typography"),
    require("@tailwindcss/container-queries"),
  ],

When I run the command: bin/dev the following error messages appear:

18:28:42 css.1     | Error: Cannot find module '@tailwindcss/container-queries'
18:28:42 css.1     | Require stack:
18:28:42 css.1     | - /Users/astorayestas/work/eel-backend/config/tailwind.config.js
18:28:42 css.1     | - /snapshot/tailwindcss/lib/cli/build/plugin.js
18:28:42 css.1     | - /snapshot/tailwindcss/lib/cli/build/index.js
18:28:42 css.1     | - /snapshot/tailwindcss/lib/cli.js
18:28:42 css.1     | - /snapshot/tailwindcss/standalone-cli/standalone.js
18:28:42 css.1     | 1) If you want to compile the package/file into executable, please pay attention to compilation warnings and specify a literal in 'require' call. 2) If you don't want to compile the package/file into executable and want to 'require' it from filesystem (likely plugin), specify an absolute path in 'require' call using process.cwd() or process.execPath.
18:28:42 css.1     |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
18:28:42 css.1     |     at Function._resolveFilename (pkg/prelude/bootstrap.js:1955:46)
18:28:42 css.1     |     at Function.Module._load (node:internal/modules/cjs/loader:778:27)
18:28:42 css.1     |     at Module.require (node:internal/modules/cjs/loader:1005:19)
18:28:42 css.1     |     at Module.require (pkg/prelude/bootstrap.js:1855:31)
18:28:42 css.1     |     at Module.require (/snapshot/tailwindcss/standalone-cli/standalone.js:25:22)
18:28:42 css.1     |     at require (node:internal/modules/cjs/helpers:102:18)
18:28:42 css.1     |     at Object.<anonymous> (/Users/astorayestas/work/eel-backend/config/tailwind.config.js:77:5)
18:28:42 css.1     |     at Module._compile (node:internal/modules/cjs/loader:1105:14)
18:28:42 css.1     |     at Module._compile (pkg/prelude/bootstrap.js:1894:32) {
18:28:42 css.1     |   code: 'MODULE_NOT_FOUND',
18:28:42 css.1     |   requireStack: [
18:28:42 css.1     |     '/Users/astorayestas/work/eel-backend/config/tailwind.config.js',
18:28:42 css.1     |     '/snapshot/tailwindcss/lib/cli/build/plugin.js',
18:28:42 css.1     |     '/snapshot/tailwindcss/lib/cli/build/index.js',
18:28:42 css.1     |     '/snapshot/tailwindcss/lib/cli.js',
18:28:42 css.1     |     '/snapshot/tailwindcss/standalone-cli/standalone.js'
18:28:42 css.1     |   ],
18:28:42 css.1     |   pkg: true
18:28:42 css.1     | }

Ruby version: 3.1.2
Rails 7.0.4
The application was created with this instruction: rails new my-app --css tailwind.
Does the tailwindcss standalone-cli include the container query plugin?

Plugin doesn't appear to work when config includes "prefix" property

What version of @tailwindcss/container-queries are you using?

3.3.1

What version of Node.js are you using?

Tailwind Play

What browser are you using?

Chrome

What operating system are you using?

MacOS

Reproduction repository

https://play.tailwindcss.com/PL5t2J58Lw

Describe your issue

When using the prefix property in the config, the plugin doesn't appear to work. In this Play example, if you comment-out the prefix property, the first "test" word will be underlined properly.

https://play.tailwindcss.com/PL5t2J58Lw

I really like this plugin. Just hoping to get it working when a prefix is used.

Thanks!

Calc is not supported as an [arbitrary] container size

What version of @tailwindcss/container-queries are you using?

0.1.0

What browser are you using?

Chrome 108.0.5359.125

What operating system are you using?

Windows

Reproduction repository

https://play.tailwindcss.com/ZJqWpcCdgG

Describe your issue

I would like to use calc as the container size. This works in CSS, but doesn't work in Tailwind.
https://chromestatus.com/feature/5643732019838976 - a grain of information as to why this should work.
CSS version works in Chrome 108, Firefox 110 Nightly (stable does not support CQ yet), Safari (some of the latest version).

dynamic attribute No space

What version of @tailwindcss/container-queries are you using?

0.1.1

What version of Node.js are you using?

v18.19.0

What browser are you using?

Chrome or Whale

What operating system are you using?

osX or windows

Reproduction repository

https://play.tailwindcss.com/4ZDDvrPavL

Describe your issue

image
To solve this problem, I tried to increase the _ to __, but it still often does not include spaces when rebuilding.

Bug using container queries with angular

What version of @tailwindcss/container-queries are you using?

v0.1.0

What version of Node.js are you using?

18.12.1

What browser are you using?

Chrome

What operating system are you using?

Windows

Reproduction repository

https://github.com/Eoreata/tailwindcss-container-queries-bug

Describe your issue

The issue is difficult to describe and the bug extremely unpredictable, so I had to make a video of it : https://youtu.be/b5HytyqPjvU

In short, when using container queries with angular, it may (or may not) work and it's hard to say how to reproduce the error. It seems to be related to the use of [class]. Sometimes it works, sometimes it only works after some weird manipulations (like adding commented codes containing container queries).

Types are not published

What version of @tailwindcss/container-queries are you using?

0.1.0

What version of Node.js are you using?

18.14.2

What browser are you using?

N/A

What operating system are you using?

macOS

Reproduction repository

https://www.npmjs.com/package/@tailwindcss/container-queries/v/0.1.0?activeTab=code

Describe your issue

The dist/index.d.ts file is missing in the published package on NPM.
Sorry I wasn't able to make a reproduction repository, I initially tried forking the repository to fix and make a PR instead of an issue, but I can't reproduce the problem locally: npm run build then npm pack gives me a package that contains the dist/index.d.ts file. The logs for the action that published the 0.1.0 version on NPM are gone so no luck there either.

Arbitrary values are not applied when using the `@md:`/`@xs:`/etc. container size classes

What version of @tailwindcss/container-queries are you using?

0.1.1

What version of Node.js are you using?

19.3.0

What browser are you using?

Microsoft Edge 112.0.1722.34

What operating system are you using?

Windows

Reproduction repository

https://github.com/revosw/tailwindcss-container-query-arbitrary-repro/tree/main

Describe your issue

Arbitrary values are not applied when using the @md:/@xs:/etc. container size classes.

StackBlitz repro link (Be sure to click "open in new tab" in the top right, or else images won't load and the example doesn't properly show the problem)

Expected:
image

Actual:
image

error TS2322 with `exactOptionalPropertyTypes` enabled

What version of @tailwindcss/container-queries are you using?

v0.1.1

What version of Node.js are you using?

v18.16.1

What browser are you using?

n/a

What operating system are you using?

macOS

Reproduction repository

https://github.com/montchr/tailwind-container-queries-types-repro

Describe your issue

With this tsconfig.json:

{
  "$schema": "https://json.schemastore.org/tsconfig",
  "compilerOptions": {
    "exactOptionalPropertyTypes": true,
    "strict": true,
    "esModuleInterop": true
  },
  "include": ["tailwind.config.ts"],
  "exclude": ["node_modules"]
}

And this tailwind.config.ts:

import ContainerQueriesPlugin from '@tailwindcss/container-queries'
import type {Config} from 'tailwindcss'

export default {
  plugins: [ContainerQueriesPlugin],
} satisfies Config

I get the following TypeScript error when attempting to load this plugin:

tailwind.config.ts:5:13 - error TS2322: Type '{ handler: PluginCreator; config?: Partial<Config> | undefined; }' is not assignable to type 'PluginCreator | { handler: PluginCreator; config?: Partial<Config>; } | { (options: any): { handler: PluginCreator; config?: Partial<...>; }; __isOptionsFunction: true; } | undefined'.
  Type '{ handler: PluginCreator; config?: Partial<Config> | undefined; }' is not assignable to type '{ handler: PluginCreator; config?: Partial<Config>; }' with 'exactOptionalPropertyTypes: true'. Consider adding 'undefined' to the types of the target's properties.
    Types of property 'config' are incompatible.
      Type 'Partial<Config> | undefined' is not assignable to type 'Partial<Config>'.
        Type 'undefined' is not assignable to type 'Partial<Config>'.

5   plugins: [ContainerQueriesPlugin],
              ~~~~~~~~~~~~~~~~~~~~~~

When I disable exactOptionalPropertyTypes there is no error.

Fallback for Container Queries

What version of @tailwindcss/container-queries are you using?

v0.1.0

What version of Node.js are you using?

What browser are you using?

Chrome 109.0.5414.75

What operating system are you using?

Windows 10

Reproduction repository

https://play.tailwindcss.com/ksciGipiQh

Describe your issue

I would like to add a fallback option in case Container Queries are not supported.
supports-[container-type:_inline-size]:@lg/someName:modern_styles & @lg:fallback_styles looks like a good choice for this purpose. The problem is that the styles @lg: override the supports-... styles.

image

The workaround may be !important or supports-[not..., but this solution is not always possible.

Missing support for core-supported dynamic breakpoints

What version of @tailwindcss/container-queries are you using?

v0.1.1

What version of Node.js are you using?

v18.16.0

What browser are you using?

Chrome v112.0.5615.137

What operating system are you using?

macOS v13.1 (22C65)

Tailwind Play Reproduction

https://play.tailwindcss.com/iyNosSvgd1

Describe your issue

Purpose / Problem

There currently exists no way AFAIK to use the dynamic breakpoints functionality newly introduced in TailwindCSS v3.2, or anything comparable when it comes to container queries.

There is this arbitrary syntax supported: @[17.5rem]:underline however…

  • no max- equivalent exists
  • the syntax for this departs a bit from the core non-container-related implementation of min-*

Proposed Solution

It might be helpful to follow the core implementation's lead by adding support for min-* and max-* dynamic breakpoints for arbitrary values:

<div class="@container grid h-screen w-screen">
  <div class="bg-[red] @min-[1px]:bg-[green]"></div>
  <div class="bg-[red] @max-[1px]:bg-[green]"></div>
</div>

This way, it works identically to the core implementation but is prepended with @, which is already being used by this plugin to denote container-related queries.

Support for width/height distinction

Furthermore, per a related PR to the core implementation to add support for an explicit w:/h: prefix to the arbitrary value to determine the dimension (e.g. min/max-width/height), implementing the same pattern could be helpful here if that PR passes, which could work like this:

<!--
  `w:` is implied when no `w:`/`h:` prefix is specified,
  but it can also be included explicitly, optionally
-->
<div class="@min-[h:100px]:@max-[100px]:font-bold" />

Syntax consideration

It may be worth considering which of these syntaxes makes more sense in line with the core implementation:

@ before min/max

(as shown above)

  • @min-[1px]:
  • @min-[w:1px]:
  • @min-[h:1px]:
  • @max-[100vw]:
  • @max-[w:100vw]:
  • @max-[h:100vw]:

@ before […]

(as shown above)

  • min-@[1px]:
  • min-@[w:1px]:
  • min-@[h:1px]:
  • max-@[100vw]:
  • max-@[w:100vw]:
  • max-@[h:100vw]:

Make the @ prefix configurable

What version of @tailwindcss/container-queries are you using?

0.1.1

What version of Node.js are you using?

v18.17.1

What browser are you using?

Edge

What operating system are you using?

Windows

Reproduction repository

https://github.com/tailwindlabs/tailwindcss-container-queries

Describe your issue

When using this plugin in ASP.net razor pages (cshtml / razor), the @ sign is used to input code/variables. This makes it tricky to use as a prefix for classes.

Normally you would escape it using another @, like @@container - but then tailwind won't recognize the clase. Another way is to use @("@container") but that is annoying to write.

Would it be possible to make the prefix configurable, so we could use eg. # or $ instead?

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.