radio 和checkbox与文字对齐问题

本文记录了解决网页布局中不同字体大小下radio按钮与文字对齐的问题,提供了多种方法来调整对齐效果,包括设置CSS样式属性如font-size、vertical-align、margin等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在项目中遇到radio和文字对齐问题(ie不明显,火狐和google比较明显),在此记录。

1.浏览器默认文字大小为14px,因而当文字字体为14px时radio和checkbox与文字对齐良好,如下所示:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <input type="radio" value="1"> 1
    <input type="radio" value="2"> 2
    <input type="radio" value="3"> 3
    <input type="radio" value="4"> 4
    <input type="radio" value="5"> 5
    <input type="radio" value="6"> >5
   <br/>
<input type="radio" value="1"> 学生 <input type="radio" value="2"> 老师 </body> </html>

输出结果如下:

2.更改字体大小,对齐出现问题

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
body {
    font-size: 12px;
}
</style>
</head>
<body>
    <input type="radio" value="1"> 1
    <input type="radio" value="2"> 2
    <input type="radio" value="3"> 3
    <input type="radio" value="4"> 4
    <input type="radio" value="5"> 5
    <input type="radio" value="6"> >5
    <br/>
    <input type="radio" value="1"> 学生
    <input type="radio" value="2"> 老师

</body>
</html>

输出结果如下:

若字体更改为10px或者更小对齐问题更加严重(当然字体大于14px也会出现类似问题)如下为字体为10px时

3.解决方法

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style>
body {
    font-size: 12px;
}
.inputStyle {
    vertical-align: text-bottom;
    margin-bottom: 2px;
    *margin-bottom: -2px;  //兼容IE6,IE7
}
</style>
</head>
<body>
    <input type="radio" value="1" class="inputStyle"> 1
    <input type="radio" value="2" class="inputStyle"> 2
    <input type="radio" value="3" class="inputStyle"> 3
    <input type="radio" value="4" class="inputStyle"> 4
    <input type="radio" value="5" class="inputStyle"> 5
    <input type="radio" value="6" class="inputStyle"> >5
    <br/>
    <br/>
    <input type="radio" value="1" class="inputStyle"> 学生
    <input type="radio" value="2" class="inputStyle"> 老师

</body>
</html>

效果如下:

4.其他方法

1)当文字12px左右大小时,单(复)选框设置height:13px; vertical-align:text-top; margin-top:0;效果如下:
单选框   复选框
2)当文字12px左右大小时,单(复)选框设置height:15px; vertical-align:bottom; margin-bottom:3px; margin-top:-1px;效果如下:
单选框   复选框
3)当文字12px左右大小时,单(复)选框设置height:14px; vertical-align:top;样式后的表现,效果如下:
单选框   复选框 
4)当文字12px左右大小时,单(复)选框设置vertical-align:middle; margin-top:-2px; margin-bottom:1px;效果如下:
单选框   复选框 

 

 

 

 

转载于:https://www.cnblogs.com/wishyouhappy/p/3666244.html

当使用 CSS 修改 checkbox radio 样式尺寸后,可能会导致选择框后面对应的文字无法水平对齐。这是因为原生的选择框自定义的选择框大小位置不同,导致文字对齐出现问题。为了解决这个问题,可以采用以下两种方法之一: 1. 使用 `vertical-align` 属性将选择框文字垂直对齐。示例代码如下: ```css input[type="checkbox"], input[type="radio"] { display: none; } label.checkbox, label.radio { display: inline-block; vertical-align: middle; margin-right: 10px; position: relative; padding-left: 30px; cursor: pointer; } label.checkbox:before, label.radio:before { content: ""; display: inline-block; width: 20px; height: 20px; border: 1px solid #ccc; position: absolute; left: 0; top: 0; border-radius: 4px; transition: all 0.3s ease; } label.checkbox:before { border-radius: 4px; } label.checkbox:after, label.radio:after { content: ""; display: inline-block; width: 10px; height: 10px; position: absolute; left: 4px; top: 4px; border-radius: 2px; transition: all 0.3s ease; opacity: 0; } label.checkbox:hover:before, label.radio:hover:before { border-color: #666; } input[type="checkbox"]:checked + label.checkbox:before, input[type="radio"]:checked + label.radio:before { border-color: #f00; } input[type="checkbox"]:checked + label.checkbox:after, input[type="radio"]:checked + label.radio:after { opacity: 1; } ``` 2. 将选择框文字放在容器中,并使用 `display: flex; align-items: center;` 将其垂直对齐。示例代码如下: ```css .container { display: flex; align-items: center; } input[type="checkbox"], input[type="radio"] { display: none; } label.checkbox, label.radio { position: relative; padding-left: 30px; cursor: pointer; } label.checkbox:before, label.radio:before { content: ""; display: inline-block; width: 20px; height: 20px; border: 1px solid #ccc; position: absolute; left: 0; top: 0; border-radius: 4px; transition: all 0.3s ease; } label.checkbox:before { border-radius: 4px; } label.checkbox:after, label.radio:after { content: ""; display: inline-block; width: 10px; height: 10px; position: absolute; left: 4px; top: 4px; border-radius: 2px; transition: all 0.3s ease; opacity: 0; } label.checkbox:hover:before, label.radio:hover:before { border-color: #666; } input[type="checkbox"]:checked + label.checkbox:before, input[type="radio"]:checked + label.radio:before { border-color: #f00; } input[type="checkbox"]:checked + label.checkbox:after, input[type="radio"]:checked + label.radio:after { opacity: 1; } ``` 以上两种方法都可以解决选择框文字无法水平对齐问题,根据实际情况选择其中一种即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值