What is the best way to emphasize the selected color?

I am having trouble with setting the focus and active properties for an input element. Even after selecting a color, the highlight does not appear. However, the hover property is working fine.

Here is the code I have used to implement the highlight property for colors:

.highlightC:focus,
.highlightC:focus-visible,
.highlightC:visited {
  border: 2px solid orange!important;
}

.highlightC:hover,
.highlightC:active {
  border: 2px solid white;
}

.highlightC {
  height: 20px;
  width: 20px;
  border-radius: 50%;
  display: inline-block;
  border: 2px solid transparent;
}

.color1 {
  background-color: red;
}

.color2 {
  background-color: green;
}

.color3 {
  background-color: blue;
}
<div class="text-left mt-2">
  <p class="tpColorsHeading ">Available colors</p>
  <input type="radio" id="1" name="color" value="1" class="">
  <label for="1" class="dot color1 highlightC"></label>
  <input type="radio" id="2" name="color" value="2" class="">
  <label for="2" class="dot color2 highlightC"></label>
  <input type="radio" id="3" name="color" value="3" class="">
  <label for="3" class="dot color3 highlightC"></label>
</div>

https://i.sstatic.net/7KUSX.jpg

Answer №1

It seems like the issue lies in checking the label for :active status, which only works for elements like links.

I have made some changes in the code and commented where those changes are.

For further explanation on why you need to access it this way and why your previous attempt didn't yield any results, please refer to these helpful resources:

  1. How to style radio buttons

  2. Operators to affect other elements (the + operator in my example)

/*active is only working for links, not label elements*/
.highlightC:hover {
  border: 2px solid white;
}
/*you can style the border by checking the status of the input, + as an oeprator affects the next following label after clicking any radio button in this case, so it only works if the label comes after the radio-button in your dom*/
.text-left input[type="radio"]:checked+label {
   border: 2px solid white;
}

/*what are you trying to achieve with this? Visited also only works for links*/
.highlightC:focus,
.highlightC:focus-visible,
.highlightC:visited {
  border: 2px solid orange!important;
}
.highlightC {
  height: 20px;
  width: 20px;
  border-radius: 50%;
  display: inline-block;
  border: 2px solid transparent;
}

.color1 {
  background-color: red;
}

.color2 {
  background-color: green;
}

.color3 {
  background-color: blue;
}
<div class="text-left mt-2">
  <p class="tpColorsHeading ">Available colors</p>
  <input type="radio" id="1" name="color" value="1" class="">
  <label for="1" class="dot color1 highlightC"></label>
  <input type="radio" id="2" name="color" value="2" class="">
  <label for="2" class="dot color2 highlightC"></label>
  <input type="radio" id="3" name="color" value="3" class="">
  <label for="3" class="dot color3 highlightC"></label>
</div>

Answer №2

To achieve this effect, consider utilizing the :checked pseudo class selector along with the + adjacent sibling combinator:

.color-input {
  display: none;
}

.color-input:checked + .color-label {
  border-color: orange;
}

.color-label {
  height: 20px;
  width: 20px;
  border-radius: 50%;
  display: inline-block;
  border: 2px solid transparent;
}

.color-label--red {
  background-color: red;
}

.color-label--green {
  background-color: green;
}

.color-label--blue {
  background-color: blue;
}
<fieldset>
  <legend>Available colors</legend>

  <input type="radio" id="1" name="color" value="1" class="color-input">
  <label for="1" class="color-label color-label--red"></label>
  
  <input type="radio" id="2" name="color" value="2" class="color-input">
  <label for="2" class="color-label color-label--green"></label>
  
  <input type="radio" id="3" name="color" value="3" class="color-input">
  <label for="3" class="color-label color-label--blue"></label>
</fieldset>

Answer №3

If you're looking to style a checked radio button using CSS, here's how you can do it:

Check out the solution below:

input[type="radio"] {
  display: none;
}

.highlightC {
  height: 20px;
  width: 20px;
  border-radius: 50%;
  display: inline-block;
  margin: 3px;
  box-shadow: 0 0 0 2px transparent;
}

input[type="radio"]:checked+.highlightC {
  box-shadow: 0 0 0 2px darkorange;
}

.color1 {
  background-color: red;
}

.color2 {
  background-color: green;
}

.color3 {
  background-color: blue;
}
<div class="text-left mt-2">
  <p class="tpColorsHeading ">Available colors</p>
  <input type="radio" id="1" name="color" value="1" class="">
  <label for="1" class="dot color1 highlightC"></label>
  <input type="radio" id="2" name="color" value="2" class="">
  <label for="2" class="dot color2 highlightC"></label>
  <input type="radio" id="3" name="color" value="3" class="">
  <label for="3" class="dot color3 highlightC"></label>
</div>

Answer №4

To apply the highlight effect, make sure to include the css class name "highlightC" within the class attribute like so: class="highlightC"

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

creating a div element with a height that matches its content dimensions

I'm having trouble creating a navigation bar because the outer div isn't matching the height of the unordered list inside it. I attempted using display:inline-block as well, but it didn't solve the issue. Here is the HTML: http://jsfiddle ...

