html - 如何让输入单选元素(包括其文本)成为固定宽度?

标签 html css radio-button

出于对齐目的,我需要我的输入单选元素 + 关联文本具有相同的宽度。我试过这个:

.rad {      
    width: 80px;
}

...但这只适用于 radio 元素本身(将它们置于一片空白的海洋中)。

所以我尝试了这个:

.rad text {      
    width: 160px;
}

...但它什么都不做。

用于样式化的 html 是:

<input type="radio" class="rad" id="visitor" name="travelertype" />Visitor
<input type="radio" class="rad" id="ucstudent" name="travelertype"/>UC Student
<input type="radio" class="rad" id="ucemployee" name="travelertype"/>UC Employee

更新 2

尝试回答,我得到:

enter image description here

最佳答案

您需要更改标记才能实现此目的。方法有很多种,这里介绍其中的两种。

方法 1:为单选按钮和选择使用单独的表格列。

<table>
   <tr>
      <td>
        <input type="radio" class="rad" id="visitor" name="travelertype" />Visitor
      </td>
      <td>
         <select ...>
      </td>
   </tr>

   ... and so on.
</table>

方法二:将文字包裹在<label>中s 并为标签添加样式

<input type="radio" class="rad" id="visitor" name="travelertype" /><label for="travelertype" class="label-radio">Visitor</label>

CSS:

label.label-radio {
    width: 100px;
}

关于html - 如何让输入单选元素(包括其文本)成为固定宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32083843/

相关文章:

android - 如何在回收 View 中获取选定单选按钮的值?

html - 为什么悬停不能处理移动的物体?

javascript - 调用 jQuery.i18n 库后,我收到 Uncaught TypeError : string is not a function in my login method

javascript - 如何将嵌套的 foreach 值传递给 CanvasJS 图表?

html - Bootstrap/Jasny "offcanvas"侧边栏菜单卡住页面内容(即防止滚动)

html - CSS冲突,网站没有意义。属性不适用于某些 div

javascript - 为什么我的 onchange 事件是在我的页面加载时触发的,而不是在我单击单选按钮时触发的?

html - CSS 选择器不工作 — 输入[type=radio] :checked

html - 显示 : flex not working in CSS

Markdown 样式表所需的 css 兄弟选择器帮助