1

有没有办法使用Tailwind CSS在 HTML 组件的背景图像样式上调整线性渐变角度?我唯一能做的就是在方向选项之间进行选择:'t(top)'、'tr(top-right)',但我想将带有 Tailwind 的 hr 元素的渐变角度设置为 24 度像“ .bg -gradient- [ 160deg ]”这样的类(和颜色:“.from-lime”“.to-red”)

4

2 回答 2

1

当然,您可以为此任务编写一个简单的插件

const plugin = require('tailwindcss/plugin')

module.exports = {
  theme: {
    extend: {
      // custom user configuration
      bgGradientDeg: {
        75: '75deg',
      }
    }
  },
  plugins: [
    plugin(function({ matchUtilities, theme }) {
      matchUtilities(
          {
              'bg-gradient': (angle) => ({
                  'background-image': `linear-gradient(${angle}, var(--tw-gradient-stops))`,
              }),
          },
          {
              // values from config and defaults you wish to use most
              values: Object.assign(
                  theme('bgGradientDeg', {}), // name of config key. Must be unique
                  {
                      10: '10deg', // bg-gradient-10
                      15: '15deg',
                      20: '20deg',
                      25: '25deg',
                      30: '30deg',
                      45: '45deg',
                      60: '60deg',
                      90: '90deg',
                      120: '120deg',
                      135: '135deg',
                  }
              )
          }
       )
    })
  ],
}

并像使用它一样

<div class="h-40 from-red-500 via-yellow-500 to-blue-500 bg-gradient-90">
  90 deg from defaults
</div> 

<div class="h-40 from-red-500 via-yellow-500 to-blue-500 bg-gradient-10 sm:bg-gradient-60">
  10 deg on mobile,
  60 on desktops
</div> 

<div class="h-40 from-red-500 via-yellow-500 to-blue-500 bg-gradient-[137deg] sm:bg-gradient-to-br">
  137 deg from JIT on mobile,
  to bottom right on desktop
</div> 

<div class="h-40 from-red-500 via-yellow-500 to-blue-500 bg-gradient-75">
  75 deg from user's custom config
</div>

演示

如果有帮助,我刚刚为此创建了 Tailwind v3 的简单

于 2022-02-15T15:52:37.930 回答
-1
.repeating-linear {
background: repeating-linear-gradient(-45deg, red, red 5px, blue 5px, blue 10px);
}

通过在具有重复线性梯度的类中给出角度来尝试这一点。

于 2022-02-15T02:32:09.483 回答