【问题标题】:Nested syntax with PostCSS for :hover and :focusPostCSS 的嵌套语法用于 :hover 和 :focus
【发布时间】:2020-12-12 11:26:26
【问题描述】:

我正在使用 TailwindCSS 和 PostCSS,我有这个 css 代码:

.btn {
  @apply py-1;
  @apply px-4;
  @apply border;
  @apply rounded;
}

.btn:hover {
  @apply bg-white text-black;
}

.btn:focus {
  @apply bg-black text-white;
}

PostCSS 中是否有本地方式(或使用插件)来编写如下代码?

.btn {
  @apply py-1;
  @apply px-4;
  @apply border;
  @apply rounded;

  &:hover {
    @apply bg-white text-black;
  }
  
  &:focus {
    @apply bg-black text-white;
  }
}

【问题讨论】:

标签: css postcss tailwind-css postcss-import


【解决方案1】:

使用postcss-preset-env

第一次安装,npm install postcss-preset-env --save-dev

然后在您的postcss.config.js 文件中启用nesting-rules

module.exports = {
  plugins: [
    "tailwindcss",
    [
      "postcss-preset-env",
      {
        stage: 3,
        features: {
          "nesting-rules": true,
        },
      },
    ],
  ],
};

在这里你可以找到可以启用的list of features ID

【讨论】:

    【解决方案2】:

    您也可以使用postcss-nested 插件。

    在你的package.json:

    {
      "dependencies": {
        "postcss": "^8.2.9",
        "tailwindcss": "^2.0.4",
        "postcss-nested": "^5.0.5"
      }
    }
    

    在你的postcss.config.js:

    module.exports = {
      plugins: [
        require('postcss-nested'),
        require('tailwindcss'),
      ]
    }
    

    【讨论】:

      【解决方案3】:

      也可以,遵循对象符号Reference

      module.exports = {
          plugins: {
              'postcss-import': {},
              tailwindcss: {},
              autoprefixer: {},
              'postcss-preset-env': { stage: 2 },
          },
      }
      

      【讨论】:

        猜你喜欢
        • 2011-09-02
        • 2021-12-31
        • 1970-01-01
        • 2021-11-03
        • 1970-01-01
        • 2013-04-05
        • 1970-01-01
        • 2022-12-18
        • 1970-01-01
        相关资源
        最近更新 更多