简体   繁体   English

Tailwind z-index 任意值不起作用

[英]Tailwind z-index arbitrary value is not working

I'm trying to use Tailwind's z-index with arbitrary value as per documentation , but it looks like the CSS is not generating (see attached screenshot).我正在尝试根据文档使用具有任意值的 Tailwind 的 z-index,但看起来 CSS 没有生成(请参阅随附的屏幕截图)。

Although I use it with React in the project, I've tried using z-index with arbitrary values in plain HTML to make sure it's even working.虽然我在项目中将它与 React 一起使用,但我尝试在普通 HTML 中使用具有任意值的 z-index 以确保它甚至可以正常工作。 But it's doesn't.但事实并非如此。

Eg I want to display 5 boxes 1st one should be the topmost and the 5th bottommost using z-index with arbitrary values.例如,我想显示 5 个框,第一个应该是最顶部的,第五个应该是最底部的,使用具有任意值的 z-index。 CodePen example and HTML code below that is not working. CodePen 示例和下面的 HTML 代码不起作用。 In the same CodePen you can see that using z-10, z-20, ... and so on is working.在同一个 CodePen 中,您可以看到使用 z-10、z-20 等是有效的。

<div class="flex justify-center items-center w-20 h-20 shadow-lg relative bg-red-300 z-[5]">1</div>
<div class="flex justify-center items-center w-20 h-20 shadow-lg relative bg-purple-200 -top-3 left-3 z-[4]">2</div>
<div class="flex justify-center items-center w-20 h-20 shadow-lg relative bg-green-200 -top-6 left-6 z-[3]">3</div>
<div class="flex justify-center items-center w-20 h-20 shadow-lg relative bg-blue-200 -top-9 left-9 z-[2]">4</div>
<div class="flex justify-center items-center w-20 h-20 shadow-lg relative bg-yellow-200 -top-12 left-12 z-[1]">5</div>

The tailwind.config.js file for the project I use:我使用的项目的tailwind.config.js文件:

const defaultTheme = require('tailwindcss/defaultTheme')

module.exports = {
  mode: 'jit',
  content: ['./src/**/*.js'],
  darkMode: 'media',
  theme: {
    extend: {
      fontFamily: {
        sans: ['Inter var', ...defaultTheme.fontFamily.sans]
      },
      colors: {
        transparent: 'transparent',
        current: 'currentColor',
        indy: {
          50: '#e0d3ef',
          100: '#c4b7d4',
          200: '#a99cb8',
          300: '#8d809d',
          400: '#726482',
          500: '#564867',
          600: '#3b2d4b',
          700: '#1f1130',
          800: '#1a0f29',
          900: '#150c21'
        },
        error: {
          DEFAULT: '#FF0055',
          50: '#FFB8CF',
          100: '#FFA3C2',
          200: '#FF7AA7',
          300: '#FF528B',
          400: '#FF2970',
          500: '#FF0055',
          600: '#C70042',
          700: '#8F0030',
          800: '#57001D',
          900: '#1F000A'
        },
        success: {
          DEFAULT: '#22CF6B',
          50: '#B6F3D0',
          100: '#A4F0C4',
          200: '#81EAAD',
          300: '#5EE597',
          400: '#3BDF80',
          500: '#22CF6B',
          600: '#1A9F52',
          700: '#126F39',
          800: '#0A3E20',
          900: '#020E07'
        }
      }
    }
  },
  variants: {
    extend: {
      opacity: ['disabled']
    }
  },
  plugins: [require('@tailwindcss/forms')]
}

在此处输入图像描述

EDIT :编辑

  1. The example in the CodePen was not working due to a Tailwind v2, that didn't support arbitrary values.由于 Tailwind v2 不支持任意值,CodePen 中的示例无法正常工作。

  2. In my example I need a custom z-index for each item in a list of rows, because each one of these items has a dropdown that when opened appears below the list.在我的示例中,我需要为行列表中的每个项目自定义 z-index,因为这些项目中的每一个都有一个下拉列表,打开时会显示在列表下方。 So to generate the z-index value for the arbitrary value I use the following code:因此,要为任意值生成 z-index 值,我使用以下代码:

