首页 > 解决方案 > 如何设置两个除法元素平行?

问题描述

我想让标签和复选框大小相同。请问如何针对此类问题更改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

标签: javascripthtmlcss

解决方案


  • 使用 CSSflex和对齐子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>


推荐阅读