[英]HTML/CSS Styling Checkbox
I am trying to have a letter being a checkbox so that when the checkbox is clicked the background of the checkbox changes but the letter is still there. 我正在尝试将字母作为复选框,以便单击该复选框时,该复选框的背景会更改,但该字母仍然存在。 So far I got the checkbox to change color when checked, but unable to get letter as a background. 到目前为止,我已选中该复选框以在选中时更改颜色,但无法获取字母作为背景。 Also each checkbox will have a different letter. 另外,每个复选框都有一个不同的字母。
.checkbox { display: inline-block; cursor: pointer; font-size: 13px; line-height: 18px; } input[type=checkbox] { display: none; } .checkbox:before { content: ""; display: inline-block; width: 40px; height: 40px; vertical-align: middle; text-align: center; border-width: 5px; border-style: solid; border-radius: 25px; } input[type=checkbox]:checked+.checkbox:before { background-color: red; background-size: contain; }
<input type="checkbox" id="checkbox"> <label class="checkbox" for="checkbox"></label> <label>S</label>
I would put the text in the label, and make the circle absolute
ly positioned in the label, then use flex
to center the text in the label. 我将文本放入标签中,并使圆absolute
位于标签中,然后使用flex
将文本置于标签中。
.checkbox { cursor: pointer; font-size: 13px; line-height: 18px; width: 40px; height: 40px; position: relative; display: flex; align-items: center; justify-content: center; } input[type=checkbox] { display: none; } .checkbox:before { content: ""; position: absolute; z-index: -1; vertical-align: middle; text-align: center; border-width: 5px; border-style: solid; border-radius: 50%; top: 0; left: 0; right: 0; bottom: 0; } input[type=checkbox]:checked + .checkbox:before { background-color: red; background-size: contain; }
<input type="checkbox" id="checkbox"> <label class="checkbox" for="checkbox">S</label>
Alternative: use position absolute on the label with text "S" and transform translate to center in middle snippet below 备选方案:在标签上使用文本“ S”的绝对位置,并将其转换为以下中间代码段的中心
.checkbox { display: inline-block; cursor: pointer; font-size: 13px; line-height:18px; } input[type=checkbox] { display:none; } .checkbox:before { content: ""; display: inline-block; width: 40px; height: 40px; vertical-align: middle; text-align: center; border-width: 5px; border-style: solid; border-radius: 25px; } input[type=checkbox]:checked + .checkbox:before { background-color: red; background-size: contain; } .checkbox{ position:relative } .checkbox label{ position:absolute; top:0; left:0; top:50%; left:50%; transform:translate(-50%,-50%) }
<input type="checkbox" id="checkbox"> <label class="checkbox" for="checkbox"> <label>S</label> </label>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.