const containerClasses = `relative inline-block text-left left-[-75px] z-[${itemsLength - index}]`
// itemsLength is the length of items in an array
// index is the index of each item

You are trying to generate dynamic class names:您正在尝试生成动态 class 名称:

`z-[${itemsLength - index}]`

which won't work because Tailwind doesn't actually evaluate your source code.这不会起作用,因为 Tailwind 实际上不会评估您的源代码。 Tailwind does not know that z-[${itemsLength - index}] will become z-[1], z-[2] etc. (this code is compiled in React). Tailwind 不知道z-[${itemsLength - index}]会变成z-[1], z-[2]等(这段代码是在 React 中编译的)。

Tailwind can only detect static unbroken class strings, which means that your classes need to exist as complete strings for Tailwind to detect them correctly. Tailwind 只能检测 static 完整的 class 字符串,这意味着您的类需要作为完整字符串存在,Tailwind 才能正确检测它们。

wrong :错误

<div class="text-{{ error ? 'red' : 'green' }}-600"></div>

correct :正确

<div class="{{ error ? 'text-red-600' : 'text-green-600' }}"></div>

Now, a possible solution would be to " safelist " some classes (Tailwind will generate certain class names that don't exist in your content files), by using safelist option:现在,一个可能的解决方案是通过使用安全列表选项将某些类“列入安全列表”(Tailwind 将生成内容文件中不存在的某些safelist名称):

safelist: [
    'z-[1]',
    'z-[2]',
    ...
  ]

but I don't know if it suits you, since the number of items might be variable.但我不知道它是否适合你,因为项目的数量可能是可变的。

My suggestion is to:我的建议是:

  • reverse the order into the items array (in DOM, the first item will be the last element)将顺序反转到items数组中(在 DOM 中,第一个项目将是最后一个元素)
  • use display: flex; flex-direction: column-reverse;使用display: flex; flex-direction: column-reverse; display: flex; flex-direction: column-reverse; (in Tailwind, flex flex-col-reverse ) on the parent. (在 Tailwind 中, flex flex-col-reverse )在父级上。 In this way, you don't need z-index anymore.这样,你就不再需要 z-index 了。

https://play.tailwindcss.com/2JjQSwBYlB https://play.tailwindcss.com/2JjQSwBYlB

More info in Tailwind Docs: Tailwind 文档中的更多信息:

The problem is the Tailwind version you are using.问题是您使用的 Tailwind 版本。 The documentation you are making reference is v3.2.4 but your codepen example uses v2: https://v2.tailwindcss.com/docs/z-index您参考的文档是 v3.2.4,但您的代码笔示例使用 v2: https://v2.tailwindcss.com/docs/z-index

Upgrade your tailwind version to v3.2.4 and try again.将您的 tailwind 版本升级到 v3.2.4,然后重试。

This is your code using v3.2.4 https://play.tailwindcss.com/L1hP3hoeHY这是您使用 v3.2.4 https://play.tailwindcss.com/L1hP3hoeHY的代码

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

相关问题 Tailwind css 和 Next.js 导航组件 z-index 不工作 - Tailwind css and Next.js nav component z-index not working 使用暗滤镜作为内容和顺风 css 的 flowbite 模式校正背景图像的 z-index 顺序 - correcting z-index order for background image with dark filter as content and flowbite modal from tailwind css 顺风转换延迟任意值仅适用于特定值 - Tailwind transition delay arbitrary value only working for specific values Django顺风自定义任意颜色不起作用 - Django tailwind custom arbitrary colors not working 在 SCSS 中使用 Tailwind 任意值支持 - Using Tailwind Arbitrary Value Support with SCSS 下拉传递落后于 swiperjs、z-index 问题或 tailwindcss 问题? - Dropdown pass behind swiperjs, z-index issue or tailwindcss issue? Tailwind 的任意断点值在 react.js 中停止工作 - Tailwind's arbitrary values for breakpoints stopped working in react.js NextJS Image 组件与其他组件重叠,与 z-index 无关 - NextJS Image component is overlapping other components regardless of z-index 使用 React 在 Tailwind 上出现任意值的问题 - Problem with arbitrary values on Tailwind with React 如何在tailwindcss中使用z-index将元素隐藏在父元素后面? - How to use z-index in tailwindcss for hiding an element behind the parent element?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM