【问题标题】:Tailwindcss box shadow not showingTailwindcss 框阴影未显示
【发布时间】:2021-12-31 11:07:21
【问题描述】:

我正在尝试在一个 React 项目中使用 Tailwindcss,我在该项目中复制了《星球大战》website 移动菜单。但是,当导航抽屉打开时,我添加到汉堡图标段的框阴影没有显示。

沙盒链接:https://play.tailwindcss.com/upmiAWTcso

index.js

const toggleDrawer = (event) => {
  document.querySelector("#drawer").classList.toggle("left-[-100%]");
  document.querySelector("#drawer").classList.toggle("left-0");
  document.querySelector("#bar-1").classList.toggle("hidden");
  document.querySelector("#bar-2").classList.toggle("active-2");
  document.querySelector("#bar-3").classList.toggle("active-3");
};

<div
  onClick={toggleDrawer}
  className="h-full flex flex-col justify-center items-center space-y-[8px]"
>
  <span
    id="bar-1"
    className="block h-[2px] w-[30px] border-zinc-500 border-l-[4px] border-r-[20px] rounded-full transition-all duration-300"
  ></span>
  <span
    id="bar-2"
    className="block h-[2px] w-[30px] border-zinc-500 shadow-md border-l-[20px] border-r-[4px] rounded-full origin-bottom-right transition-all duration-300"
  ></span>
  <span
    id="bar-3"
    className="block h-[2px] w-[30px] border-zinc-500 shadow-md border-l-[4px] border-r-[20px] rounded-full origin-bottom-left transition-all duration-300"
  ></span>
</div>;

global.css

@layer components {
  .active-2 {
    @apply
      rotate-45
      -translate-x-2
      translate-y-[530%]
      !w-[34px]
      !border-l-[25px]
      !border-r-[5px]
      !border-white
      shadow-[#d50032];
  }
  .active-3 {
    @apply
      -rotate-45
      translate-x-1
      !w-[34px]
      !border-l-[5px]
      !border-r-[25px]
      !border-white
      shadow-[#106ae0] !important;
  }
}

【问题讨论】:

    标签: css reactjs jsx tailwind-css


    【解决方案1】:

    您只声明了一种阴影颜色。您还需要像 shadow-md 这样的阴影大小。

    这是 Tailwind Play 上的一个示例,显示了 https://play.tailwindcss.com/gxG7cir5EQ 的区别

    【讨论】:

    • 感谢您的帮助。我相信我在标签本身而不是 CSS 中定义了类 shadow-md
    • 我创建了一个较小的示例 (play.tailwindcss.com/PPQS6s7JWZ),但问题似乎仍然存在
    • 所以这只是在对于特定阴影设置而言太小的元素上使用较大的阴影实用程序的情况。大的 Tailwind 阴影并不打算用于 2px 高度的元素(不知道为什么从 UI 的角度来看你也想要这个)这是你的同一个 Play 示例,但我将第二个“bar”设置为足够高以显示 xl 阴影和第三个我做到了shadow这两项工作。我还删除了bg-black 以使其更明显。 play.tailwindcss.com/SVJqf75iEg
    • 对此行为的一般解释是 TW 阴影使用负扩散值来传达深度。当您有常规大小的元素时,这很好,但是如果与负值相比,元素低于某个阈值,那么整个阴影就会消失,因为生成阴影的对象中没有足够的表面积来补偿负值传播。
    【解决方案2】:

    您以错误的方式应用!important。要将!important 添加到顺风类中,只需添加!,添加如下开头:

    @layer components {
      .active-2 {
        @apply
          rotate-45
          -translate-x-2
          translate-y-[530%]
          w-[34px]
          border-l-[25px]
          border-r-[5px]
          border-white
          !shadow-[#d50032];
      }
      .active-3 {
        @apply
          -rotate-45
          translate-x-1
          w-[34px]
          border-l-[5px]
          border-r-[25px]
          border-white
          !shadow-[#106ae0];
      }
    }
    

    【讨论】:

    • 感谢您的回复。不幸的是,这仍然没有解决问题,我真的不知道为什么。即使当我检查元素时,它也显示应用了正确的样式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多