【问题标题】:How can I change the size of a Bootstrap checkbox?如何更改 Bootstrap 复选框的大小?
【发布时间】:2014-05-09 17:20:39
【问题描述】:

想知道是否可以像使用按钮一样更改复选框的大小。我希望它更大,所以它很容易按下。现在它看起来像这样:

代码:

 <div class="form-group">
    <label  class="col-md-7 control-label">Kalsiumklorid: </label>
    <div class="col-md-5" >
      {{ Form::checkbox('O_Kals_Klor', 1 , array('class' => 'form-control' ))  }}  
    </div>
  </div>

【问题讨论】:

标签: css twitter-bootstrap checkbox laravel-4 laravel-blade


【解决方案1】:

在 css 中是可以的,但不是所有的浏览器都可以。

对所有浏览器的影响:

http://www.456bereastreet.com/lab/form_controls/checkboxes/

一种可能性是使用 javascript 自定义复选框:

http://ryanfait.com/resources/custom-checkboxes-and-radio-buttons/

【讨论】:

【解决方案2】:

或者你可以用像素设置样式。

 .big-checkbox {width: 30px; height: 30px;}

【讨论】:

  • 在 chrome 中工作,但在 firefox 中无效 :( 停止测试。
  • @John 我只在移动设备上使用它,因为默认的复选框尺寸太小,手指无法舒适地点击它,而且它适用于我尝试过的移动浏览器。
  • @John 谢谢你,我没有费心去尝试这个。我只是在复选框的顶部和底部添加了额外的空间以使其引人注目。
【解决方案3】:

我已经成功使用了这个库

http://plugins.krajee.com/checkbox-x

它需要 jQuery 和 bootstrap 3.x

在此处下载 zip:https://github.com/kartik-v/bootstrap-checkbox-x/zipball/master

将 zip 的内容放入项目中的文件夹中

在标题中弹出所需的库

<link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link href="path/to/css/checkbox-x.min.css" media="all" rel="stylesheet" type="text/css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js"></script>
<script src="path/to/js/checkbox-x.min.js" type="text/javascript"></script>

使用 data-size="xl" 将数据控件添加到元素以更改大小,如下所示http://plugins.krajee.com/cbx-sizes-demo

<label for="element_id">CheckME</label>
<input type="checkbox" name="my_element" id="element_id" value="1" data-toggle="checkbox-x" data-three-state="false" data-size="xl"/>

如果您浏览插件站点,还有许多其他功能。

【讨论】:

    【解决方案4】:

    现在可以为最流行的浏览器实现自定义引导复选框。

    您可以在 GitHub 中查看我的 Bootstrap-Checkbox 项目,其中包含简单的 .less file。 在 MDN 中有一个good article 描述了一些技术,其中两个主要是:

    1. 标签重定向点击事件。

      如果标签具有for 属性(如&lt;label for="target_id"&gt;Text&lt;/label&gt; &lt;input id="target_id" type="checkbox" /&gt;),或者如果它包含输入(如Bootstrap 案例:&lt;label&gt;&lt;input type="checkbox" /&gt;Text&lt;/label&gt;),则标签可以将点击事件重定向到其目标。

      这意味着可以在浏览器的一个角落放置一个标签,点击它,然后标签会将点击事件重定向到位于另一个角落的复选框,从而为该复选框产生选中/取消选中动作。

      我们可以在视觉上隐藏原始复选框,但让它仍然工作并从标签中获取点击事件。在标签本身中,我们可以模拟带有标签或伪元素:before :after 的复选框。

    2. 旧浏览器一般不支持的标签

      一些旧浏览器不支持一些 CSS 功能,例如选择兄弟姐妹 p+p 或特定搜索 input[type=checkbox]。根据 MDN 文章,支持这些功能的浏览器也支持:root CSS 选择器,而其他的则不支持。 :root 选择器只选择文档的根元素,即 HTML 页面中的 html。因此,可以使用 :root 回退到旧浏览器和原始复选框。

      最终代码sn-p:

    :root {
      /* larger checkbox */
    }
    :root label.checkbox-bootstrap input[type=checkbox] {
      /* hide original check box */
      opacity: 0;
      position: absolute;
      /* find the nearest span with checkbox-placeholder class and draw custom checkbox */
      /* draw checkmark before the span placeholder when original hidden input is checked */
      /* disabled checkbox style */
      /* disabled and checked checkbox style */
      /* when the checkbox is focused with tab key show dots arround */
    }
    :root label.checkbox-bootstrap input[type=checkbox] + span.checkbox-placeholder {
      width: 14px;
      height: 14px;
      border: 1px solid;
      border-radius: 3px;
      /*checkbox border color*/
      border-color: #737373;
      display: inline-block;
      cursor: pointer;
      margin: 0 7px 0 -20px;
      vertical-align: middle;
      text-align: center;
    }
    :root label.checkbox-bootstrap input[type=checkbox]:checked + span.checkbox-placeholder {
      background: #0ccce4;
    }
    :root label.checkbox-bootstrap input[type=checkbox]:checked + span.checkbox-placeholder:before {
      display: inline-block;
      position: relative;
      vertical-align: text-top;
      width: 5px;
      height: 9px;
      /*checkmark arrow color*/
      border: solid white;
      border-width: 0 2px 2px 0;
      /*can be done with post css autoprefixer*/
      -webkit-transform: rotate(45deg);
      -moz-transform: rotate(45deg);
      -ms-transform: rotate(45deg);
      -o-transform: rotate(45deg);
      transform: rotate(45deg);
      content: "";
    }
    :root label.checkbox-bootstrap input[type=checkbox]:disabled + span.checkbox-placeholder {
      background: #ececec;
      border-color: #c3c2c2;
    }
    :root label.checkbox-bootstrap input[type=checkbox]:checked:disabled + span.checkbox-placeholder {
      background: #d6d6d6;
      border-color: #bdbdbd;
    }
    :root label.checkbox-bootstrap input[type=checkbox]:focus:not(:hover) + span.checkbox-placeholder {
      outline: 1px dotted black;
    }
    :root label.checkbox-bootstrap.checkbox-lg input[type=checkbox] + span.checkbox-placeholder {
      width: 26px;
      height: 26px;
      border: 2px solid;
      border-radius: 5px;
      /*checkbox border color*/
      border-color: #737373;
    }
    :root label.checkbox-bootstrap.checkbox-lg input[type=checkbox]:checked + span.checkbox-placeholder:before {
      width: 9px;
      height: 15px;
      /*checkmark arrow color*/
      border: solid white;
      border-width: 0 3px 3px 0;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
    <p>
     Original checkboxes:
    </p>
    <div class="checkbox">
          <label class="checkbox-bootstrap">                                        
              <input type="checkbox">             
              <span class="checkbox-placeholder"></span>           
              Original checkbox
          </label>
     </div>
     <div class="checkbox">
          <label class="checkbox-bootstrap">                                        
              <input type="checkbox" disabled>             
              <span class="checkbox-placeholder"></span>           
              Original checkbox disabled
          </label>
     </div>
     <div class="checkbox">
          <label class="checkbox-bootstrap">                                        
              <input type="checkbox" checked>             
              <span class="checkbox-placeholder"></span>           
              Original checkbox checked
          </label>
     </div>
      <div class="checkbox">
          <label class="checkbox-bootstrap">                                        
              <input type="checkbox" checked disabled>             
              <span class="checkbox-placeholder"></span>           
              Original checkbox checked and disabled
          </label>
     </div>
     <div class="checkbox">
          <label class="checkbox-bootstrap checkbox-lg">                           
              <input type="checkbox">             
              <span class="checkbox-placeholder"></span>           
              Large checkbox unchecked
          </label>
     </div>
      <br/>
    <p>
     Inline checkboxes:
    </p>
    <label class="checkbox-inline checkbox-bootstrap">
      <input type="checkbox">
      <span class="checkbox-placeholder"></span>
      Inline 
    </label>
    <label class="checkbox-inline checkbox-bootstrap">
      <input type="checkbox" disabled>
      <span class="checkbox-placeholder"></span>
      Inline disabled
    </label>
    <label class="checkbox-inline checkbox-bootstrap">
      <input type="checkbox" checked disabled>
      <span class="checkbox-placeholder"></span>
      Inline checked and disabled
    </label>
    <label class="checkbox-inline checkbox-bootstrap checkbox-lg">
      <input type="checkbox" checked>
      <span class="checkbox-placeholder"></span>
      Large inline checked
    </label>

    【讨论】:

    • 很棒的东西。奇迹般有效。谢谢
    【解决方案5】:

    我只使用了“保存缩放”,例如:

    .my_checkbox {
        width:5vw;
        height:5vh;
    }
    

    【讨论】:

    • 简单有效的答案
    【解决方案6】:
    input[type=checkbox]
    {
      /* Double-sized Checkboxes */
      -ms-transform: scale(2); /* IE */
      -moz-transform: scale(2); /* FF */
      -webkit-transform: scale(2); /* Safari and Chrome */
      -o-transform: scale(2); /* Opera */
      padding: 10px;
    }
    

    【讨论】:

    • 这是一个很好的解决方案,但我对复选框和标签之间的间距有问题。
    • 只有 1 或 2 个  标签之前:-)
    • 这会导致 Chrome for iPhone 上的复选框出现问题,它们会变大几倍。任何想法如何解决这个问题?
    • 工作得很好,但在 Safari (Mac) 上的动画过程中有点像素化。
    【解决方案7】:
    <div id="rr-element">
       <label for="rr-1">
          <input type="checkbox" value="1" id="rr-1" name="rr[]">
          Value 1
       </label>
    </div>
    //do this on the css
    div label input { margin-right:100px; }
    

    【讨论】:

      【解决方案8】:

      以下在 bootstrap 4 中有效,在 CSS、移动设备中显示良好,并且标签间距没有问题。

      CSS

      .checkbox-lg .custom-control-label::before, 
      .checkbox-lg .custom-control-label::after {
        top: .8rem;
        width: 1.55rem;
        height: 1.55rem;
      }
      
      .checkbox-lg .custom-control-label {
        padding-top: 13px;
        padding-left: 6px;
      }
      
      
      .checkbox-xl .custom-control-label::before, 
      .checkbox-xl .custom-control-label::after {
        top: 1.2rem;
        width: 1.85rem;
        height: 1.85rem;
      }
      
      .checkbox-xl .custom-control-label {
        padding-top: 23px;
        padding-left: 10px;
      }
      

      HTML

      <div class="custom-control custom-checkbox checkbox-lg">
            <input type="checkbox" class="custom-control-input" id="checkbox-3">
            <label class="custom-control-label" for="checkbox-3">Large checkbox</label>
          </div>
      

      您也可以通过声明 checkbox-xl 来使其变大

      如果 BS 团队的任何人正在阅读这篇文章,那么如果你开箱即用,那就太好了,我在 BS 5 中也看不到任何内容

      source

      【讨论】:

        【解决方案9】:

        只需使用简单的 css

        .big-checkbox {width: 1.5rem; height: 1.5rem;top:0.5rem}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-05-12
          • 1970-01-01
          • 2011-09-09
          • 2012-04-26
          • 2016-08-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多