css - 垂直放置样式系列的单选按钮

标签 css radio-button

我有以下 html。

<td>
    <div id="form_comparison" class="field radio_field">
        <input type="radio" id="form_comparison_0" name="form[comparison]" value="1"/>
        <label for="form_comparison_0">Increased</label>
        <input type="radio" id="form_comparison_1" name="form[comparison]" value="2" />
        <label for="form_comparison_1">About the same</label>
        <input type="radio" id="form_comparison_2" name="form[comparison]" value="3" />
        <label for="form_comparison_2">Decreased</label>
    </div>
</td>

如何使用 css 垂直放置单选按钮,以便标签显示在同一行中各自的单选按钮之后?

最佳答案

我不知道你为什么把它包装在一个 td 元素中,如果你正在设计一个表单布局,而不是忽略表格并使用 div 来设计你的形式。谈到你的问题,你可以将标签包裹在 input 标签周围并使用 display: block; 作为 label

#form_comparison label {
    display: block;
}

像这样使用label包装每个input

<label for="form_comparison_0">
    <input type="radio" id="form_comparison_0" name="form[comparison]" value="1"/>
    Increased
</label>

Demo


如果您没有更改标记的任何权限,您可以将 CSS content 属性与 white-space: pre; 一起使用,这将为您提供所需的内容输出

label:after {
    content: "\A";
    white-space: pre;
    margin-bottom: 10px;
}

Demo (标记没有变化)

Note: Use #form_comparison label instead of only label as it will select and apply all label element in your website where #form_comparison label will only select label elements inside #form_comparison

关于css - 垂直放置样式系列的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16644614/

相关文章:

javascript - 为什么在点击时从 iframe 中删除属性?

javascript - 如何创建包含colspans的固定表标题行?

html - 单选按钮中的 Tab 键顺序不起作用

css - 更改选中单选按钮的不透明度

android - 如何更改 RadioButton 的轮廓大小

image - CSS 中的内容属性以显示图像图标

css - 引用从 css 元素到另一个文件的路径

ios - 如何让两个 UIButton 像 Swift 中的单选按钮一样执行?

javascript - 使用 javascript 缺少内容更改 html 内容

forms - 使用原型(prototype)获取/设置复选框和单选按钮值