javascript - 复选框强制换行

标签 javascript html css

我正在尝试添加一个复选框,但它似乎总是在下一行。

我以某种方式通过设计修复了它,但它总是回到新线路。

这是我从网络复制的示例复选框

input[type=checkbox]+label {
  display: block;
  cursor: pointer;
  padding: 0.2em;
  width: 113px;
}

input[type=checkbox] {
  display: none;
}

input[type=checkbox]+label:before {
  content: "\2714";
  border: 0.1em solid #000;
  border-radius: 0.2em;
  display: inline-block;
  width: 1em;
  height: 1em;
  vertical-align: bottom;
  color: transparent;
  transition: .2s;
}

input[type=checkbox]+label:active:before {
  transform: scale(0);
}

input[type=checkbox]:checked+label:before {
  background-color: MediumSeaGreen;
  border-color: MediumSeaGreen;
  color: #fff;
}
<input type="checkbox" id="fruit1" name="fruit-1" value="Apple">
<label for="fruit1">Apple</label>
<input type="checkbox" id="fruit2" name="fruit-2" value="Banana" disabled>
<label for="fruit2">Banana</label>
<input type="checkbox" id="fruit3" name="fruit-3" value="Cherry" checked disabled>
<label for="fruit3">Cherry</label>
<input type="checkbox" id="fruit4" name="fruit-4" value="Strawberry">
<label for="fruit4">Strawberry</label>

最佳答案

不要使用标签的显示 block :

<!DOCTYPE html>
<html>

<head>
    <title>Parcel Sandbox</title>
    <meta charset="UTF-8" />
    <style>
    input[type=checkbox]+label {
  cursor: pointer;
  padding: 0.2em;
  width: 113px;
}

input[type=checkbox] {
  display: none;
}

input[type=checkbox]+label:before {
  content: "\2714";
  border: 0.1em solid #000;
  border-radius: 0.2em;
  display: inline-block;
  width: 1em;
  height: 1em;
  vertical-align: bottom;
  color: transparent;
  transition: .2s;
}

input[type=checkbox]+label:active:before {
  transform: scale(0);
}

input[type=checkbox]:checked+label:before {
  background-color: MediumSeaGreen;
  border-color: MediumSeaGreen;
  color: #fff;
}
    </style>
</head>

<body>
    <div id="app">
        <input type="checkbox" id="fruit1" name="fruit-1" value="Apple">
        <label for="fruit1">Apple</label>
        <input type="checkbox" id="fruit2" name="fruit-2" value="Banana" disabled>
        <label for="fruit2">Banana</label>
        <input type="checkbox" id="fruit3" name="fruit-3" value="Cherry" checked disabled>
        <label for="fruit3">Cherry</label>
        <input type="checkbox" id="fruit4" name="fruit-4" value="Strawberry">
        <label for="fruit4">Strawberry</label>
    </div>
    </script>
</body>

</html>

Sample

关于javascript - 复选框强制换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60791439/

相关文章:

javascript - 是否可以仅在某些浏览器宽度时更改 div 的顺序(不仅仅是美学上的)?

javascript - 该页面表示用户在文本框部分输入的值

CSS - 是否可以根据设备分辨率上传不同的图像?

html - 在另一个 div 中左右定位 div

javascript - 如何在运行时更改/删除 CSS 类定义?

javascript - 使用逻辑运算符解构状态分配

javascript - 样式历史后退按钮

html - float div 在悬停时展开,将第二行 div 向左移动而不是向下移动

javascript - 背景图像 jQuery 不工作,我做错了什么?

html - 当我们在 <ol> 中有多个元素时,如何将 <ol> 中的元素与 <oil> 的编号对齐