如何使复选框旁边的文本垂直对齐中心?

时间:2017-02-16 10:36:24

标签: html css

input[type="checkbox"] {
  display: none;
}

span:before {
  font-family: "FontAwesome";
  font-style: normal;
  content: 'O';
  margin-right: 3px;
  font-size: 30px;
 }

input[type="checkbox"]:checked ~ span:before {
  content: 'Z';
}
<label>
<input type="checkbox"/>
<span>Text</span>
</label>
有什么办法可以让文本在css内容旁边的中间?

5 个答案:

答案 0 :(得分:4)

您可以在display: flex元素上使用align-items: centerspan

span {
  display: flex;
  align-items: center;
}
input[type="checkbox"] {
  display: none;
}
span:before {
  font-family: "FontAwesome";
  font-style: normal;
  content: 'O';
  margin-right: 3px;
  font-size: 30px;
}
input[type="checkbox"]:checked ~ span:before {
  content: 'Z';
}
<label>
  <input type="checkbox" />
  <span>Text</span>
</label>

答案 1 :(得分:2)

添加


    vertical-align:middle;


    span:before {
      font-family: "FontAwesome";
      font-style: normal;
      content: 'O';
      margin-right: 3px;
      font-size: 30px;
      vertical-align:middle;
     }

答案 2 :(得分:1)

您可以将以下内容添加到span:before元素中:

span:before {
   vertical-align: middle;
   position: relative;
}

答案 3 :(得分:0)

将范围的display设置为flex并对齐项center会有效。

input[type="checkbox"] {
  display: none;
}

label span {
  display: flex;
  align-items: center;
}

span:before {
  font-family: "FontAwesome";
  font-style: normal;
  content: 'O';
  margin-right: 3px;
  font-size: 30px;
 }

input[type="checkbox"]:checked ~ span:before {
  content: 'Z';
}
<label>
<input type="checkbox"/>
<span>Text</span>
</label>

答案 4 :(得分:0)

只需将vertical-align:sub添加到span:before

这是js小提琴链接 - Fiddle

span:before {
 font-family: "FontAwesome";
 font-style: normal;
 content: 'O';
 margin-right: 3px;
font-size: 30px;
 vertical-align:sub;
}