html - 使用图像而不是单选按钮

标签 html css

如果我有一个带按钮的单选组:

Image 1

...我怎样才能在选择选项中只显示图像而不是按钮,例如

enter image description here

最佳答案

  • radioimage 包装在<label>
  • 隐藏单选按钮(不要使用 display:nonevisibility:hidden,因为这会影响可访问性)
  • 使用 Adjacent sibling selector 定位隐藏 radio 旁边的图像+
  • 不要忘记在 alt 中提供替代文本属性,特别是因为它充当单选按钮的标签

/* HIDE RADIO */
[type=radio] { 
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

/* IMAGE STYLES */
[type=radio] + img {
  cursor: pointer;
}

/* CHECKED STYLES */
[type=radio]:checked + img {
  outline: 2px solid #f00;
}
<label>
  <input type="radio" name="test" value="small" checked>
  <img src="https://via.placeholder.com/40x60/0bf/fff&text=A" alt="Option 1">
</label>

<label>
  <input type="radio" name="test" value="big">
  <img src="https://via.placeholder.com/40x60/b0f/fff&text=B" alt="Option 2">
</label>

不要忘记在您的标签中添加一个,并在 CSS 中使用该


自定义样式和动画

这是使用 <i> 的高级版本元素和 ::after pseudo-element :

CSS custom radio and checkbox

body{color:#444;font:100%/1.4 sans-serif;}


/* CUSTOM RADIO & CHECKBOXES
   http://stackoverflow.com/a/17541916/383904 */
.rad,
.ckb{
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.rad > input,
.ckb > input{ /* HIDE ORG RADIO & CHECKBOX */
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}
/* RADIO & CHECKBOX STYLES */
/* DEFAULT <i> STYLE */
.rad > i,
.ckb > i{ 
  display: inline-block;
  vertical-align: middle;
  height: 16px;
  transition: 0.2s;
  box-shadow: inset 0 0 0 8px #fff;
  border: 1px solid gray;
  background: gray;
}
.rad > i {
  width:  16px;
  border-radius: 50%;
}
.ckb > i {
  width: 25px;
  border-radius: 3px;
}
.rad:hover > i{ /* HOVER <i> STYLE */
  box-shadow: inset 0 0 0 3px #fff;
  background: gray;
}
.rad > input:focus + i { /* FOCUS <i> STYLE */
  outline: 1px solid blue;
}
.rad > input:checked + i{ /* (RADIO CHECKED) <i> STYLE */
  box-shadow: inset 0 0 0 3px #fff;
  background: orange;
}
/* CHECKBOX */
.ckb > input + i::after{
  content: "";
  display: block;
  height: 12px;
  width:  12px;
  margin: 2px;
  border-radius: inherit;
  transition: inherit;
  background: gray;
}
.ckb > input:focus + i {
  outline: 1px solid blue;
}
.ckb > input:checked + i::after{ /* (RADIO CHECKED) <i> STYLE */
  margin-left: 11px;
  background:  orange;
}
<label class="rad">
  <input type="radio" name="rad1" value="a">
  <i></i> Radio 1
</label>
<label class="rad">
  <input type="radio" name="rad1" value="b" checked>
  <i></i> Radio 2
</label>

<br>

<label class="ckb">
  <input type="checkbox" name="ckb1" value="a" checked>
  <i aria-hidden="true"></i> Checkbox 1
</label>
<label class="ckb">
  <input type="checkbox" name="ckb2" value="b">
  <i aria-hidden="true"></i> Checkbox 2
</label>

关于html - 使用图像而不是单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17541614/

相关文章:

javascript - 在负载上多次生成随机 div

javascript - 如何在悬停事件上打开包含可点击内容的弹出窗口

javascript - 如何克服图像悬停中的这个问题?

javascript - Bootstrap : Remove column margin

html - 如何居中对齐页面上的元素?

javascript - 如何使用 Jquery 移动溢出的 div 中的元素?

javascript - 如何解决包含数据库信息的页面加载缓慢

html - 我可以使用带边框颜色的渐变吗?

html - 在 Bootstrap 模式中调整 Bootstrap 登录组件的大小

javascript - 如何隐藏鼠标悬停在按钮或链接上时跟随光标的箭头