What is the method to create a bold label for an input that is checked but not disabled using CSS?

I am working on a form that includes checkboxes and radio buttons. My goal is to make the selected options appear bold for better visual emphasis.

Following advice from this Stack Overflow thread, I have been using "label for" in HTML.

In my scenario, some of the radio button options can be disabled based on other form fields. When an option is disabled, I do not want it to appear in bold.

While I know how to achieve this using JavaScript, I am exploring possible ways to implement it through CSS.

Here is a snippet of the code I have put together:

:checked + label {
  font-weight: bold;
}
<input id="rad1" type="checkbox" name="rad" checked/><label for="rad1">Radio 1</label>
<input id="rad2" type="checkbox" name="rad"/><label for="rad2">Radio 2</label>
<input id="chkdis1" type="radio" name="chkdis" value="chkdis1" checked disabled><label for="chkdis1"> chkdis</label>
<input id="chkdis2" type="radio" name="chkdis" value="chkdis2" disabled><label for="chkdis2"> chkdis</label>

After experimenting with CSS, here is the solution I came up with:

:checked + label {
  font-weight: bold;
}

:disabled + label {
  font-weight: normal;
}
<input id="rad1" type="checkbox" name="rad" checked/><label for="rad1">Radio 1</label>
<input id="rad2" type="checkbox" name="rad"/><label for="rad2">Radio 2</label>
<input id="chkdis1" type="radio" name="chkdis" value="chkdis1" checked disabled><label for="chkdis1"> chkdis</label>
<input id="chkdis2" type="radio" name="chkdis" value="chkdis2" disabled><label for="chkdis2"> chkdis</label>

One drawback of my current solution is that if I switch the order by putting :disabled + label before :checked + label, the desired functionality no longer works. This leaves room for potential errors if someone were to inadvertently swap these lines in the code.

Do you have any suggestions for a more reliable approach?

Answer №1

Consider trying out the :not() selector in your CSS

:not(:checked)+label,
:checked:disabled+label {
  font-weight: normal;
}

:checked+label {
  font-weight: bold;
}
<input id="rad1" type="checkbox" name="rad" checked/><label for="rad1">Radio 1</label>
<input id="rad2" type="checkbox" name="rad" /><label for="rad2">Radio 2</label>
<input id="chkdis1" type="radio" name="chkdis" value="chkdis1" checked disabled><label for="chkdis1"> chkdis</label>
<input id="chkdis2" type="radio" name="chkdis" value="chkdis2" disabled><label for="chkdis2"> chkdis</label>

Answer №2

Simply assign a class to your label. By the way, do you know what the 'C' signifies in CSS lingo?

:disabled + label {
  font-weight: normal;
}

:checked + .xyz{
  font-weight: bold;
}
<input id="rad1"  type="checkbox" name="rad" checked/><label class='xyz' for="rad1">Radio 1</label>
<input id="rad2"  type="checkbox" name="rad"/><label class='xyz' for="rad2">Radio 2</label>
<input id="chkdis1" type="radio" name="chkdis" value="chkdis1" checked disabled><label for="chkdis1"> chkdis</label>
<input id="chkdis2" type="radio" name="chkdis" value="chkdis2" disabled><label for="chkdis2"> chkdis</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

Conflicting media queries

Attempting to resolve the issue of site content overlapping with the background slider plugin on a WordPress website, I have encountered conflicting media queries for different devices. While it may work perfectly on mobile devices, it can create problems ...

Ways to conceal sidebar after user logs out and reveal sidebar upon user's successful login?

Currently, I am utilizing the Dashgum bootstrap template obtained from Gridgum for a specific assignment. The task involves displaying the side navbar for logged-in users persistently, even after they have closed the site, and hiding the side navbar comple ...

How can you customize the default select box in a Django template using a passed parameter?

I have a situation where I am passing a dictionary to a Django template and need to set a default value for a select box. The context is that this template serves as an 'edit product' page for a previous 'add product' form. When the use ...

The Navbar is not displaying the React Bootstrap 4 CSS module as intended

Having some trouble changing the color of my navbar. Is there a step I might be missing? Here's the component I'm trying to render: import React from 'react'; import { Nav, Navbar } from 'react-bootstrap'; import styles from ...

