Ways to identify and differentiate various checkboxes using HTML and CSS

I am currently working on implementing a thumbs-up and thumbs-down feature, where clicking on the thumb-up icon will change the color to green, and clicking on the thumb-down icon will change it to red. However, I am facing some difficulties understanding the 'checkbox' function in my CSS file.

At the moment, the code I have seems to turn both thumbs red when clicked. I suspect that this is due to conflicting inputs at the end of my CSS file, where the red one is given priority because it comes last. My challenge lies in distinguishing between the two inputs.

body {}

.main_box {
  background-color: #320082;
  height: 500px;
  width: 250px;
  border: 2px solid #000000;
  border-radius: 20px;
}

/* Other CSS styles here */

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <title>Thumbs Feedback</title>
  <link rel="stylesheet" type="text/css" href="styles.css" />
</head>

<body>
  <div class="main_box">
    <h1>Thumbs Feedback</h1>
  </div>
  <div class="greencheckbox">
    <input type="checkbox">
    <span class="thumb">👍</span>
  </div>
  <div class="redcheckbox">
    <input type="checkbox">
    <span class="thumb">👎</span>
  </div>
</body>

</html>

Answer №1

It seems like you've come across a style cascade issue. To fix this, simply ensure that the selectors for the parent elements are specific to each input.

For example:

.greencheckbox input[type=checkbox]:checked {
  background-color: rgb(0, 160, 0);
}

.redcheckbox input[type=checkbox]:checked {
  background-color: rgb(160, 0, 0);
}

body {}

.main_box {
  background-color: #320082;
  height: 500px;
  width: 250px;
  border: 2px solid #000000;
  border-radius: 20px;
}

h1 {
  text-align: center;
  font-size: 40px;
  color: #ff4da6;
}

h2 {
  font-size: 16px;
  text-align: center;
  color: aliceblue;
}

.glow {
  color: #fff;
  text-align: center;
  animation: glow 1s ease-in-out infinite alternate;
}

