javascript - 如何设置两个除法元素平行?
问题描述
我想让标签和复选框大小相同。请问如何针对此类问题更改CSS属性?
屏幕截图中说明了该问题。
[![复选框和标签][1]][1]
const viewTemplate = (
<div className="stack-small">
<div className="c-cb">
<input
id={props.id}
type="checkbox"
defaultChecked={props.completed}
onChange={() => props.toggleTaskCompleted(props.id)}
/>
<label className="todo-label" htmlFor={props.id}>
<p style={{ fontWeight: props.important ? 'bold' : 'normal' }}>{props.name}</p>
</label>
</div>
CSS
.c-cb {
box-sizing: border-box;
font-family: Arial, sans-serif;
-webkit-font-smoothing: antialiased;
font-weight: 400;
font-size: 1.6rem;
line-height: 1.25;
display: block;
position: relative;
min-height: 44px;
padding-left: 40px;
clear: left;
}
.c-cb > label::before,
.c-cb > input[type="checkbox"] {
box-sizing: border-box;
top: -2px;
left: -2px;
width: 44px;
height: 44px;
display: inline-block;
}
.c-cb > input[type="checkbox"] {
-webkit-font-smoothing: antialiased;
cursor: pointer;
position: absolute;
z-index: 1;
margin: 0;
opacity: 0;
display: inline-block;
}
.c-cb > label {
font-size: inherit;
font-family: inherit;
line-height: inherit;
display: inline-block;
margin-bottom: 0;
padding: 8px 15px 5px;
cursor: pointer;
touch-action: manipulation;
}
.c-cb > label::before {
content: "";
position: absolute;
border: 2px solid currentColor;
background: transparent;
}
.c-cb > input[type="checkbox"]:focus + label::before {
border-width: 4px;
outline: 2px #228bec;
}
.c-cb > label::after {
box-sizing: content-box;
content: "";
position: absolute;
top: 11px;
left: 9px;
width: 18px;
height: 7px;
transform: rotate(-45deg);
border: solid;
border-width: 0 0 5px 5px;
border-top-color: transparent;
opacity: 0;
background: transparent;
}
.c-cb > input[type="checkbox"]:checked + label::after {
opacity: 1;
}
是否需要安装任何 CSS 框架来解决此类问题?[1]:https ://i.stack.imgur.com/ZTQtn.gif
解决方案
- 使用 CSS
flex
和对齐子align-items: center;
元素justify-content: center;
- 用作
<label>
包装。切勿将块级元素(DIV、P、H1等)放在行内级元素(如标签、跨度等)内。
/*QuickReset*/ * {margin:0;box-sizing:border-box;}
body {font:14px/1.4 sans-serif;}
/* CUSTOM-CHECKBOX */
.c-cb-label {
font-size: 1.6em;
display: flex; align-items: center;
cursor: pointer;
}
.c-cb-label::before {
content: " ";
display: flex; align-items: center; justify-content: center;
width: 1.5em; height: 1.5em;
margin-right: 0.3em;
border: 4px solid currentColor;
}
.c-cb :checked + .c-cb-label::before {
content: "\2714"; /* or use a font-icon (like icomoon) unicode hex */
}
<label class="c-cb">
<input hidden type="checkbox" name="test" />
<span class="c-cb-label">Prop name</span>
</label>
推荐阅读
- python-3.x - 鹡鸰模板页面不呈现图像
- c# - 尝试在 xamarin.uitests 中调用后门方法失败
- python-3.x - 我在将列表中的元素加载到 .h5 文件时遇到问题
- c++ - 在 C++ 中使用动态内存
- vue.js - bryntum 调度程序 showCheckAll 不起作用
- spring - Spring Boot MVC 项目中的国际化
- python - 使用 Python 字典确定要删除哪些重复图像
- python - python discord.py 中的 JSON
- node.js - 安装@ngrx/store 时如何清除错误?
- json - 在 Groovy(或 JOLT)中修改 JSON