
盒子里的绝对定位元素发生预期外的像素偏移?
在使用自定义的 input checkbox 选择框样式时,发现不同分辨率下居中效果会偏移,且在选中状态下红色小元素相对于外框不居中。代码如下:
原因:在不同分辨率下,使用像素作为单位会导致像素点移位。
解决方案:将所有像素值换成相对单位。例如:
.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;
}










