Switch out the checkbox for a span for a different approach

Working with Angular 9 reactive forms, I am currently using the following form setup:

 
    <form [formGroup]="form" (ngSubmit)="onSubmit()">
      <label for="active">Active</label>
      <input id="active" type="checkbox" formControlName="active"> 
      <button class="action" type="submit">Send</button>
    </form>

However, rather than a traditional checkbox, I want to implement a clickable rectangle that functions like a checkbox:

.active { padding: 20px; display: inline-block; }
.checked { border: 1px solid green; color: green; }
<span class="active checked">Active</span>

What steps do I need to take to achieve this?

Answer №1

Styling Checkboxes with CSS
input[type="checkbox"] {
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
}

label {
  border: 1px solid transparent;
  display: inline-block;
  padding: 20px;
  cursor: pointer;
}

input:checked+label {
  border-color: green;
  color: green;
}
<div>
  <input id="my-input" type="checkbox">
  <label for="my-input">Active</label>
</div>

Answer №2

Is this what you were looking for?

.control-me::after {
    content: "😃";
    font-size: 100px;
}
#toggle:checked ~ .control-me::after {
    content: "😩";
}

label {
    background: #A5D6A7;
    padding: 0.5rem 1rem;
    border-radius: 0.5rem;
}

.visually-hidden {
    position: absolute;
    left: -100vw;
    
    /* You could also overlay the checkbox on top of the label and set opacity to zero, which might be better for some touch devices */
}

body {
    height: 100vh;
    margin: 0;
    display: grid;
    place-items: center;
    text-align: center;
    font: 900 24px/1.4 -system-ui, sans-serif;
}
<div>
    <label for="toggle">Toggle</label>
    
    <input type="checkbox" id="toggle" class="visually-hidden">

    <div class="control-me"></div>
</div>

Feel free to use or modify this code, maybe make the "Active" button turn green. If you need any assistance with your code, feel free to reach out to me via email at: [email protected]

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

Achieving uniform row height in CSS Grid Layout

How do I ensure all footers with a green background have the same height, while also keeping the content height consistent? Current result: https://i.sstatic.net/SrgFq.png Desired output: https://i.sstatic.net/4cr25.png CodePen: https://codepen.io/yas ...

Fill in the text field according to the option selected from the dropdown menu

I have a function in my JavaScript code that is supposed to fill a textbox based on the selection made in a drop-down menu. Here is an example with just one option out of eight: <script> function UpdateQuantity(){ if (document.getElementByID("Raff ...

When using Firefox, the -moz-transform:scale property allows you to scale the image without impacting its original dimensions

While resizing icons on a sprite sheet by scaling the image with CSS works fine in Chrome, Firefox seems to scale the graphic correctly but it still takes up the same space as the unscaled version. Even though I found -moz-focus-inner which removed some p ...

Avoid using CSS browser prefixes when selecting plugins or addons

I tried searching on Google for a solution, but I couldn't find anything! Is there a trick or library available to avoid repeating CSS extensions for browsers? Instead of using -webkit-, -moz-, and -o- prefixes? -webkit-animation: NAME-YOUR-ANIMATI ...

Modify the `<link>` tag using the `onclick` function in JavaScript

I want to switch up the site's style with just a click on an icon <head> <meta charset="utf-8"> <link rel="stylesheet" href="styled.css" id="styles"> </head> Everytime I try to tackle th ...

Embedding a CSS file into a PHP class

I've encountered a little issue that I can't seem to solve on my own. I've put together a "webengine" using multiple classes. The primary class responsible for obtaining a theme's internals is called "Logic". This Logic class contain ...

Guide to setting up the primary navigation in the WordPress administration panel

I recently started using WordPress and I'm struggling with changing the main menu href category. I've tried looking for the header in the FTP files, but I still can't figure out how to do it. I've read through all of the WordPress help ...

Encountering a CORS error after deploying an Angular and Spring Boot application on Heroku

Currently facing challenges with an app due to CORS issues. It functions perfectly locally, but once deployed, an error occurs. Access to XMLHttpRequest at 'https://portafolioback.herokuapp.com/portafolio/version1/post/all' from the origin &apos ...

What could be causing the hover effect to not work on the cards in my Svelte component? (HTML+CSS)

Greetings everyone! This is my debut post on this platform, and I'm in urgent need of assistance as I am relatively new to the world of programming. I've been trying tirelessly to implement a hover effect on these cards, but for some reason, it ...

Best Practices for Architecture and Routing in Angular 8 (suggestions)

As a newcomer to Angular, I find myself grappling with the recommended architecture for my project. In an effort to streamline my inquiry, I will structure it in the form of choosing between options A or B and exploring how to implement that choice. Specif ...

What could be causing the absence of the margin in my code?

I am currently working on a website layout where I want to display an image with some information below it, and the information should have a colored background with some margin around it. However, when I try to view the code I have written, the margin i ...

The default behavior of Angular-Keycloak does not include automatically attaching the bearer token to my http requests

I'm currently working on integrating keycloak-angular into my project, but I'm facing an issue with setting the bearer token as the default for my HTTP requests. "keycloak-angular": "9.1.0" "keycloak-js": "16.0 ...

Issue with modal box not displaying properly when hidden div is within a for loop

In my project, I have a MySQL select statement that retrieves all the results to be displayed in an unordered list (<ul>) using a for loop. This functionality works perfectly. However, I wanted to add a "click more" link at the bottom of each result ...

What is the best way to create a navigation link in PHP along with forms?

If the login credentials are correct, I will be directed to the member page. If not, I should remain on the same page. I am struggling with how to create a link to another page. I have come across some suggestions involving headers, but I am still unsure o ...

Maintaining the order of elements in Angular's insertion process

I am currently facing an issue with my HTML fragment that iterates over a key and value collection. Everything works perfectly when I insert values into an object and iterate through it using the HTML fragment. However, in order to maintain a specific key ...

Achieving the perfect button using a combination of material-ui and styled-components

Utilizing the ToggleButton and ToggleButtonGroup components from material-ui, starting with material-ui's gatsby template. To prevent common material-ui errors with production builds, I am attempting to incorporate styled-components as well. The foll ...

Is requesting transclusion in an Angular directive necessary?

An issue has cropped up below and I'm struggling to figure out the reason behind it. Any suggestions? html, <button ng-click="loadForm()">Load Directive Form</button> <div data-my-form></div> angular, app.directive(&apos ...

Guidelines for toggling text visibility based on a randomly generated number with if statements in vanilla JavaScript

I am currently attempting to showcase some intricate text alongside an image once a button is clicked. At the moment, I have successfully implemented the functionality to display a random image when the button is clicked. However, I am encountering diffic ...

A CSS rule to display a nested list on the left-hand side

I created a menu that you can view here. When hovering over "tanfa demo example," the sublist appears on the right side. The issue I'm facing is that my menu is positioned all the way to the right, which means the sublist also appears on the extreme ...

Angular and Jest combo has encountered an issue resolving all parameters for the AppComponent. What could be causing this error?

I am currently working within a Typescript Monorepo and I wish to integrate an Angular 8 frontend along with Jest testing into the Monorepo. However, I am facing some challenges. The tools I am using are: Angular CLI: 8.3.5 My Approach I plan to use ...