简体   繁体   English

排列/样式复选框分为两行[Bootstrap&AngularJS]

[英]Arranging/styling checkboxes to be in two rows [Bootstrap & AngularJS]

I have an AngularJS app on which I need to collect Day preferences from the user. 我有一个AngularJS应用程序,我需要从该用户收集日期偏好设置。 I'm having trouble getting it laid out how I want it. 我无法按照我想要的方式制作它。

The days array has 8 items (7 + "All"), and I want it arranged in 2 rows of 4. How can I get it to split itself so it's inline for the 4, but then breaks into a new row? days数组有8个项目(7 +“All”),我希望它排成2行4.我怎么能让它自己分裂所以它是4的内联,但是然后分成一个新行? I'd like to do this without hard coding all of the days since it's easier to relate to my model this way. 我想在没有硬编码的情况下这样做,因为这样更容易与我的模型相关联。

And here's the incorrect (current) layout, and how I want it to look... 这是不正确的(当前)布局,以及我希望它看起来如何...... 布局不正确我希望它看起来像什么

    <div class="form-group checkbox">
        <p class="h2">When do you usually like to lunch?</p>
        <label ng-repeat="day in days" class="sm" ng-click="dayClicked(day)">
            <input type="checkbox"
                   value="{{day.name}}"
                   ng-model="day.selected">
            {{day.name}}
        </label>
    </div>

Try something like this. 尝试这样的事情。

label.sm {
  display: block;
  float: left;
  width: 25%;
}
label.sm:last-child:after {
  clear: both;
  content: " ";
  display: block;
  height: 0px;
  overflow: hidden;
}

Added explanation 补充说明

The first rule, label.sm , targets each of your labels. 第一个规则label.sm定位您的每个标签。 It modifies the default display of a label so it will have a specific width. 它会修改标签的默认显示,使其具有特定的宽度。 You can technically use display: inline-block , but some older browsers ( cough cough IE) don't respect that. 你可以在技术上使用display: inline-block ,但是一些旧的浏览器( 咳嗽咳嗽 IE)不尊重它。 Combine display: block with float: left to emulate display: inline-block . 结合display: blockfloat: left模拟display: inline-block Specifying width: 25% makes each label one-fourth (100% / 4 = 25%) the width of the available space in its container. 指定width: 25%使每个标签占其容器中可用空间宽度的四分之一(100%/ 4 = 25%)。

The second rule, label.sm:last-child:after targets the last of the labels in the group and creates a pseudo-element (an element that isn't in the HTML). 第二个规则label.sm:last-child:after定位组中的最后一个标签并创建一个伪元素(不在HTML中的元素)。 The pseudo-element has a single space for its contents, spans the width of its container, has no height, hides anything outside of the space it's been given (which is nothing), and clears the float flag set by the labels. 伪元素的内容有一个空格,跨越其容器的宽度,没有高度,隐藏它所给出的空间之外的任何东西(什么都不是),并清除标签设置的浮动标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM