How to make checkbox visible when hover or select the element?

The problem here is how to make a checkbox visible only when:

  1. It is hovered over
  2. Selected

Approach:
The checkbox shouldn’t be visible when not selected. It only becomes visible when hovered over again.
The solution to the problem is simple. We can use the opacity property of the checkbox element and manipulate that. Just change the opacity value of the checkbox to 1 when the checkbox element is hovered over or when it is selected, using CSS. The default value of opacity should be 0.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Checkbox</title>
    <style>
        #box{
            margin:50px;
            width:50px;
            height:50px;
            border:2px solid black;
            padding:10px;
        }
  
        .cardcheckbox {
        opacity: 0;
          
        }
  
        .cardcheckbox:hover,
        .cardcheckbox:checked {
            opacity: 1;
        }
    </style>
</head>
<body>
    <div id="box">
        <div>
            <input type="checkbox"
                   class="cardcheckbox" 
                   align="right"/>
        </div>
    </div>
</body>
</html>

chevron_right


Output:




My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.