0

I have an a element that acts like a toggle button :

var container = document.getElementsByClassName("favorite");

for (var i = 0; i < container.length; i++) {
  //For each element in the container array, add an onclick event.
  container[i].onclick = function(event) {
    this.classList.toggle('selected');
  }
}
body {
  background-color: #04246A;
}
.container {
    position: relative;
    width: 204px;
    max-width: 204px;
    height: 82px;
    max-height: 82px;
    padding: 24px;
    background: #fff;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    box-sizing: border-box;
    color: black;
  list-style: none;
   border: 2px solid black;
}

.container .favorite {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 40px;
    height: 40px;
    display: flex;
    border-radius: 50%;
    cursor: pointer;
}

.container .favorite:hover {
  background: #D9DEEA;
}

.container .favorite::after {
    z-index: 2;
    position: relative;
    margin: auto;
    height: 20px;
    content: url("https://img.icons8.com/ios/16/000000/star.png");
}

.container .favorite.selected::after {
    content: url("https://img.icons8.com/ios-filled/16/000000/star.png");
}

.container dl {
    margin: 0;
}

.container dl dt {
    display: block;
    font-size: 12px;
    color: #6B7790;
}
.container dl dd {
    margin: 0;
    font-weight: normal;
    color: #04246A;
}
<div class="container">
  <a class="favorite"></a>
  <dl>
    <dt>Ref</dt>
    <dd>XXX123</dd>
  </dl>
</div>

I want to transform it to a checkbox, but the solutions I find are for a checkbox with a label, I only want to use the checkbox in my html without the label or some additional div, this is what I tried :

body {
  background-color: #04246A;
}
.container {
position: relative;
width: 204px;
max-width: 204px;
height: 82px;
max-height: 82px;
padding: 24px;
background: #fff;
display: flex;
flex-direction: column;
justify-content: space-between;
box-sizing: border-box;
color: black;
  list-style: none;
   border: 2px solid black;
}

 input[type="checkbox"]{
   height: 20px;
   width: 20px;
   position: absolute;
   opacity: 1;
   margin: auto;
   height: 20px;
   cursor: pointer;
   top: 10px;
  right: 10px;
}

input[type="checkbox"]::after{
  content: url("https://img.icons8.com/ios/16/000000/star.png");
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 50%;
  margin: auto;
  height: 20px;
  width: 20px;
  pointer-events: none;
}

input[type="checkbox"]:checked:after {
  content: url("https://img.icons8.com/ios-filled/16/000000/star.png");

}
input[type="checkbox"]:hover:after{
  background: #D9DEEA;
}

.container dl {
margin: 0;
}

.container dl dt {
display: block;
font-size: 12px;
color: #6B7790;
}
.container dl dd {
margin: 0;
font-weight: normal;
color: #04246A;
}
<div class="container">
  <input type="checkbox" />
  <dl>
    <dt>Ref</dt>
    <dd>XXX123</dd>
  </dl>
</div>

Which doesn't look good.

How can I fix this ? and thanks in advance.

Renaud is Not Bill Gates
  • 1,684
  • 34
  • 105
  • 191

1 Answers1

2

You can add appearance:none to remove to checkbox appearance and set fixed height/width to checkbox and :after, changing :after to flex allows you to center the background.

body {
  background-color: #04246A;
}
.container {
position: relative;
width: 204px;
max-width: 204px;
height: 82px;
max-height: 82px;
padding: 24px;
background: #fff;
display: flex;
flex-direction: column;
justify-content: space-between;
box-sizing: border-box;
color: black;
  list-style: none;
   border: 2px solid black;
}

 input[type="checkbox"]{
   height: 40px;
   width: 40px;
   position: absolute;
   opacity: 1;
   margin: auto;
   cursor: pointer;
   top: 10px;
   right: 10px;
   appearance: none;
   outline: none;
}

input[type="checkbox"]::after{
  content: url("https://img.icons8.com/ios/16/000000/star.png");
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 50%;
  margin: auto;
  height: 40px;
  width: 40px;
  pointer-events: none;
  display: flex;
  justify-content: center;
  align-items: center;
}

input[type="checkbox"]:checked:after {
  content: url("https://img.icons8.com/ios-filled/16/000000/star.png");

}
input[type="checkbox"]:hover:after{
  background: #D9DEEA;
}

.container dl {
margin: 0;
}

.container dl dt {
display: block;
font-size: 12px;
color: #6B7790;
}
.container dl dd {
margin: 0;
font-weight: normal;
color: #04246A;
}
<div class="container">
  <input type="checkbox" />
  <dl>
    <dt>Ref</dt>
    <dd>XXX123</dd>
  </dl>
</div>
Amyth
  • 1,367
  • 1
  • 9
  • 15