盒子里的绝对定位元素发生预期外的像素偏移?-灵析社区

感觉对了

## 盒子里的绝对定位元素发生预期外的像素偏移 ### 自定义的input checkbox的选择框样式,在不同的分辨率下,代码设置了居中但不居中(是选中时红色的小元素相对于外框不居中!!) **选中checkbox表现** ![checkbox图片](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241011/cb18c05758c154a978b4ef5c12f837ee.png) ![checkbox图片](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241011/b68a90fb0e1f01bfd8bd4aa567ac40a9.png) ![checkbox图片](https://wmprod.oss-cn-shanghai.aliyuncs.com/c/user/20241011/262468f7462d48d6a927d387f40f3788.png) 图片看可以看到是不居中的!! 尝试过不同的居中方式,还是会在不同的分辨率下会产生偏移,但是放大看确实居中的 #### 代码 .clause-content { display: flex; flex-direction: row; align-items: start; } .clause-input { display: inline-block; vertical-align: middle; width: 15px; height: 15px; cursor: pointer; position: relative; background-color: #fff; margin-right: 12px; border: 1px solid rgba(237, 30, 14, 0.15); } .clause-input input { opacity: 0; } .clause-input input:checked+i { width: 10px; height: 10px; position: absolute; left: 50%; top: 50%; margin-left: -5px; margin-top: -5px; /* transform: translate(-50%, -50%); */ background-color: #ED1C24; } clause.

阅读量:118

点赞量:0

问AI
只要你使用px,像素点在不同分辨率下就可能会有移位的问题。都换成相对单位来计算就可以解决这个问题,我这里的数字是对照px大概给的,你可以按实际情况调整: .clause-content { display: flex; flex-direction: row; align-items: start; } .clause-input { display: inline-block; vertical-align: middle; width: 1rem; height: 1rem; cursor: pointer; position: relative; background-color: #fff; margin-right: 0.8rem; border: 0.1rem solid rgba(237, 30, 14, 0.15); } .clause-input input { opacity: 0; } .clause-input input:checked + i { width: 0.6rem; height: 0.6rem; position: absolute; left: 50%; top: 50%; margin-left: -0.3rem; margin-top: -0.3rem; /* transform: translate(-50%, -50%); */ background-color: #ed1c24; }