使用CSS的自定义单选按钮

时间:2016-02-05 10:04:50

标签: html css

我正在尝试创建自定义单选按钮,如下图所示。 enter image description here

我编写了代码,能够实现正确的样式,但无法在单选按钮之前显示标签。

.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>

请帮助我,解决此问题。

2 个答案:

答案 0 :(得分:5)

before替换为after,将span替换为margin-left,将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; }

&#13;
&#13;
<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>
&#13;
# load geosphere library 
library("geosphere")

#create a distance matrix between all points in the 2 dataframes
dist<-distm(df1[,c('X','Y')],df2[,c('X','Y')])

# find the nearest neighbour to each point
df1$nearestneighbor <- apply(dist,1,min)

# create a distance matrix where only the distances between points of the same category are recorded
sameCat <- outer(df1$Cat, df2$Cat, "!=")
dist2 <- dist + ifelse(sameCat, Inf, 0)

# find the nearest neighbour of the same category
df1$closestmatch <- apply(dist2,1,min)
&#13;
&#13;
&#13;

答案 1 :(得分:0)

:before替换为:after

使文本中间对齐设置vertical-alignmiddle

.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: middle;
  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: middle;
}
<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>