我正在try 设置单选按钮的样式,以便当 Select 一个按钮时,它看起来像这样(左/第一个):

selected radio button

这是我到目前为止掌握的代码:

@import url(https://fonts.googleapis.com/css?family=Lato:400,300);

input[type="radio"] {
  display: none;
}
input[type="radio"]:checked + label span {
  transform: scale(1.25);
}
input[type="radio"]:checked + label .red {
  border: 2px solid #711313;
}
input[type="radio"]:checked + label .orange {
  border: 2px solid #873a08;
}
input[type="radio"]:checked + label .yellow {
  border: 2px solid #816102;
}
input[type="radio"]:checked + label .olive {
  border: 2px solid #505a0b;
}
input[type="radio"]:checked + label .green {
  border: 2px solid #0e4e1d;
}
input[type="radio"]:checked + label .teal {
  border: 2px solid #003633;
}
input[type="radio"]:checked + label .blue {
  border: 2px solid #103f62;
}
input[type="radio"]:checked + label .violet {
  border: 2px solid #321a64;
}
input[type="radio"]:checked + label .purple {
  border: 2px solid #501962;
}
input[type="radio"]:checked + label .pink {
  border: 2px solid #851554;
}

label {
  display: inline-block;
  width: 36px;
  height: 36px;
  margin-right: 10px;
  cursor: pointer;
}
label:hover span {
  transform: scale(1.25);
}
label span {
  display: block;
  width: 100%;
  height: 100%;
  transition: transform 0.2s ease-in-out;
  border-radius: 50%;
}
label span.red {
  background: #db2828;
}
label span.orange {
  background: #f2711c;
}
label span.yellow {
  background: #fbbd08;
}
label span.olive {
  background: #b5cc18;
}
label span.green {
  background: #21ba45;
}
label span.teal {
  background: #00b5ad;
}
label span.blue {
  background: #2185d0;
}
label span.violet {
  background: #6435c9;
}
label span.purple {
  background: #a333c8;
}
label span.pink {
  background: #e03997;
}
<h1>Radio Color Picker</h1>
<input type="radio" name="color" id="red" value="red" />
<label for="red"><span class="red"></span></label>

<input type="radio" name="color" id="green" />
<label for="green"><span class="green"></span></label>

<input type="radio" name="color" id="yellow" />
<label for="yellow"><span class="yellow"></span></label>

<input type="radio" name="color" id="olive" />
<label for="olive"><span class="olive"></span></label>

<input type="radio" name="color" id="orange" />
<label for="orange"><span class="orange"></span></label>

<input type="radio" name="color" id="teal" />
<label for="teal"><span class="teal"></span></label>

<input type="radio" name="color" id="blue" />
<label for="blue"><span class="blue"></span></label>

<input type="radio" name="color" id="violet" />
<label for="violet"><span class="violet"></span></label>

<input type="radio" name="color" id="purple" />
<label for="purple"><span class="purple"></span></label>

<input type="radio" name="color" id="pink" />
<label for="pink"><span class="pink"></span></label>

如您所见,在此代码中,当您 Select 一个单选按钮时,将添加以下规则:

input[type="radio"]:checked + label span {
  transform: scale(1.25);
}

因此,当按钮被选中时,它们将变得更大.但我希望它们的尺寸保持不变,背景 colored颜色 覆盖的圆圈比其他 colored颜色 小.添加边框 colored颜色 的部分已经存在.我怎么才能做到这一点呢?

推荐答案

outline属性在此可能很有用(选中时添加轮廓而不是边框并更改宽度和高度) 通过更改outline-offsetwidthheight,可以调整缩进量

@import url(https://fonts.googleapis.com/css?family=Lato:400,300);

input[type="radio"] {
  display: none;
}
input[type="radio"]:checked + label span {
  /* changed */
  width: 75%;
  height: 75%;
  padding: 2px;
  margin: 2px;
  outline-offset: 4px;
}
input[type="radio"]:checked + label .red {
  outline: 2px solid #711313;/* changed */
}
input[type="radio"]:checked + label .orange {
  outline: 2px solid #873a08;/* changed */
}
input[type="radio"]:checked + label .yellow {
  outline: 2px solid #816102;/* changed */
}
input[type="radio"]:checked + label .olive {
  outline: 2px solid #505a0b;/* changed */
}
input[type="radio"]:checked + label .green {
  outline: 2px solid #0e4e1d;/* changed */
}
input[type="radio"]:checked + label .teal {
  outline: 2px solid #003633;/* changed */
}
input[type="radio"]:checked + label .blue {
  outline: 2px solid #103f62;/* changed */
}
input[type="radio"]:checked + label .violet {
  outline: 2px solid #321a64;/* changed */
}
input[type="radio"]:checked + label .purple {
  outline: 2px solid #501962;/* changed */
}
input[type="radio"]:checked + label .pink {
  outline: 2px solid #851554;/* changed */
}


label {
  display: inline-block;
  width: 36px;
  height: 36px;
  margin-right: 10px;
  cursor: pointer;
}
label:hover span {
  /*transform: scale(1.25);*/
}
label span {
  display: block;
  width: 100%;
  height: 100%;
  transition: transform 0.2s ease-in-out;
  border-radius: 50%;
}
label span.red {
  background: #db2828;
}
label span.orange {
  background: #f2711c;
}
label span.yellow {
  background: #fbbd08;
}
label span.olive {
  background: #b5cc18;
}
label span.green {
  background: #21ba45;
}
label span.teal {
  background: #00b5ad;
}
label span.blue {
  background: #2185d0;
}
label span.violet {
  background: #6435c9;
}
label span.purple {
  background: #a333c8;
}
label span.pink {
  background: #e03997;
}
<h1>Radio Color Picker</h1>
<input type="radio" name="color" id="red" value="red" />
<label for="red"><span class="red"></span></label>

<input type="radio" name="color" id="green" />
<label for="green"><span class="green"></span></label>

<input type="radio" name="color" id="yellow" />
<label for="yellow"><span class="yellow"></span></label>

<input type="radio" name="color" id="olive" />
<label for="olive"><span class="olive"></span></label>

<input type="radio" name="color" id="orange" />
<label for="orange"><span class="orange"></span></label>

<input type="radio" name="color" id="teal" />
<label for="teal"><span class="teal"></span></label>

<input type="radio" name="color" id="blue" />
<label for="blue"><span class="blue"></span></label>

<input type="radio" name="color" id="violet" />
<label for="violet"><span class="violet"></span></label>

<input type="radio" name="color" id="purple" />
<label for="purple"><span class="purple"></span></label>

<input type="radio" name="color" id="pink" />
<label for="pink"><span class="pink"></span></label>

Html相关问答推荐

NG8004:找不到名为""的管道.'' [插件Angular 编译器]

你能让Cypress测试为p—fileUpload做文件上传吗?

输入宽度在表格显示上不起作用FLEX溢出隐藏Firefox

在 bootstrap 中的弹性项之间添加连接行

为什么我的搜索栏会出现在WONG位置?

如何翻转卡片图像的背面

按钮之间的HTML文本居中不正确

如何在没有包装元素的情况下在React(Next.js)中呈现HTML注释

带有图像的虚线边框

为什么要添加换行符(至少在 Google Chrome 中)

(HTML框架标签)点击后目标框架将不再工作

如何使单选按钮在被 Select /选中时看起来像这样的设计?

按钮悬停效果不影响按钮内的图标

为什么 CSS 网格超出了父级?

R 中的网页抓取数字?

如何使用javascript在jsx中重复插入特殊字符?

按钮显示:内联不换行

th 内的 div 标签没有占据全高

鼠标悬停时切换 colored颜色 的双色链接

为什么 `html` 上的 overflow: hidden 将滚动框移动到正文?