【问题标题】:convert multiple class name to one class in html在html中将多个类名转换为一个类
【发布时间】:2022-01-14 11:38:34
【问题描述】:

我在我的 vue 项目中使用了 tailwind 预定义的 css。

<p class="bg-white text-black focus:text-red focus:bg-white-2 dark:text-white dark:bg-black dark:focus:bg-black-2 dark:focus:text-yellow">some text</p>
<p class="bg-white text-black focus:text-red focus:bg-white-2 dark:text-white dark:bg-black dark:focus:bg-black-2 dark:focus:text-yellow">some text2</p>
<p class="bg-white text-black focus:text-red focus:bg-white-2 dark:text-white dark:bg-black dark:focus:bg-black-2 dark:focus:text-yellow">some text 3</p>
<p class="bg-white text-black focus:text-red focus:bg-white-2 dark:text-white dark:bg-black dark:focus:bg-black-2 dark:focus:text-yellow">some other text</p>
<p class="bg-white text-black focus:text-red focus:bg-white-2 dark:text-white dark:bg-black dark:focus:bg-black-2 dark:focus:text-yellow">more texttttttttt</p>

正如您所看到的,对于每个标签,我必须输入所有这些多个类。 我想要的是有什么方法可以让变量类在这样的类上工作

<p class="grouped-class">text</p>
<p class="grouped-class">text 2</p>
<p class="grouped-class">text 3</p>

【问题讨论】:

    标签: html css vue.js tailwind-css tailwind-ui


    【解决方案1】:

    你可以使用@apply

    HTML:

     <!-- Before extracting a custom class -->
    <button class="py-2 px-4 bg-blue-500 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400 focus:ring-opacity-75">
      Save changes
    </button>
    
    <!-- After extracting a custom class -->
    <button class="btn-primary">
      Save changes
    </button>
    

    CSS:

    @tailwind base;
    @tailwind components;
    @tailwind utilities;
    
    @layer components {
      .btn-primary {
        @apply py-2 px-4 bg-blue-500 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-400 focus:ring-opacity-75;
      }
    }
    

    来自顺风文档https://tailwindcss.com/docs/functions-and-directives#layer

    【讨论】:

    猜你喜欢
    • 2011-04-10
    • 2022-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    相关资源
    最近更新 更多