How to customize the hover and active properties of a checked checkbox in Vue 3 using Tailwind CSS and PUG?

It seems that checkboxes have a level of sophistication that I never anticipated, or perhaps my brain just can't quite grasp the nuances of checkboxes.

After spending a significant amount of time researching and experimenting with checkboxes in a Vue 3, Tailwind, and PUG setup, I've come to a roadblock. I want to emphasize that I aim to avoid placing any styling changes within a <style> block if possible.

I created a <template> where two custom checkboxes are rendered based on whether they are checked or unchecked. Why go through the trouble? Well, I have a design system that requires different CSS styles for hover and active states depending on the checkbox's status. Let me provide an example scenario to clarify what I'm dealing with.

  1. When the checkbox is unchecked:
  • The checkbox should be gray, light gray when hovered over, and somewhere between gray and light gray when activated.
  1. When the checkbox is checked:
  • The checkbox should display as "light" dark blue, transition to "ridiculous" dark blue when hovered, and turn into "ludicrous" dark blue when activated.

Trying to achieve these various states within a single rendered checkbox option became increasingly challenging.

In the case of the checked box, while the Tailwind variant successfully applies the "light" dark blue color, any attempts to incorporate the hover and active states yield the default lighter blue from the browser's stylesheet instead of the specified dark blue.

Conversely, the unchecked box behaves as intended, showcasing the three distinct shades of gray mentioned earlier using Tailwind's hover and active pseudo-states effectively.

At this point, I feel uncertain about how to achieve the "ridiculous" dark blue and "ludicrous" dark blue colors for the hover and active states of the CHECKED box.

Below is the snippet of code I'm working with, written in PUG.

