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

Optimizing Backend Access with Laravel and Vue JS: How to Choose the Most Effective Approach

Currently, I am utilizing Laravel API passport to handle authentication in my Single Page Application (SPA) built with Vue. At the moment, whenever I need to access the backend server, I have to include a header in order to be allowed through the protected ...

What is the best way to transfer data between a checkbox/radiogroup and textview located in separate activities using Eclipse?

Is there a way to display a list of items on different pages with checkboxes and radio buttons that allow users to select items and calculate the total price dynamically? How can this be achieved? Below is the code snippet for the main page: package com. ...

Properly aligning text with checkboxes using HTML/CSS and tags like <span> or <div>

My goal is to have the text displayed as a block in alignment with the checkbox, adjusting based on the sidebar's width. For reference: Current Layout Preferred Layout I have shared the code on CodePen (taking into account screen resolution and wi ...

Combining various components within an inactive element tag using Vue

I am working on creating expandable rows for a table element using this HTML code snippet. The current approach involves alternating between parent rows and multiple rows within tbody elements. <tbody class="js-table-sections-header">Parent row</ ...

Surrounding the text with background color

My H1 text is in a div that spans multiple lines due to the fixed width of the div. I am looking to set the background color to only appear behind the actual text of the H1, without extending into empty space at the end of each line. In addition, I also n ...

Leveraging bootstrap for achieving vertical and horizontal centering of images

My webpage uses bootstrap to display content, and while I've managed to center my images vertically, I'm facing an issue with horizontal centering. I want to ensure that any image, regardless of size, remains within the column/row and adjusts it ...

Ways to initiate a transition upon clicking a button, causing it to switch from being hidden (`display: none`) to visible (`display: block`)

I'm attempting to create a smooth transition effect when a button is clicked, similar to a toggle function, where the content below it seamlessly shifts instead of abruptly moving. The classic example of this is the Bootstrap navbar hamburger menu, wh ...

Blurring the background creates an "inset shadow" problem when transitioning

Problem: Strangely, when using Backdrop-filter: blur(Xpx); during a transition as shown below, unexpected "inset Shadows" mysteriously appear until the end of the transition. https://i.stack.imgur.com/uij7F.gif Illustration of the Issue Using jsfiddle Sp ...

What is the correct way to reset the styling of a web browser element, such as a <button>?

I have come across many sources advising me to reset HTML by manually resetting numerous individual properties, as demonstrated here: https://css-tricks.com/overriding-default-button-styles/ Another approach I discovered in CSS is simply using: button: {a ...

Importing a JSON or JSONC file into a vite/typescript project can be easily done

I am looking for a way to seamlessly share my routes between my actix-web backend and Vue with Vue-Router frontend without needing separate route files. I want to define the routes on the frontend without having to make any changes on the server side. If t ...

Unusual conduct exhibited by jQuery's css() function for setting the width

My goal is to retrieve the width of the initial .imagen element and apply it to the img within .imagen. HTML: <div class="imagen"> <div class="normal"> <img> </div> <div class="hover"> <img> ...

Strange Behavior of SVG 'fill: url(#....)' in Firefox

I am struggling with an SVG graphic that I have created. Here is the code: <svg width='36' height='30'> <defs> <linearGradient id="normal-gradient" x1="0%" y1="0%" x2="0%" y2="100%"> <stop offset="0%" s ...

Tips for Populating Form by Selecting Options from Search Bar Dropdown in Laravel

I successfully implemented a search functionality in a Laravel component that displays data from a MySQL database in a dropdown using the Typeahead Js library following this helpful tutorial. The goal was to have the selected search result automatically p ...

Proper usage of a method within another method in a Vue component

Currently, I am extracting GPS data from an image using exifjs. My objective is to convert the latitude and longitude variables into a decimal variable as illustrated below: <template lang="html"> <div class="upload-wrap"> <button cla ...

Switch up the vuetify scss variables within a designated component

I have integrated Vuetify with my Vue-cli project and have a variables.scss file located in the src/styles directory. I'm now looking to modify a specific Vuetify Scss variable within just one component without affecting others. For instance, I need ...

Detecting server errors in Nuxt.js to prevent page rendering crashes: A Vue guide

Unique Context This inquiry pertains to a previous question of mine, which can be found at this link: How to handle apollo client errors crashing page render in Nuxt?. However, I'm isolating the focus of this question solely on Nuxt (excluding apollo ...

`Customizing Switch Colors on Bootstrap: A Guide`

https://i.sstatic.net/alsdy.png I obtained the code from Get Bootstrap. <div class="form-check form-switch"> <input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked> & ...

Unable to locate the hidden element

Attempt to access the attribute of a shadow element resulted in encountering ElementNotVisibleException Element with CSS input[type='checkbox'] is not present on screen <checkbox _ngcontent-ebv-c14="" label="User Access" ng ...

Unable to implement a transition to adjust the scaling transformation

As I work on developing a website for a local business, my progress involves creating a menu bar. However, I am facing an issue with the transition I have applied using transform: scale(1.2) which does not seem to be working as intended. After spending h ...

Is there a way to add a stylesheet file to just one specific template?

I am facing an issue with including a Materialize.min.css link. Here is the code I have: <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css"> However, I only want to incl ...