Create a single button that controls checkboxes with the help of HTML, CSS, SCSS, and Bootstrap 4

Is there a way to use only HTML, CSS, SCSS, and Bootstrap 4 to make all checkboxes default to the "on" condition when a button is clicked? Typically, this functionality would be achieved with JavaScript, but due to constraints, I am exploring alternative solutions. If anyone has tackled this challenge before, please share your approach.

        <div class="welcome-left-btn">
         <label class="switch">
          <input type="checkbox" name="chk" checked>
          <span class="slider round"></span>
         </label>
        </div>
        
        <div class="welcome-left-btn">
         <label class="switch">
          <input type="checkbox" name="chk" checked>
          <span class="slider round"></span>
         </label>
        </div>

        <button id="defaultbtn" class="btn" type="submit">Defaultbtn</button>


        .switch {
         position: relative;
         display: inline-block;
         width: 40px;
         height: 23px;
         }
         .switch input {
         opacity: 0;
         width: 0;
         height: 0;
         }
         .slider {
         position: absolute;
         cursor: pointer;
         top: 0;
         left: 0;
         right: 0;
         bottom: 0;
         background-color: #ccc;
         -webkit-transition: .4s;
         transition: .4s;
         }

         .slider:before {
         position: absolute;
         content: "";
         height: 19px;
         width: 19px;
         left: 2px;
         bottom: 2px;
         background-color: white;
         -webkit-transition: .4s;
         transition: .4s;
         }

         input:checked + .slider {
         background-color: #3861fb;
         }

         input:focus + .slider {
         box-shadow: 0 0 1px #3861fb;
         }

         input:checked + .slider:before {
         -webkit-transform: translateX(17px);
         -ms-transform: translateX(17px);
         transform: translateX(17px);
         }
         .slider.round {
         border-radius: 25px;
         }

         .slider.round:before {
         border-radius: 50%;
         }

         
    

Answer №1

If you want to achieve a similar visual effect without using JavaScript, consider experimenting with the :target selector.

One workaround is to modify your HTML structure by avoiding the use of the "button" tag within the "a" tag and setting the class to "btn" if Bootstrap is being used.

.switch {
  position: relative;
  display: inline-block;
  width: 40px;
  height: 23px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}
/* CSS Code continued... */
<section id="visual_off">
  <div class="welcome-left-btn">
    <label class="switch">
            /* Additional HTML code goes here... */
        </label>
  </div>

  <a type="submit" href="#visual_on"><button>ON</button></a>
</section>
<section id="visual_on">
  <div class="welcome-left-btn">
    <label class="switch">
            /* Additional HTML code goes here... */
        </label>
  </div>

  <a type="submit" href="#visual_off"><button>OFF</button></a>
</section>

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

When the margin-left changes, SVG begins to flicker and shake in a marquee effect

I've been experimenting with a marquee effect using vanilla JS. The effect is working, but I'm encountering some shaking issues with SVG and images as they move. <div class="marquee"> <h1>Nepal <svg version="1.1&qu ...

What is the process of uploading a video and showcasing it on an HTML page?

I have successfully uploaded images and displayed them on an HTML page. Now, I am looking to do the same for videos. Here is my current code: $('#addPhotosBtn').click(function() { $(this).parents().find('#addPhotosInput').click(); }) ...

What is the best way to align product cards side by side instead of stacking them on separate lines?

I am currently working on a mock-up website for a school project. For this project, I am developing a products page where I intend to showcase the items available for sale. Although I managed to successfully copy a template from w3schools, my challenge lie ...

Detect when a user's mouse is hovering over an iframe and escape the iframe accordingly

When the iframe window is visible, there are no issues. However, if the iframe window is set to 0px X 0px in order to hide it while still loading, consider redirecting the iframe or not displaying it at all. In case something is loaded within an iframe or ...

Encountering numerous errors when attempting to incorporate lottie-web into my JavaScript file

