Is it possible to create a CSS checkbox without using an ID or "for" attribute

Is it possible to create a CSS checkbox with label without using id and for attributes?

I have tried the following method, but it doesn't seem to work. I know that I can create a CSS checkbox with id and for attributes like this:

<div class="checkbox">
    <input id="f" type="checkbox" name="hi" class="checkbox" />
    <label for="f">Hello</label>
</div>

However, I want to achieve this instead:

<label class="checkbox">
<input type="checkbox" value="1" name="files" >
</label>

So, I am using this CSS (SCSS actually):

label{
    input[type=checkbox]{
        display: none;
    }
}

.checkbox{
    position: relative;
    display: inline-block;
    margin-right: 7px;
    cursor: pointer;
    vertical-align: middle;
    &:after{
        content: ' ';
        position: absolute;
        border: 2px solid $black;
        border-radius: 3px;
        width: 20px;
        height: 20px;
        top: -5px;
        left: -2px;
        width: 14px;
        height: 14px;
    }
    &:checked{
        font-family: 'FontAwesome';
        font-size: 2em;
        content: "\f00c";
        color: $blue;
        border-radius: 3px;
    }
}
/* It doesn't work */
.checkbox:after > input[type=checkbox]:checked {
        font-family: 'FontAwesome';
        font-size: 2em;
        content: "\f00c";
        color: $blue;
        border-radius: 3px;
}

Unfortunately, the checked checkbox is not showing up. Any suggestions? Please help :)

Answer №1

To properly format your Label tag, you need to include a span element.

.checkbox:after > input[type=checkbox]:checked

The current line doesn't make sense as it needs to be the opposite. It should be changed to:

input[type=checkbox]:checked ~ .checkbox:after

Using the ~ selector means affecting the next element when something is checked.

To solve this issue, adding a span will complete the solution. Here is an updated version:

Working Solution

$blue : blue;
$black : black;

label{
    input[type=checkbox]{
        opacity: 0;
    }
}

