简体   繁体   English

Django顺风自定义任意颜色不起作用

[英]Django tailwind custom arbitrary colors not working

I've been using django with django-tailwind to build a website that involves color mixing.我一直在使用 django 和 django-tailwind 来构建一个涉及颜色混合的网站。 I take a bunch of colors from a database and mix them together.我从数据库中取出一堆颜色并将它们混合在一起。 This results in new colors that I can't write down in the tailwind config and so I've been trying to use the arbitrary values custom colors from the documentation.这导致我无法在顺风配置中写下新颜色,因此我一直在尝试使用文档中的任意值自定义颜色。 The css for the color shows up correctly in my inspector but the color itself doesn't compile.颜色的 css 在我的检查器中正确显示,但颜色本身无法编译。

I've also noticed that if I manually enter the color hex anywhere in the code (on another element for example), all elements with that specific color code get rendered correctly so I'm guessing it's something to do with django-tailwind not compiling the colors since they are determined during runtime or something.我还注意到,如果我在代码中的任何位置(例如在另一个元素上)手动输入颜色十六进制,具有该特定颜色代码的所有元素都会正确呈现,所以我猜这与 django-tailwind 不编译有关颜色,因为它们是在运行时或其他东西中确定的。

My django template code is as follows:我的django模板代码如下:

{% for day_obj in days %}
    <div class="flex flex-col bg-[{{day_obj.day.color}}]">

and here it is from the chrome inspector这是来自 chrome 检查员在此处输入图像描述

But it doesn't work.但它不起作用。 However, this然而,这

{% for day_obj in days %}
    <div class="flex flex-col bg-[{{day_obj.day.color}}]">
      <div class="flex items-center justify-between leading-tight">
        <h2 class="text-3xl font-bold">{{ day_obj.weekday }}</h2>
        <p>{{ day_obj.day.date }}</p>
      </div>
      <div class="grid grid-cols-{{ day_obj.habits|length }}">
        {% for habit in day_obj.habits %}
        <div class="bg-[{{habit|hex_color}}] text-white">&nbsp;</div>
        {% endfor %}
      </div>
      <div class="flex flex-col bg-[#82e153]">

will render all the elements correctly that have that specific color code: #82e153.将正确渲染具有该特定颜色代码的所有元素:#82e153。 Elements that are rendered from before also tend to stick around after I remove the manually-entered-hex-code but usually just stop rendering after a while (I assume due to some sort of caching of the color in the stylesheet)在我删除手动输入的十六进制代码后,之前渲染的元素也往往会留下来,但通常会在一段时间后停止渲染(我假设是由于样式表中的某种颜色缓存)

Is there a way to make this work or should I just resort to using something else like the CDN?有没有办法让这项工作,或者我应该求助于使用其他类似 CDN 的东西? I really want to stick to django-tailwind for the time being.我真的很想暂时坚持使用 django-tailwind。

I think you could use CSS variables for this.我认为您可以为此使用 CSS 变量。 See tailwind docs. 请参阅顺风文档。 You could add a color to the tailwind config like:您可以向顺风配置添加颜色,例如:

module.exports = {
  theme: {
    extend:
      colors: {
        mixed: 'rgb(var(--color-mixed) / <alpha-value>)',
    }, 
  }
}

Now you can use the bg-mixed class in your html.现在您可以在 html 中使用bg-mixed类。

Setting the CSS var you can do with a style attribute: style="--color-mixed: 130 225 83" (these need to be RGB values separated by a space).设置 CSS var,您可以使用样式属性: style="--color-mixed: 130 225 83" (这些需要是用空格分隔的 RGB 值)。 I do not know much about Django, but I think replacing the value by {{day_obj.day.color}} will work, because the browser will add the value to the CSS var, which will be used by the compiled Tailwind class.我对 Django 了解不多,但我认为用{{day_obj.day.color}}替换值会起作用,因为浏览器会将值添加到 CSS var 中,编译后的 Tailwind 类将使用该值。

<div class="flex flex-col bg-mixed" style="--color-mixed: {{day_obj.day.color}}">

You may need to add the initial value of the var to the :root in the base layer in your index.css , like:您可能需要将 var 的初始值添加到index.css基础层的:root中,例如:

@layer base {
  :root {
    --color-mixed: 255 255 255; /* White */
  }
}

Hope this helps.希望这可以帮助。

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

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