0

I have multiple color select option, I want to highlight whatever they are selected

Here is my first image first image,

Here I want to convert this to this image below

Thanks for time and suggestions

mahan
  • 12,366
  • 5
  • 48
  • 83

1 Answers1

0

You can change the style of radio put likewise. For the checked state, use [input-selector]:checked + label:after and for the unchecked state, use [input-selector]:not(:checked) + label:before.

[type="radio"]:checked,
[type="radio"]:not(:checked) {
  position: absolute;
  left: -9999px;
}

[type="radio"]:checked+label,
[type="radio"]:not(:checked)+label {
  position: relative;
  padding-left: 28px;
  cursor: pointer;
  line-height: 20px;
  display: inline-block;
  color: #666;
}

[type="radio"]:checked+label:before,
[type="radio"]:not(:checked)+label:before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  width: 18px;
  height: 18px;
  border: 1px solid #ddd;
  border-radius: 100%;
  background: #fff;
}

[type="radio"]:checked+label:after,
[type="radio"]:not(:checked)+label:after {
  content: '';
  width: 12px;
  height: 12px;
  position: absolute;
  top: 3px;
  left: 3px;
  border-radius: 100%;
  -webkit-transition: all 0.2s ease;
  transition: all 0.2s ease;
}

[type="radio"]:not(:checked)+label:after {
  -webkit-transform: scale(0);
  transform: scale(0);
}

[type="radio"]:checked+label:after {
  -webkit-transform: scale(1);
  transform: scale(1);
}


/* To change the background color, only customize the code below*/

.red-input:not(:checked)+label:before {
  background: red;
}

.red-input:checked+label:after {
  background: black;
}

.green-input:not(:checked)+label:before {
  background: green;
}

.green-input:checked+label:after {
  background: blue;
}

.black-input:not(:checked)+label:before {
  background: black;
}

.black-input:checked+label:after {
  background: silver;
}

.silver-input:not(:checked)+label:before {
  background: silver
}

.silver-input:checked+label:after {
  background: #E6B5A3;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css" rel="stylesheet"/>
<form class="form-inline m-5">
  <div class="form-group">
    <div class="mx-2">
      
    <input type="radio" id="test1" name="radio-group" class="red-input">
    <label for="test1">Peach</label>
   </div>
    <div class="mx-2">
    <input type="radio" id="test2" name="radio-group" class="green-input">
    <label for="test2">Orange</label>
   </div>
   <div class="mx-2">
    <input type="radio" id="test3" name="radio-group" class="black-input">
    <label for="test3">Mango</label>
   </div>
   <div class="mx-2">
    <input type="radio" id="test4" name="radio-group" class="silver-input">
    <label for="test4">Lime</label>
   </div>
  </div>
</form>

Check this codepen

Update

To set the default background of the inputs, use [input-selector]:not(:checked)+label:before.

.red-input:not(:checked)+label:before {
  background: red;
}
mahan
  • 12,366
  • 5
  • 48
  • 83
  • Hi @mahan, how can i show on page load all their respectively color instead of white background?, on click just highlight as previous highlighted one – user9795151dsasda May 15 '18 at 17:18
  • Updated the code and the codepen link too. Use `[input-slector]:not(:checked) + label:before` – mahan May 15 '18 at 17:36
  • Thanks a lot you are genius in css and bootstrap – user9795151dsasda May 15 '18 at 17:41
  • can you do one more favour, i am working on bootstrap template, i am facing real issue like , can you please look at this once? https://stackoverflow.com/questions/50335471/how-to-fix-right-div-image-and-scroll-left-div-text-content-in-bootstrap – user9795151dsasda May 15 '18 at 17:42