简体   繁体   English

如何在CSS中包装复选框表单元素?

[英]How can I wrap checkbox form elements in CSS?

I am working on some code that presents a form with a number of checkboxes. 我正在处理一些代码,这些代码提供了带有多个复选框的表单。 I would like to get the checkboxes to wrap to a second (and third, and fourth) line, but am having trouble doing so. 我想将复选框包装到第二行(第三行和第四行),但是这样做很麻烦。 At the moment, the checkboxes run straight off the page in a line without wrapping. 此刻,复选框直接在页面上一行而没有换行。

There are 10 (or more) checkboxes, but for the sake of brevity I've listed only a few of them since listing all of them wouldn't really add to the conversation: 有10个(或更多)复选框,但为简洁起见,我只列出了其中几个,因为列出所有这些并不会真正添加到对话中:

My CSS: 我的CSS:

.add-to-cart .attribute label {
    display: inline;
    padding-right: 35px;
}
.add-to-cart .form-checkboxes{
    max-width: 600px;
    height: 300px;
    display: inline-flex;
}

.add-to-cart .attribute .form-item .form-type-checkbox {
    position: absolute;
    display: inline-block;
    width: 100%;
    height: 90px;
    background-color: #ddd;
    padding: 20px;
    margin: 10px;
    white-space: nowrap;
    text-align: center;
    vertical-align: middle;
    font: bold 10px verdana, arial, 'Raleway', sans-serif;
    font-style: italic;
}

My HTML/Code: 我的HTML /代码:

<div class="content">
<div class="add-to-cart">
    <form class="ajax-cart-submit-form" action="/this-product" method="post" id="uc-product-add-to-cart-form-7" accept-charset="UTF-8">
    <div class="attribute attribute-7 even">
        <div class="form-item form-type-checkboxes form-item-attributes-7">
            <label for="edit-attributes-7">Extras </label>
            <div id="edit-attributes-7" class="form-checkboxes">
                <div class="form-item form-type-checkbox form-item-attributes-7-49">
                    <input type="checkbox" id="edit-attributes-7-49" name="attributes[7][49]" value="49" class="form-checkbox" />
                    <label class="option" for="edit-attributes-7-49"> Blue </label>
                </div>
                <div class="form-item form-type-checkbox form-item-attributes-7-43">
                    <input type="checkbox" id="edit-attributes-7-43" name="attributes[7][43]" value="43" class="form-checkbox" />
                    <label class="option" for="edit-attributes-7-43"> Red </label>
                </div>
                <div class="form-item form-type-checkbox form-item-attributes-7-50">
                    <input type="checkbox" id="edit-attributes-7-50" name="attributes[7][50]" value="50" class="form-checkbox" />
                    <label class="option" for="edit-attributes-7-50"> Green </label>
                </div>
            </div>
        </div>
    </div>
</div>
</div>

Try this 尝试这个

.add-to-cart .attribute label {
    display: inline;
    padding-right: 35px;
}
.add-to-cart .form-checkboxes{
    max-width: 600px;
    height: 300px;
    display: inline-flex;
}

.add-to-cart .attribute .form-checkboxes:not(.form-item) {
    position: absolute;
    display: inline-flex;
    width: 100%;
    height: 90px;
    background-color: #ddd;
    padding: 20px;
    margin: 10px;
    white-space: nowrap;
    text-align: center;
    vertical-align: middle;
    font: bold 10px verdana, arial, 'Raleway', sans-serif;
    font-style: italic;
}

live demo - https://jsfiddle.net/91r7v20q/ 现场演示-https: //jsfiddle.net/91r7v20q/

Well, I figured it out. 好吧,我知道了。 I changed this CSS code: 我更改了此CSS代码:

.add-to-cart .form-checkboxes{
    max-width: 600px;
    height: 300px;
    display: inline-flex;
}

To this: 对此:

.add-to-cart .form-checkboxes{
    max-width: 600px;
    display: inline-block;
}

I removed the fixed height to allow the block to be flexible in allowing the elements to be completely displayed before the last row (with the submit button, see comments on this questions). 我删除了固定高度,以使块变得灵活,从而可以在最后一行之前完全显示元素(使用提交按钮,请参阅有关此问题的评论)。 Whew! 呼!

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

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