Reputation: 55
Here is what I have working right now. Just trying to figure out how to add another small circle inside it to get it to make exactly like the image I included above.
/* The container */
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default radio button */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
/* Create a custom radio button */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 28px;
width: 28px;
background-color: #eee;
border-radius: 50%;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #0f8c73;
}
/* When the radio button is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #0f8c73;
}
/* Create the indicator (the dot/circle - hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the indicator (dot/circle) when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the indicator (dot/circle) */
.container .checkmark:after {
top: 3.5px;
left: 3px;
width: 22px;
height: 22px;
border-radius: 50%;
background: white;
}
</style>
<body>
<h1>Custom Radio Buttons</h1>
<label class="container">One
<input type="radio" checked="checked" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Two
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Three
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
<label class="container">Four
<input type="radio" name="radio">
<span class="checkmark"></span>
</label>
</body>
</html>
I'm trying to replicate this. I'm not sure how to implement this way using CSS. Currently, I just have a circle check box working. Not sure how to add the ring or shrink the inner circle.
Upvotes: 0
Views: 1234
Reputation: 2891
Alexander makes a good point about whether this is better suited semantically as a radio button.
Either way the approach to add a border with a gap is to combine border
and box-shadow
:
.round input[type="checkbox"]:checked+label {
left: 2px;
position: absolute;
top: 2px;
width: 18px;
height: 18px;
border-radius: 50%;
background-color: teal;
border: 5px solid #fff;
box-shadow: 0 0 0 2px teal;
}
Snippet in action below:
.round {
position: relative;
}
.round label {
background-color: #fff;
border: 2px solid teal;
border-radius: 50%;
cursor: pointer;
height: 28px;
left: 0;
position: absolute;
top: 0;
width: 28px;
}
.round input[type="checkbox"] {
visibility: hidden;
height: 10px;
width: 10px;
}
.round input[type="checkbox"]:checked+label {
left: 2px;
position: absolute;
top: 2px;
width: 18px;
height: 18px;
border-radius: 50%;
background-color: teal;
border: 5px solid #fff;
box-shadow: 0 0 0 2px teal;
}
.round input[type="checkbox"]:checked+label:after {
opacity: 1;
}
<div class="round">
<input type="checkbox" id="checkbox" />
<label for="checkbox"></label>
</div>
Upvotes: 3