.greencheckbox {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

redcheckbox {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

input[type=checkbox] {
  appearance: none;
  height: 100px;
  width: 100px;
  background-color: rgb(175, 175, 175);
  border-radius: 50%;
  cursor: pointer;
}

thumb {
  position: absolute;
  font-size: 35px;
  color: white;
}

.greencheckbox input[type=checkbox]:checked {
  background-color: rgb(0, 160, 0);
}

.redcheckbox input[type=checkbox]:checked {
  background-color: rgb(160, 0, 0);
}

@-webkit-keyframes glow {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #e60073, 0 0 40px #e60073, 0 0 50px #e60073, 0 0 60px #e60073, 0 0 70px #e60073;
  }
  to {
    text-shadow: 0 0 20px #fff, 0 0 30px #ff4da6, 0 0 40px #ff4da6, 0 0 50px #ff4da6, 0 0 60px #ff4da6, 0 0 70px #ff4da6, 0 0 80px #ff4da6;
  }
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <script type="module" src="https://unpkg.com/ionicons/ionicons.esm.js"></script>
  <script nomodule src="https://unpkg.com/ionicons/ionicons.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" type="text/css" href="MainBox.css" />
  <title>checkbox</title>
</head>

<body>
  <div class="main_box">
    <h1 class="glow">Wolfies</h1>

    <h2>560 Bungalow St</h2>
  </div>
  <div class="greencheckbox">
    <input type="checkbox">
    <ion-icon name="thumbs-up" class="thumb"></ion-icon>
  </div>
  <div class="redcheckbox">
    <input type="checkbox">
    <ion-icon name="thumbs-down" class="thumb"></ion-icon>
  </div>
</body>

</html>

BETTER SOLUTION

Another approach is to assign a class to each input to differentiate them. This way, even if you relocate the input within a different parent element, it will still maintain its checked state styles:

.greencheckbox {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.redcheckbox {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

input[type=checkbox] {
  appearance: none;
  height: 100px;
  width: 100px;
  background-color: rgb(175, 175, 175);
  border-radius: 50%;
  cursor: pointer;
}

.thumb {
  position: absolute;
  font-size: 35px;
  color: white;
}

input[type=checkbox].green:checked {
  background-color: rgb(0, 160, 0);
}

input[type=checkbox].red:checked {
  background-color: rgb(160, 0, 0);
}
<script type="module" src="https://unpkg.com/ionicons/ionicons.esm.js"></script>
<script nomodule src="https://unpkg.com/ionicons/ionicons.js"></script>

<div class="greencheckbox">
  <input type="checkbox" class="green thumbs-up">
  <ion-icon name="thumbs-up" class="thumb"></ion-icon>
</div>
<div class="redcheckbox">
  <input type="checkbox" class="red thumbs-up">
  <ion-icon name="thumbs-down" class="thumb"></ion-icon>
</div>

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

I'm curious about how I can apply @media queries given that certain phones possess higher resolution than computers

There seems to be a common recommendation to utilize @media queries for adjusting CSS based on whether the user is on mobile or not. However, I'm a bit confused because my phone has a width of 1440p while my computer has 1920p. Should I consider apply ...

Having difficulty uploading files to my website [PHP]

I'm having trouble with a PHP tutorial I followed for uploading an image. Despite my best efforts, the upload always fails and I can't figure out why. I've rewritten the code multiple times but the problem persists. The goal of this code is ...

Utilizing multiple address parameters within a single-page application

Apologies for the lengthy post. I have encountered an issue with my code after running it through a bot that I can't seem to resolve. In my code, I aim to create functionality where you can visit the address #two, followed by two separate parameters ...

Display or conceal elements based on the screen size using Bootstrap 3 classes

While this may seem straightforward for some, the reality is quite different. I have two images that need to be displayed based on screen size. In my HTML code, I used classes like hidden-md, hidden-sm, and hidden-xs assuming they would only show on larg ...

Achieve dynamic styling and adjust window size using window.open

My mind is exhausted after days of trying: function prtDiv( o ) { var css = 'body {font:normal 10px Arial;}' ; var wo = window.open('','print','width=600,height=600,resizable=yes'); wo.document.write( '<he ...

Issue with the hamburger menu dropdown color not being applied properly

Greetings to all who are taking the time to review this query! Everything seemed to be going well until I refreshed my code after making numerous changes, and then this issue arose: Dropdown menu lacking color: https://i.sstatic.net/3lz5a.png The color ...

Incorporating React's dynamic DIV layout algorithm to generate a nested box view design

My goal is to showcase a data model representation on a webpage, depicting objects, attributes, and child objects in a parent-child hierarchy. I had the idea of developing a versatile React component that can store a single data object while also allowing ...

Switching up the Label Colors in Chart.JS

It's been a while since my last question, so please bear with me if I'm not following the rules. I've attached my current code and a reference image of the chart. I am completely new to working with ChartJS. The situation is a bit unique: t ...

The alignment of the website design is off

I'm in the process of creating a custom template for my website and everything was going smoothly until I encountered an issue with the news bar placement. Instead of appearing below the navbar and welcome message bar, it's showing up above the n ...

Ways to position a DIV on the right side of the screen using percentage margin to adjust its distance from the edge as the browser is resized

Allow me to provide a clearer explanation here: Imagine an element placed on a webpage with a margin of 10% from the left side. When the size of the page is adjusted, the margin on the left decreases accordingly - for example, 10% from 1400px - 140px, and ...

What is the method for sending data through $_POST using a plain text HTML link (otherwise known as an href URL)?

I'm in the process of designing a basic webpage where a user lands on it, enters their username and password, and after clicking a button (which I'd prefer to turn into a clickable URL text, but I'm having trouble figuring out how to do that ...

Adjust the image's width to match the width of the browser

I am seeking assistance on how to resize an image to fit the width of the browser. Specifically, the image I want to resize is my header image and I want it to span the entire screen width. Additionally, I need to overlay a div on top of the image. Curren ...

What is the best way to utilize justify-content or align-self to perfectly center this?

Despite my efforts to center it, I'm struggling to do so. I've experimented with justify-content and align-items for each row and col class, but unfortunately, nothing seems to be effective. In the breakpoints (md, lg), I really need the 4 lorem ...

Learning how to utilize various types of buttons in C# and ASP.NET with this

I was following this tutorial: I like everything the same except I want to make the following change: <section> <a rel="external" href="#button" id="button">&#xF011;</a> <span></span> </section> ...

What type of technology is typically utilized in creating functional platforms such as goDaddy, wix, and other web design websites?

I am currently working on a web development project that aims to allow users to edit webpages without needing any coding knowledge. Users with authorization will be able to modify not only the text but also various HTML tags on the page, similar to platfor ...

How do you determine the dimensions of a background image?

My div is a perfect square, measuring 200 pixels by 200 pixels. The background image I've chosen to use is larger at 500 pixels by 500 pixels. I'm searching for a method to ensure that the entire background image fits neatly within the confines ...

The combination of the card hover effect and the bootstrap modal creates complications

Hey there! I'm in the midst of crafting a webpage using Bootstrap and EJS. My aim is to craft a modal window that pops up when a specific button is clicked to display extra information, and upon clicking an X or "close" button, the modal should disapp ...

Implementing a clickable search icon within a form input field using CSS: a guide

I have added a search icon to the form input field. Check it out here: https://i.sstatic.net/pnv6k.jpg #searchform { position: relative; } #searchform:before { content: "\f118"; font-family: "pharma"; right: 0px; position: absolute; ...

Adding additional images to the left side of the slide

My goal is to display multiple images in two rows. The alignment works well for up to 9 images, but beyond that, the additional images appear on a third row instead of staying within the two rows. I want to ensure they are contained within 2 rows only. How ...

Expanding the input focus to include the icon, allowing it to be clicked

Having trouble with my date picker component (v-date-picker) where I can't seem to get the icon, a Font Awesome Icon separate from the date-picker, to open the calendar on focus when clicked. I've attempted some solutions mentioned in this resour ...