Display MQTT information on a Django template

I've been utilizing paho-MQTT successfully to receive messages. However, I'm facing a challenge in displaying the received data in a template. Below is an excerpt of my code: import paho.mqtt.client as mqtt import json def on_connect(client, use ...

Place the divs over the background image by using relative positioning

I am working on a project where I have a full-width div with a background image containing people. I want to display a tooltip when hovering over each person, but I am facing challenges in aligning the divs properly. Image maps with % widths do not seem t ...

Executing a PHP function through an HTML button click while maintaining the current page state without

I have a php script: <?php $species = $_GET['species'] ...//do things to prepare the query $result = //result of the query. if (isset($_GET['download'])){ getCSV(); } function getCSV(){ if ($result->num_rows > 0){ ...

Tips for adjusting the color of linked text within a jQuery accordion

Is there a way to change the color of a link within a jQuery accordion? css .x { color: red; } html <div id="test"> <h3><a href="#">One</a></h3> <div>111 </div> <h3><a href="#">T ...

List of recommended countries with their respective flags and descriptive text

I am currently working on creating a country suggestion list that includes flags next to the selected result. While I have been successful in generating the suggestion box based on the country names, I am looking to enhance it by displaying the respective ...

Assistance needed with CSS - making an element occupy the entire remaining vertical space

Although I'm quite confident in my CSS/XHTML abilities, this particular issue has me stumped. You can view the problem here: - (please note that the website is still under development, most features are not functional, and it's subject to frequ ...

When the HTML and PHP code keeps running, the JavaScript text on the page updates itself

I was experimenting with threading in different languages like Java, PHP, and JavaScript. I am aware that JavaScript is single-threaded and PHP lacks robust threading capabilities. Below is the code snippet I have been working on: <body> <p id= ...

Hide overflow for images with unique shapes

Here are the two image layers: https://i.sstatic.net/lUUQC.jpg One shows a hand holding a phone, and the other is a black hole. This is how you can achieve it using HTML markup: <div class="wrapper"> <img class="wrapper__image" src="path/to/ ...

Can pagination numbers in Jquery DataTable be configured based on the total records returned by the server and the number of records chosen by the user?

I am currently diving into the world of DataTable.js, a jQuery plugin, and working hard to articulate my questions effectively. For my specific needs, I need to retrieve only 10 records initially whenever an AJAX request is made, even if there are 100 rec ...

Creating round corners for images with jQuery

To achieve rounded corners on multiple blocks using a single image above, I am opting for the use of images instead of plugins for maximum compatibility. This involves creating wrappers around the blocks to apply the corner images: <div class="wrapper- ...

Dynamic Navigation - Apply a class if the menu item has subcategories

After successfully creating a navigation menu for desktop, I am now faced with the challenge of resizing the nav to fit mobile and tablet screens. Specifically, I want to add a class of "dropdown" to the top level list item if it contains children on small ...

Servlet fails to update and displays a blank screen

I am relatively new to the world of programming and have encountered an issue with my Java web application. Despite successfully adding and deleting products from the MySQL database, I am facing difficulties updating product information. When I attempt to ...

Dimensions in Material UI

As I embark on my journey with Material UI components for React, I am facing the challenge of integrating pre-styled components into my project. I have noticed that some components appear too large in my layout due to excessive padding and margins. Curren ...

Switch the background image in a table data cell using ngIf in Angular

Can you help me with this: I have a td in a table with a background image applied <td background="assets/images/crew/sign.jpg" style="background-repeat: no-repeat; background-size: contain; background-position: center center"> W ...

The fundamentals of CSS breadcrumbs and their simplicity

I tried to follow a tutorial on creating an Apple-inspired breadcrumb trail. I referenced the tutorial here: Even after examining the updated example and downloading the source code from this link: I'm confused about how only the HTML and CSS can id ...

Extract the HTML field value from an AJAX drop-down selection, rather than relying on user input

I have a field that allows users to select from a dropdown list using the following code: $(function() { $('.serial_number').live('focus.autocomplete', function() { $(this).autocomplete("/admi ...