What are some ways to accomplish this task without relying on a photo editing tool?

Is it possible to swap the image link: https://i.sstatic.net/Wa4mo.jpg with this new link: https://i.sstatic.net/hqVuQ.jpg without having to use a photo editor? Below is the CSS and HTML code: I was unable to upload the logo due to the restrictio ...

Error message displayed on PHP web page

I'm working on a page that will display "no query string" if $_REQUEST is null, and if not, it will show a page with the content saying "query string". Additionally, it should output the line "The query string is: var_dump($_REQUEST)". I've writt ...

Exploring the Differences Between Nth-CSS and Jquery

What are the advantages of using the nth function in CSS instead of applying it through jQuery, especially considering its compatibility with IE? Is it better to simply use jQuery from the start and avoid using it in a stylesheet altogether? Hopefully thi ...

Dynamic navigation menu shifting when bolded

Looking at my menu, you'll notice the following layout: If one clicks on Recruitment in the menu, it correctly bolds the text as intended. However, an issue arises where the entire menu unexpectedly shifts 1 or 2px to the left. The menu is implemente ...

List of elements inside an HTML box

In my current HTML project, I am trying to include a box on a webpage that contains a scrollable list of elements. I want the content inside the box to scroll independently without affecting the rest of the page. Is there a way to achieve this? ...

Using JQuery to conceal floated divs

I'm facing an issue that I had resolved the last time I worked on this website. However, I am currently redesigning it to include additional elements, and I am struggling to make the jQuery part function correctly. Essentially, I have an HTML documen ...

I am facing an issue with my login button as it does not seem to be performing the necessary function. Instead of advancing to the next

this is my implementation code from flask import Flask,request, render_template, redirect,session, url_for from flask_mysqldb import MySQL import MySQLdb.cursors app = Flask(__name__) app.config["MYSQL_HOST"] = "localhost" app.confi ...

Problems encountered when applying box-shadow for text background across multiple lines

Lately, I've become accustomed to using box-shadow to add backgrounds to text headings. I've been employing a little trick of applying a box-shadow to a span element within h1 tags in order to create a unique "background with padding" highlight e ...

When hovering or mousing over, the text remains stationary and does not follow the scroll bar movement within the

A method I am using involves displaying additional data on hover for a shortened header, like this: Hover behavior However, the text shifts across the page when the scrollbar is used, as shown here: Scrollbar interaction This table showcases HTML, CSS, ...

How to store selected checkbox values in an array using AngularJS

Check out this code snippet: <tr ng-repeat="doc in providers"> <td><input type="checkbox" ng-true-value="{{doc.provider.Id}}" ng-false-value="" ng-model="ids"></td> </tr> {{ids}} I am trying to store the values of ...

Browser displaying a CSS error: "Invalid property name" while applying pseudo-element :after

I encountered an issue in Chrome and Explorer while attempting to set a CSS property for a pseudo element :after. (I'm trying to create a styled burger nav icon) The error message I received was: 'Unknown property name' This happened wh ...

What is the best way to gather the "name" and "value" attributes from a designated section of a form using JavaScript and store them in an array?

How can I extract the "name" and "value" attributes from a specific section of a form and store them in an array using JavaScript? Here is the section of the form in question: <div class="multiPickerForm"> <input type="text" name="Id" value="1"& ...

Why are there no borders around the rows and columns of my table?

I'm just starting to learn HTML so I appreciate your patience with what may be a basic question for some. After creating a table using , I've noticed that there are no visible lines separating the cells. Is there a way to add lines around the ce ...

Getting Started with Angular JS: A Beginner's Essentials

I am feeling lost on where to begin with developing my app. When I click the add button, I want a form to show up - should I append a div to that button? How should I approach this problem? Snippet of Code: <body> <div id="wrap"> <!- ...

Tips for displaying a variable that contains HTML in PHP

I'm facing an issue with a variable that holds an HTML iframe obtained from a third-party package. When I try to display it using <?php echo $variable ?>, the page shows the raw HTML with tags instead of rendering it properly. Can someone guide ...

Tips for shortening button text within a flex child element

I'm struggling to achieve a specific layout using flexbox. I require one child element that does not shrink, and two child elements that can shrink/grow and be truncated based on their content while considering the sizes of their content. My current ...

Creating a gap between two elements without using the space-between property

Currently working on creating a responsive menu, <div style={{ display: "flex", flexFlow: "row wrap" }}> {/* left side */} <div key="1">nav 1</div> <div key="2">n ...

Achieving consistent vertical alignment of text in different web browsers

I have thoroughly reviewed the existing links on this topic, but none of them have been useful in my specific case. Most solutions suggest a way to vertically move text so that it appears aligned. However, the issue I am facing is that the text is already ...

The CSS code conceals the icon within the background

Could someone please help me out? I am new to this and can't seem to figure out what's wrong. The phone icon seems to be hidden behind a white background. Everything else looks fine, but I really want the icon to be in the front with a border and ...