Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom radio button using css

Tags:

html

css

I am trying to create the custom radio button as shown in the picture below. enter image description here

I wrote the code and able to achieve the correct styling but not able to make the label appear before the radio buttons.

.lengend-action-buttons {
  float: left;
  margin-left: 10px;
  margin-top: 10px;
}
label {
  font-weight: normal;
  font-size: 14px;
  Font-Family: Metric-Regular;
  Color: #666666;
  display: block;
  cursor: pointer;
}
[type="radio"] {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}
[type="radio"] + span:before {
  content: '';
  display: inline-block;
  width: 1.1em;
  height: 1.1em;
  vertical-align: -0.10em;
  border-radius: 1em;
  border: 0.35em solid #fff;
  box-shadow: 0 0 0 0.10em #36B18D;
  margin-right: 0.75em;
  transition: 0.5s ease all;
}
[type="radio"]:checked + span:before {
  background: #36B18D;
  box-shadow: 0 0 0 0.10em #36B18D;
}
[type="radio"]:focus + span::after {
  font-size: 1.2em;
  line-height: 1;
  vertical-align: -0.125em;
}
<div class="lengend-action-buttons lengend-action-buttons-first">
  <label for="d3_graph_chart0011day">
    <input type="radio" name="date_range" id="d3_graph_chart0011day" value="1day" checked="checked">
    <span>1 Day</span>
  </label>
</div>

<div class="lengend-action-buttons lengend-action-buttons-first">
  <label for="d3_graph_chart0017day">
    <input type="radio" name="date_range" id="d3_graph_chart0017day" value="7day">
    <span>7 Day</span>
  </label>
</div>

<div class="lengend-action-buttons lengend-action-buttons-first">
  <label for="d3_graph_chart00130day">
    <input type="radio" name="date_range" id="d3_graph_chart00130day" value="30day">
    <span>30 Day</span>
  </label>
</div>

<div class="lengend-action-buttons lengend-action-buttons-first">
  <label for="d3_graph_chart00190day">
    <input type="radio" name="date_range" id="d3_graph_chart00190day" value="901day">
    <span>90 Day</span>
  </label>
</div>

Please help me, resolve this issue.

like image 884
nikunj2512 Avatar asked Feb 05 '16 10:02

nikunj2512


People also ask

How do I target a radio button in CSS?

For a radio button, you can use the input[type="radio"]:checked + label selector, which matches a label that immediately follows a checked input of type radio.

How do I inline a radio button in CSS?

Using the +operator, we select the sibling element in CSS. Selecting the selector using input[type="radio"] + label span we can define properties for the radio button. The width, height are for the dimensions. The display: inline-block property makes it a block level element that behaves as inline.


1 Answers

Replace before with after and vice-versa for span and yeah, margin-left with margin-right:

.lengend-action-buttons {
  float: left;
  margin-left: 10px;
  margin-top: 10px;
}
label {
  font-weight: normal;
  font-size: 14px;
  Font-Family: Metric-Regular;
  Color: #666666;
  display: block;
  cursor: pointer;
}
[type="radio"] {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}
[type="radio"] + span:after {
  content: '';
  display: inline-block;
  width: 1.1em;
  height: 1.1em;
  vertical-align: -0.10em;
  border-radius: 1em;
  border: 0.35em solid #fff;
  box-shadow: 0 0 0 0.10em #36B18D;
  margin-left: 0.75em;
  transition: 0.5s ease all;
}
[type="radio"]:checked + span:after {
  background: #36B18D;
  box-shadow: 0 0 0 0.10em #36B18D;
}
[type="radio"]:focus + span::before {
  font-size: 1.2em;
  line-height: 1;
  vertical-align: -0.125em;
}
<div class="lengend-action-buttons lengend-action-buttons-first">
  <label for="d3_graph_chart0011day">
    <input type="radio" name="date_range" id="d3_graph_chart0011day" value="1day" checked="checked">
    <span>1 Day</span>
  </label>
</div>

<div class="lengend-action-buttons lengend-action-buttons-first">
  <label for="d3_graph_chart0017day">
    <input type="radio" name="date_range" id="d3_graph_chart0017day" value="7day">
    <span>7 Day</span>
  </label>
</div>

<div class="lengend-action-buttons lengend-action-buttons-first">
  <label for="d3_graph_chart00130day">
    <input type="radio" name="date_range" id="d3_graph_chart00130day" value="30day">
    <span>30 Day</span>
  </label>
</div>

<div class="lengend-action-buttons lengend-action-buttons-first">
  <label for="d3_graph_chart00190day">
    <input type="radio" name="date_range" id="d3_graph_chart00190day" value="901day">
    <span>90 Day</span>
  </label>
</div>
like image 132
Praveen Kumar Purushothaman Avatar answered Oct 05 '22 17:10

Praveen Kumar Purushothaman