I am in the process of creating a unique audio player for my website utilizing HTML, CSS, and JavaScript. I encountered some challenges while trying to get it to work effectively on Codepen as well as my text editor. The animations were not functioning pro ...

Text area capacity

Is there a limit to the maximum capacity of a textarea for accepting text? The HTML page functions correctly when the text is limited to around 130-140 words. However, if the text exceeds this limit, it causes the page to hang without any response. The tex ...

Using a `right: 0` value will not be effective when using absolute positioning

Currently, I am in the process of developing a website utilizing Bootstrap 3.3.6. After going through a tutorial on how to create a responsive banner slider, I found this resource helpful: http://www.jqueryscript.net/slider/Full-Width-Responsive-Carousel- ...

What are some ways to prompt electron to refresh its rendering with updated CSS?

I am currently in the process of developing my first Electron app on Windows 10, using Visual Studio Code and Git Bash as my primary tools. However, I have encountered a frustrating issue where my app has suddenly stopped updating based on css changes. Spe ...

Learn how to use CSS flexbox to easily vertically center items within a container with a full-height background

I'm having trouble with aligning text vertically centered within a box and making sure the background color stretches to full height. HTML: <div class="feature-content"> <div class="feature-visual"> <div class="embed-container"> ...

What is the best way to prevent a sticky element from scrolling along with the

I am looking to prevent a sticky div from taking up space on the website. It currently sticks to the desired position as expected. HTML <div v-if="isToastActive" class="toast"> <span class="message">{{ messa ...

Tips for accessing the HTML code of a TextBox in JavaScript while utilizing HTMLEditorExtender

Currently, I am utilizing the HTMLEditorExtender ajax tool on my website. The data is being saved in HTML format into the database and then retrieved within my project without any issues. However, there is a setback that I have encountered... When attemp ...

Implementation of Ionic Tabs with Hidden Back Button

Is there a way to hide the back button for a specific tab in my ionic app without affecting the rest of the page? I tried adding "hide-back-button="true" to the tab code, but it didn't work as expected. I want to achieve something like this: <ion ...

Is there a bug causing the Admin LTE content height issue? Looking for solutions to fix it

What's the reason behind the oversized footer? The footer appears normal when I resize or zoom the page, why is that? How can this be resolved? ...

Developing a system mode called "night mode"

I've decided to incorporate a dark mode feature into my Wordpress theme. Creating both dark and light modes was a breeze, but now I want to add a third mode that serves as the default for pages. This new mode will automatically switch between dark a ...

Ensuring the text is positioned centrally and at the bottom of a table cell

Is there a way to center and align text at the bottom of a cell in table? This is my desired layout: ...

Ways to implement CSS styling to every input element excluding inputs located within list items of a specific class

input.not(li.tagit-new >input) { background-color: lightgrey; } <ul id="tagAdministrator" style="width: 505px" class="tagit ui-widget ui-widget-content ui-corner-all"> <li class="tagit-new" style="box-shadow: none;"> <input ty ...

Tips for making text flow around an image

How can I achieve a design where the text wraps around an image positioned in the bottom right corner? [Image Removed by Owner] Your guidance on implementing this effect is greatly appreciated. Thank you. ...

Are CSS3 animations limited to working only with Webkit?

Trying to create a CSS3 animation featuring a puzzle piece on my website. Below is the code I'm using. Strangely, the animation only seems to be working on Safari and Chrome. Any tips on how to make it compatible with Firefox and Opera? Appreciate you ...

Putting an <input/> element inside a Material UI's <TextField/> component in ReactJS - a beginner's guide

I am attempting to style <TextField/> (http://www.material-ui.com/#/components/text-field) to resemble an <input/>. Here is what I have tried: <TextField hintText='Enter username' > <input className="form-control ...

Adjusting the height of an <iframe> element to match the full height of the document

My code snippet is: <iframe src="http://www.externalsite.com/" style=""/> I need to set the height of the iframe to match the total height (X pixels) of www.externalsite.com. Since X is unknown, how can I achieve this using CSS or alternative metho ...