css - 格式化列和行中的元素

标签 css formatting

为了避免重新发明轮子,我使用 CakePHP 的表单助手生成了一个表单,它创建了以下标记:

    <div class="input select"><label for="ReportFruit">Fruit</label>
        <input type="hidden" name="data[Report][Fruit]" value="" id="ReportFruit" /> 

<div class="checkbox"><input type="checkbox" name="data[Report][Fruit][]" value="0" id="ReportFruit0" /><label for="ReportFruit0">Banana</label></div> 
<div class="checkbox"><input type="checkbox" name="data[Report][Fruit][]" value="1" id="ReportFruit1" /><label for="ReportFruit1">Apple</label></div> 
<div class="checkbox"><input type="checkbox" name="data[Report][Fruit][]" value="2" id="ReportFruit2" /><label for="ReportFruit2">Pear</label>
...
</div> 
</div>​

它以这种格式生成一堆复选框:

[] Banana
[] Apple
[] Pear
[] ...

我想对它们进行格式化,以便它们像这样显示: (理想情况下,我仍然可以设置每行的选项数,但如果不是也可以)

[] Banana    [] Apple     [] Pear
[] Mango     [] Lemon     [] ...

我能否仅使用 CSS 完成此操作,还是必须使用 JS 操作 DOM(或在输出之前使用 PHP 更改标记)?

最佳答案

您可以使用以下 CSS:

div.checkbox { float: left; width: 31%; margin-right: 1% }

(1% 用于舍入问题;根据您的需要减小宽度并增加 margin-right。当然您也可以使用像素值)

这会将复选框及其标签分布在三列中。但是,如果长标签横跨多行,您可能会遇到分布问题(试试看我的意思)。

为防止这种情况,给每第 3 列附加类 newline:

<div class="checkbox newline">

并在 CSS 中定义:

div.checkbox.newline { clear: left }

关于css - 格式化列和行中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3391324/

相关文章:

Javascript字符串格式化,在xpath表达式中传递方法参数

html - 缩小浏览器会导致样式问题

CSS 问题 - 导航菜单

html - ul 在 table 上移动

xml - 使用 Powershell 进行外科 XML 编辑

javascript - 如何在 chrome 控制台中完全打印大数组?

javascript - 使用 icomoon 将 SVG 转换为字体图标

javascript - 我怎样才能改变这个网格上各个方 block 的颜色......?

数据对象中许多字段的 Java 数字格式

python - 如何在 Eclipse 中自动填充段落?