51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Tailwind CSS为什么不能将伪类(例如:hover)应用到我的HTML元素?

英文:

Why is tailwind CSS not applying psuedo classes such as hover to my HTML elements?

问题 {#heading}

以下是您要翻译的HTML和配置文件部分:

<!DOCTYPE html>
<html lang="en">

<head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Test</title> <script src="https://cdn.tailwindcss.com"></script> <!--link rel="stylesheet" href="output.css" CDN FOR SO Purposes--> </head>

<body class="bg-gray-100"> <main> <div class="flex justify-center items-center min-h-screen"> <div class="grid-flow-row max-w-[330px] w-full bg-white p-8 rounded-lg shadow-lg"> <div class="mx-auto"> <img class="" src="../src/img/logo.gif" alt="logo"> </div> <div class="border w-full"></div> <div class="grid justify-items-center content-center"> <form class="grid justify-items-center content-center"> <input class="border mt-2 rounded-md p-1" type="text" id="username" placeholder="Username" autocomplete="username"> <input class="border mt-2 rounded-md p-1" type="password" id="password" placeholder="Password" autocomplete="current-password"> </form> <button class="rounded bg-gray-300 mt-4 text-white font-bold h-8 w-1/2" onclick="">Submit</button> </div> </div> </div> </main> </body>

</html>

配置文件:

module.exports = {
  content: ["./dist/**/*.{html,js}"],
  theme: {
    extend: {},
  },
  plugins: [],
}

请注意,我已经将HTML和配置文件中的HTML标签和属性保留在翻译中,以保持代码的完整性。 英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;

&amp;lt;head&amp;gt; &amp;lt;meta charset=&amp;quot;UTF-8&amp;quot;&amp;gt; &amp;lt;meta name=&amp;quot;viewport&amp;quot; content=&amp;quot;width=device-width, initial-scale=1.0&amp;quot;&amp;gt; &amp;lt;title&amp;gt;Test&amp;lt;/title&amp;gt; &amp;lt;script src=&amp;quot;https://cdn.tailwindcss.com&amp;quot;&amp;gt;&amp;lt;/script&amp;gt; &amp;lt;!--link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;output.css&amp;quot; CDN FOR SO Purposes--&amp;gt; &amp;lt;/head&amp;gt;

&amp;lt;body class=&amp;quot;bg-gray-100&amp;quot;&amp;gt; &amp;lt;main&amp;gt; &amp;lt;div class=&amp;quot;flex justify-center items-center min-h-screen&amp;quot;&amp;gt; &amp;lt;div class=&amp;quot;grid-flow-row max-w-[330px] w-full bg-white p-8 rounded-lg shadow-lg&amp;quot;&amp;gt; &amp;lt;div class=&amp;quot;mx-auto&amp;quot;&amp;gt; &amp;lt;img class=&amp;quot;&amp;quot; src=&amp;quot;../src/img/logo.gif&amp;quot; alt=&amp;quot;logo&amp;quot;&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;div class=&amp;quot;border w-full&amp;quot;&amp;gt;&amp;lt;/div&amp;gt; &amp;lt;div class=&amp;quot;grid justify-items-center content-center&amp;quot;&amp;gt; &amp;lt;form class=&amp;quot;grid justify-items-center content-center&amp;quot;&amp;gt; &amp;lt;input class=&amp;quot;border mt-2 rounded-md p-1&amp;quot; type=&amp;quot;text&amp;quot; id=&amp;quot;username&amp;quot; placeholder=&amp;quot;Username&amp;quot; autocomplete=&amp;quot;username&amp;quot;&amp;gt; &amp;lt;input class=&amp;quot;border mt-2 rounded-md p-1&amp;quot; type=&amp;quot;password&amp;quot; id=&amp;quot;password&amp;quot; placeholder=&amp;quot;Password&amp;quot; autocomplete=&amp;quot;current-password&amp;quot;&amp;gt; &amp;lt;/form&amp;gt; &amp;lt;button class=&amp;quot;rounded bg-gray-300 mt-4 text-white font-bold h-8 w-1/2&amp;quot; onclick=&amp;quot;&amp;quot;&amp;gt;Submit&amp;lt;/button&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/div&amp;gt; &amp;lt;/main&amp;gt; &amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;


<!-- end snippet -->

On the button when I add hover:bg-gray-400 nothing happens when you hover over it. It keeps the bg-gray-300 class and is not overridden but does not style with the pseudo class as expected. I'm trying to get pseudo classes to work but I just can't seem to figure it out. The issue does not lay in my code. It is in the setup of tailwind and I followed tailwindcss's directions to the t. They had to have changed the commands and not updated the docs

file structure is as follows

SRC↓
   |&gt;dist&gt;[index.html, output.css]
   |&gt;node_modules&gt;[lots of stuff]
   |&gt;src&gt;
&lt;div id=&quot;slider-single&quot; class=&quot;owl-carousel slider-single be-wol&quot;&gt;&lt;/div&gt;, input.css]

|&amp;gt;package-lock.json |&amp;gt;package.json |&amp;gt;tailwind.config.js


Below is my config file.
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

module.exports = {
  content: [&quot;./dist/**/*.{html,js}&quot;],
  theme: {
    extend: {},
  },
  plugins: [],
}

<!-- end snippet -->

答案1 {#1}

得分: 0

Tailwind 的输出文件可能很小,因为它支持树摇。未使用的类在运行构建命令时不会放入文件中。我只是没有意识到,我没有在命令的末尾添加 --Watch 选项来在开发环境中持续构建。所以确保在
npx tailwindcss -i 'input file' -o 'output file'.css
的末尾添加 --Watch。 英文:

The output file for tailwind can be small because of tree-shaking. The classes that aren't used won't be put in the file when running the build command. I had just not realized that I didn't add --Watch at the end of the command to keep building in the dev environment. So absolutely make sure you add --Watch to the end of
npx tailwindcss -i &#39;input file&#39; -o &#39;output file&#39;.css


赞(3)
未经允许不得转载:工具盒子 » Tailwind CSS为什么不能将伪类(例如:hover)应用到我的HTML元素?