CSS 美化checkbox核取方塊

antzone發表於2018-06-05

分享一段程式碼例項,它實現了利用css3美化核取方塊的效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style type="text/css">
.ant {
  width: 25px;
  margin: 20px 100px;
  position: relative;
}
.ant input[type=checkbox] {
  visibility: hidden;
}
 
.ant label {
  cursor: pointer;
  position: absolute;
  width: 25px;
  height: 25px;
  top: 0;
  left: 0;
  background: #fff;
  border:2px solid #ccc;
  border-radius:3px;            /* W3C syntax */
}
.ant label:after {
  opacity: 0;
  content: '';
  position: absolute;
  width: 13px;
  height: 5px;
  background: transparent;
  top: 6px;
  left: 6px;
  border: 2px solid #333;
  border-top: none;
  border-right: none;
  transform: rotate(-45deg);
}
 
.ant label:hover::after {
 opacity: 0.5;
}
.ant input[type=checkbox]:checked + label:after {
  opacity: 1;
}
.ant label span{
  display: inline-block;
  margin-left: 30px;
  margin-top: 5px;
  white-space: nowrap;
}
</style>
</head>
<body>
<div class="ant">
  <input type="checkbox" value="1" id="ant"  name="ant" />
  <label for="ant"><span>螞蟻部落</span></label>
</div>
</body>
</html>

上述程式碼實現了對核取方塊的基本美化效果,可以在此基礎上進行美化。

相關文章