简体   繁体   English

有没有办法在 Tailwind CSS 中调整线性渐变的角度?

[英]Is there a way to adjust the angle of the linear gradient in Tailwind CSS?

Is there a way to adjust the angle of the linear gradient on a background image style of an HTML component using Tailwind CSS?有没有办法使用 Tailwind CSS 调整 HTML 组件的背景图像样式的线性渐变角度?

The only thing I can do is choose between the directional options: t(top) , tr(top-right) , etc but I want to set the angle of the gradient to 24 degree for an hr element with a Tailwind class like .bg-gradient-[160deg] (and the colors: .from-lime .to-red )我唯一能做的就是在方向选项之间进行选择: t(top)tr(top-right),但我想将带有 Tailwind class 之类的 hr 元素的渐变角度设置为 24 度,例如.bg-gradient-[160deg] (和 colors: .from .from-lime .to .to-red

Sure, you may write a simple plugin for this task当然,您可以为此任务编写一个简单的插件

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',
                  }
              )
          }
       )
    })
  ],
}

and use it like并像使用它一样

<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>

DEMO演示

If it will help, I just created simple package for Tailwind v3 for this如果有帮助,我刚刚为 Tailwind v3 创建了简单的package

.repeating-linear {
background: repeating-linear-gradient(-45deg, red, red 5px, blue 5px, blue 10px);
}

try this by giving the angle in the class with repeating-linear-gradient.通过在 class 中给出具有重复线性梯度的角度来尝试此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM