如何使用CSS自定义复选框外观

时间:2016-08-27 09:39:37

标签: html5 twitter-bootstrap css3 checkbox

我想自定义它,以便复选框颜色应在点击时更改。请参考link and image并建议如何实现这一点,因为我不知道该怎么做。

以下是复选框的HTML代码:

<div id="container">
    <div id="btnOuterDIV">
        <div id="btnChkbox">
            <input type="checkbox" id="btnCB" />
        </div>
    </div>
</div>

复选框的CSS,它应该看起来像图像中的那个:

#container {
    padding:50px;
}
#btnOuterDIV {
    height:30px;
    width:80px;
    border:1px solid #ccc;
    border-radius:5px;
}
#btnOuterDIV:hover {
    border-color:#888;
}
#btnChkbox {
    height:15px;
    width:15px;
    padding-top:5px;
    padding-left:5px;
}

5 个答案:

答案 0 :(得分:1)

你必须制作一个<label>并设计它。

<input type="checkbox" id="cb">
<label for="cb" class="checkbox"></label>

在CSS中,有:checked,您可以这样使用它:

#cb:checked + .checkbox {
  /* style here */
}

Example here

答案 1 :(得分:0)

[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
  position: absolute;
  left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}

/* checkbox aspect */
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
  content: '';
  position: absolute;
  left:0; top: 2px;
  width: 17px; height: 17px;
  border: 1px solid #aaa;
  background: #f8f8f8;
  border-radius: 3px;
  box-shadow: inset 0 1px 3px rgba(0,0,0,.3)
}
/* checked mark aspect */
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
  content: '✔';
  position: absolute;
  top: 3px; left: 4px;
  font-size: 18px;
  line-height: 0.8;
  color: #09ad7e;
  transition: all .2s;
}
[type="checkbox"]:not(:checked) + label:after {
  opacity: 0;
  transform: scale(0);
}
body {
  font-family: "Open sans", "Segoe UI", "Segoe WP", Helvetica, Arial, sans-serif;
  color: #777;
}
h1 {
  margin-bottom: 5px;
  font-weight: normal;
  text-align: center;
}
form {
  width: 80px;
  margin: 0 auto;
}
<h1>Custom checkboxes with CSS</h1>


<form action="#">
  <p>
    <input type="checkbox" id="test1" />
    <label for="test1">Red</label>
  </p>
  
</form>

答案 2 :(得分:0)

    <h1>Custom checkboxes with CSS</h1>


    <form action="#">
      <p>
        <input type="checkbox" id="test1" />
        <label for="test1">Red</label>
      </p>

    </form>



------CSS--------


[type="checkbox"]:not(:checked),
[type="checkbox"]:checked {
  position: absolute;
  left: -9999px;
}

[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
  position: relative;
  padding-left: 25px;
  cursor: pointer;
}


/* checkbox aspect */

[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
  content: '';
  position: absolute;
  left: 0;
  top: 2px;
  width: 17px;
  height: 17px;
  border: 1px solid #aaa;
  background: #f8f8f8;
  border-radius: 3px;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, .3)
}


/* checked mark aspect */

[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
  content: '✔';
  position: absolute;
  top: 3px;
  left: 4px;
  font-size: 18px;
  line-height: 0.8;
  color: white;
  background-color:#FF3336;
  transition: all .2s;
}

[type="checkbox"]:not(:checked) + label:after {
  opacity: 0;
  transform: scale(0);
}

body {
  font-family: "Open sans", "Segoe UI", "Segoe WP", Helvetica, Arial, sans-serif;
  color: #777;
  background-color:#4CFF33;
}

h1 {
  margin-bottom: 5px;
  font-weight: normal;
  text-align: center;
}

form {
  width: 80px;
  margin: 0 auto;
}

答案 3 :(得分:0)

    res.clearCookie(name, { maxAge: 30000, httpOnly: true, secure: false, sameSite: "Lax" })

答案 4 :(得分:0)

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<style>
.checkbox {
    display: inline-flex;
    cursor: pointer;
    position: relative;
}

.checkbox > span {
    color: #34495E;
    padding: 0.5rem 0.25rem;
}

.checkbox > input {
    height: 25px;
    width: 25px;
    -webkit-appearance: none;
    -moz-appearance: none;
    -o-appearance: none;
    appearance: none;
    border: 1px solid #34495E;
    border-radius: 4px;
    outline: none;
    transition-duration: 0.3s;
    background-color: #41B883;
    cursor: pointer;
  }

.checkbox > input:checked {
    border: 1px solid #41B883;
    background-color: #34495E;
}

.checkbox > input:checked + span::before {
    content: '\2713';
    display: block;
    text-align: center;
    color: #41B883;
    position: absolute;
    left: 0.7rem;
    top: 0.2rem;
}

.checkbox > input:active {
    border: 2px solid #34495E;
}
</style>
<body>
  <label class="checkbox">
      <input type="checkbox" />
      <span>Check Me</span>
  </label>
</body>
</html>