【问题标题】:center a check/tick within a custom check box在自定义复选框中居中选中/勾选
【发布时间】:2017-01-24 20:26:30
【问题描述】:

我正在构建一个自定义复选框,功能差不多了,但样式还有一段路要走。

我有一些问题,

  1. 我需要以圆圈为中心的支票
  2. 我只需要在单击复选框本身而不是文本时激活检查。

恐怕如果不使用左侧位置的填充,我似乎无法将支票居中。

HTML

<label for='product-45-45'>
  <input type='checkbox' style="float:left;" id='product-45-45' />
  <div class="accord-text">
    <strong>header:</strong> sub text
    <strong>more text!</strong>
  </div>
</label>

CSS

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

input[type=checkbox] + .accord-text:before {
  width: 30px;
  height: 30px;
  border-radius: 200%;
  background-color: #d6e4ec;
  border: 1px solid #000;
  display: block;
  font-size: 150%;
  font-weight: 900;
  content: "";
  color: green;
}

input[type=checkbox]:checked + .accord-text:before {
  display: table;
  content: "\2713";
}

【问题讨论】:

    标签: html css checkbox styling


    【解决方案1】:
    • 问题 1:text-align: center; 选中复选框 ic 并从复选框中删除 style="float:left;"
    • 问题2:将accord-text div中的文字去掉,放到外面。

    .row{
      display: flex;
      flex-direction: row;
    }
    
    input[type=checkbox] {
      display: none;
    }
    
    input[type=checkbox] + .accord-text:before {
      width: 30px;
      height: 30px;
      border-radius: 200%;
      background-color: #d6e4ec;
      border: 1px solid #000;
      display: block;
      font-size: 140%;
      font-weight: 900;
      content: "";
      color: green;
    }
    
    input[type=checkbox]:checked + .accord-text:before {
      display: table;
      content: "\2713";
      text-align: center;
    }
    <div class="row">
      <label for='product-45-45'>
      <input type='checkbox' style="float:left;" id='product-45-45' />
      <div class="accord-text">
    
      </div>
    </label>
    
      <strong>header:</strong> sub text
      <strong>more text!</strong>
    </div>

    【讨论】:

    • 要将所有内容放在一行中,我是否只需将其包装在一个跨度中?
    • 我更喜欢使用带有 flex-direction = row 的 div,但是是的,使用 span 也是一个不错的解决方案。
    • 我们检查时复选框变大了,你能避免吗
    • 是的,只是让字体大小变小,我们必须保持两个大小之间的关系。看看
    【解决方案2】:

    用这个 sn-p 检查我也已经解决了我们检查时的高度问题,

    display:blockmax-height 添加到此input[type=checkbox]:checked + .accord-text:before

    input[type=checkbox] {
      display: none;
    }
    label {
    float:left;
    }
    input[type=checkbox] + .accord-text:before {
      width: 30px;
      height: 30px;
      border-radius: 200%;
      background-color: #d6e4ec;
      border: 1px solid #000;
      display: block;
      font-size: 150%;
      font-weight: 900;
      content: "";
      color: green;
      text-align:center;
      max-height:30px;
    }
    
    input[type=checkbox]:checked + .accord-text:before {
      display: table;
      content: "\2713";
      max-height:30px;
      display:block;
    }
    span {
    float:left;
    padding-left:5px;
    }
    <label for='product-45-45'>
      <input type='checkbox' style="float:left;" id='product-45-45' />
      <div class="accord-text"></div>
    </label>
    <span><strong>header:</strong> sub text
      <strong>more text!</strong>
    </span>

    【讨论】:

    • check with now,点击时没有跳转复选框
    【解决方案3】:

    您需要删除将输入样式设置为向左浮动的内联代码,当浮动打开时您无法将其居中。然后添加 margin: 0 auto 使其居中:

    input[type=checkbox]:checked {
      float: none;
    }
    
    input[type=checkbox] + .accord-text:before {
      margin: 0 auto;
      width: 30px;
      height: 30px;
      border-radius: 200%;
      background-color: #d6e4ec;
      border: 1px solid #000;
      display: block;
      font-size: 150%;
      font-weight: 900;
      content: "";
      color: green;
    }
    

    【讨论】:

    • 不,我在小提琴上检查过这个,但不管用,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-12
    • 2010-12-15
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    相关资源
    最近更新 更多