.checkbox{
    position: relative;
    display: inline-block;
    margin-right: 7px;
    cursor: pointer;
    vertical-align: middle;
    &:after{
        content: ' ';
        position: absolute;
        border: 2px solid $black;
        border-radius: 3px;
        width: 20px;
        height: 20px;
        top: -5px;
        left: -2px;
        width: 14px;
        height: 14px;
    }
    &:checked{
        font-family: 'FontAwesome';
        font-size: 2em;
        content: "\f00c";
        color: $blue;
        border-radius: 3px;
    }
}
/* This part was corrected */
input[type=checkbox]:checked ~ .checkbox:after{
        font-family: 'FontAwesome';
        font-size: 1em;
        content: "\f00c";
        color: $blue;
        border-radius: 3px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet"/>
<label>
  <input type="checkbox" value="1" name="files" >
  <span class="checkbox"></span>
</label>

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

Encountered an error when attempting to use 'appendChild' on 'Node': the first parameter is not the correct type. It was able to work with some elements, but not with others

I'm currently working on a script that utilizes fetch to retrieve an external HTML file. The goal is to perform some operations to create two HTMLCollections and then iterate over them to display a div containing one element from each collection. Here ...

JQuery function within the "onclick" event

$('.more').click(function showFriendInfo(id) { $('#MoreFriendInfo'+id).toggle(); }); I created this function called showFriendInfo(id), but how can I properly link it in HTML/PHP to obtain the id? I attempted the following ...

Ensure that only a single onmouseover event is triggered when hovering over multiple elements

I want to create a simple code snippet like the one below: <span onmouseover="alert('hi')">Hello, <span onmouseover="alert('hello')">this</span> is a test</span> However, I need to ensure that when hovering ove ...

CSS rule: The behavior of my specified property is not being implemented

I have inserted an image directly into my HTML code, which serves as a small profile picture. I am attempting to position this image in the top right corner of my website, but no matter what CSS property I apply, the image refuses to budge. body { fon ...

JavaScript - Clear the localStorage when closing the final tab of a website

Currently, I am working with AngularJS and utilizing $window.localStorage to store the username of a logged-in user. Since localStorage needs to be explicitly deleted, I have implemented an event listener for $(window).on('unload', function(){}) ...

HTML elements are replaced by codes on the screen

After setting up an ajax request to run at regular intervals, I encountered a problem when trying to navigate back. Instead of displaying the expected HTML elements, the browser showed all of my HTML code. <script> window.setInterval(function () ...

I need to display 5 columns in a parent component, each with its own unique icon. How can I conditionally render them in a React component?

Creating a parent component to reuse multiple times can be very useful. In my case, I have included 5 different icons in each instance of the component, all taken from hero icons. I have set up an object with unique ids for each child component, but I am ...

Customizing CSS for Internet Explorer browsers

I am looking to target specific CSS styles for Internet Explorer only, without affecting other browsers. Initially, I tried using conditional comments: <!--[if lt IE 7 ]><html class="ie6 ie"> <![endif]--> <!--[if IE 7 ]><html cl ...

Guide on adjusting the size of an image based on either its width or height dimensions

Currently, I am retrieving product images from an API but unfortunately, they do not have consistent dimensions. My goal is to display these images within a 250x250 div. In some cases, the image is in portrait orientation and should be scaled based on its ...

When you zoom in or out, HTML5 elements styled with CSS will dynamically adjust

Hey everyone, I have a question about a problem I'm facing with my HTML and CSS page. The issue is that when I zoom in, the "Section" part moves underneath the nav bar, and when I zoom out, the footer moves next to the section part... Below is a sni ...

Adjusting the contenteditable feature to place the caret on a specific child node

I am experiencing some challenges when attempting to position the cursor after an <i> tag within a contenteditable element. Currently, this is what I have: <p contenteditable="true"><i>H</i><i>e</i><i>l</i> ...

White border appears when hovering over MUI TextField

I've been troubleshooting this issue for what seems like an eternity. I've combed through Chrome's inspect tool, searching for any hover styles on the fieldset element, but to no avail. Here's my dilemma... I simply want a basic outline ...

Can we dynamically adjust font size based on the width of a div using CSS?

My div is set to 70% width and I have a specific goal in mind: To fill that div with text To adjust the font size so that it takes up the entire width of the div https://i.stack.imgur.com/goVuj.png Would it be possible to achieve this using only CSS? B ...

How can XPath be used to target a specific parent element?

After reviewing the material, I am led to believe that it may not be feasible; however, I will present the following scenario just in case: Consider the following oversimplified HTML structure (derived from a CMS-generated HTML, hence limited control) < ...

When working with ajax, you can easily refer to elements by using the $(this)

I am having trouble using the $(this) selector in the ajax success function to target the element with the class .selectclass that was double-clicked before. Any suggestions on how to make this work within the ajax success function? Here's the jQuery ...

Achieving vertical center alignment for the label and btn-group in Bootstrap

Is it possible to align other elements with the Bootstrap btn-group? <div class="btn-toolbar text-center"> <div class="pull-left"> <span class="glyphicon glyphicon-envelope " style="font-size:1.5em;"></span> < ...

Using a Do/While loop in combination with the setTimeout function to create an infinite loop

Having trouble with this script. The opacity transition works fine, but there seems to be an issue with the loop causing it to run indefinitely. My goal is to change the z-index once the opacity reaches 0. HTML <div class="ribbon_services_body" id="ri ...

Determine the sequence of a div based on its class

Here is the code snippet I am working with: <div class="test"></div> <div class="test"></div> <div class="test"></div> <input type="button" class="button" value="get number"> <div class="test"></div> & ...

Incorporate the list seamlessly into the remaining content of the page

I am currently developing a Vue component that utilizes an API call to search for a list of cities based on user input. My challenge is ensuring that the displayed list does not overlap with the header component, specifically overlapping the image of the s ...

What is the best way to ensure my arrow text remains properly positioned when using fullpage.js?

Seeking guidance from the web development community. I am currently working on a client's website that utilizes fullpage.js and encountering a persistent issue. On slide1 of the website, I am struggling to display the correct text next to the arrows. ...