<template lang="pug">
.p-checkbox(v-if="!checked")
  .flex
    input(
      v-model="checked"
      class=[
        'bg-placeholder',
        'rounded-sm',
        'border-2',
        'border-checkbox',
        'shadow-checkbox',
        ]
      :class=`[
        'hover:bg-a',
        'active:bg-b',
        ]`
.p-checkbox(v-if="checked")
  .flex
    input(
      v-model="checked"
      type="checkbox"
      class=[
        'rounded-sm',
        'border-2',
        'shadow-checkbox',
        ]
      :class=`[
        'checked:bg-primary',
        'hover:bg-primary-hover',
        'active:bg-primary-active',
        ]`
</template>
<script>
const props = defineProps({
  checked: {
    type: Boolean,
    default: false,
  },
});
</script>

Answer №1

It might be a little delayed, but you have the ability to combine Tailwind modifiers to achieve your desired outcome.

For instance:

<input  type="checkbox"
        v-model="checked"
        class="
          bg-placeholder
          rounded-sm
          border-2
          border-checkbox
          shadow-checkbox
          hover:bg-a
          active:bg-b 
          checked:bg-primary
          checked:hover:bg-primary-hover
          checked:active:bg-primary-active
        "/>

More information can be found in the documentation: https://tailwindcss.com/docs/hover-focus-and-other-states

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for inserting a hyperlink onto an image within a query slider?

I am experiencing difficulties when attempting to add hyperlinks to the images in my slider. I have come to understand that images cannot be clickable in jQuery sliders, so I have tried the following in the HTML: <div class="wrapper"> <div class= ...

The next column featuring a dropdown menu

My goal is to make this sketch operational. The red elements in the sketch need to be programmed. Currently, I am using angularJS and bootstrap with the following code: <div class="col-md-6"> <div class="form-group"> <label&g ...

Trying to fetch an image from a server using VueJS?

How can I fetch an image from the server in Vue.js and display it on a Blade template in Laravel? The issue seems to be with displaying the image using the code below. <tr v-for="post in posts"> <td>{{post.title}}</td> <td&g ...

What technique works best for changing the visibility of an image without causing other elements to shift position?

On my webpage, I have a table cell with an image that should only display when hovering over the cell. The problem is that when the image is shown, it shifts the other text to the left because its default state is "display:none". I'm searching for a s ...

My intention is to personalize the react-select component to suit my needs

My task involves changing the size of the arrow to be smaller and positioning it under the circle. Despite setting the width and height in the CSS, the desired effect is not achieved. To align with the UI requirements, I need to adjust the swiper-button- ...

Safari displays an inexplicable gray border surrounding the <img/> element, despite the actual presence of the image

https://i.stack.imgur.com/4QR52.png There is an unexpected gray border visible around the dropdown image, as seen above. This occurrence has perplexed me because, according to several other queries, this problem arises when the image's src cannot be ...

Using Vue to turn a string into a mathematical operation

I'm in the process of creating a basic calculator using Vue, but I'm stuck on how to convert a string into a mathematical operation. I've already written the code for building the strings. <template> <div> <input type=&q ...

Vue failing to render content from data object

I recently started using NuxtJs and decided to create a new file named test.vue within the pages directory. Below is the code snippet from my test.vue file: <template> <div> <h1 style="font-size: 30px">{{ message }} </h1> ...

the spillage exhibits only a thin streak of gray

My website is primarily a Single Page Website, however, there are specific pages that can only be accessed by registered users. On the website, there is a section referred to as a "block" where you will find a button labeled "Login / Register". Upon clicki ...

Having trouble locating the module "/Applications/Visual" while running the launch.json configuration

While developing on OSX and Windows, I encountered an error when running my debug config that was working fine over the weekend. The error message reads: internal/modules/cjs/loader.js:583 throw err; ^ Error: Cannot find module "/Applications ...

What are the steps to shift columns to the left within a table?

I need to adjust the alignment of the columns and also create a fixed size space between them. Currently, it appears like this: My desired outcome is something similar to: CSS Code: .table { width:100%; } .table table{ border-collapse: collapse ...

Animating a SVG element within a group tag using the path element

I'm struggling to make a slice of a circle rotate using the :hover selector in CSS. Even though my initial attempt at applying CSS transformations works perfectly, the rotation stops when I introduce the :hover selector. Below is the HTML and CSS co ...

Internet Explorer 8 comes with excess padding at the top and bottom, causing un

Why is IE 8 adding unnecessary padding to a div? I have checked the styling and nothing seems out of place. Can someone assist with this issue? Html The highlighted div in blue is the main concern. .block { clear: both; } .inline { float: lef ...

Retrieving variable values from a dynamic array

I'm struggling with setting a dynamic value within an Object. I am loading some data inside my created hook. created() { let vm = this; axios.all([ axios.get('/api/report/someUrl'), axios.get('/api/report/someUr ...

Tips for utilizing v-model with props within a v-for loop in Vue 3

I am trying to implement v-model with props inside a v-for loop. I have successfully used v-model with props in the past using computed variables, but I am unsure how to achieve this within a v-for loop. Here is my current code: <script setup> const ...

Highlighting table rows on mouseover using CSS

I am trying to add a hover effect to my table rows, but when I apply the CSS styles for this behavior, the rows start wrapping. Here's a comparison of how the table rows look before and after applying the styles. You can see the wrapping issue in the ...

Effective HTML element placement with CSS styling

My HTML code includes a form containing a table. Take a look: This is the Code I'm Using: <html> <head></head> <body> <form onsubmit="" id="form1" action="" method="post" name="form1" style="position: rela ...

How many logical lines of code are in the Ubuntu operating system?

As I develop my web application, it is crucial for me to track the lines of code written in languages such as php, css, html, and JavaScript specific to the /var/www directory. However, when using the command line code counter tool, I find myself tempted ...

Tips for creating responsive designs with specific media queries for various device sizes

I am looking to create a responsive website using media queries and have already created different media queries for mobile, tablets, and desktops. However, I am unsure if I should be writing the same CSS code multiple times for various device sizes, such ...

Issues with jQuery animate.css functionality specifically occurring on Firefox browser

As a beginner in jQuery, I have been exploring different animations and recently came across an issue with Firefox compatibility. I discovered the animate.css library, and decided to incorporate its animations into text captions on a slider using the Soli ...