如何对齐css内容字符串

时间:2015-10-24 20:09:09

标签: html css alignment center

大家好我已经从复选框创建了一个切换按钮。我有一个来自awesome字体的图标作为css内容。我一直在尝试几乎所有的东西,但我并不是要将css内容字符串对齐在圆圈中间。

这是css内容部分:

.onoffswitch-label:before {
    content: "\f000";
    font-family: FontAwesome;
    display: block;
    width: 30px;
    height: 30px;
    margin: 4px 4px 0 0;
    background-color: #59B200;
    color: #fff;
    position: absolute;
    top: 0;
    bottom: 0;
    right: 52px;
    border: 2px solid #59B200;
    border-radius: 38px;
    transition: all 0.3s ease-in 0s;
}

这是完整的例子

http://jsfiddle.net/vzjyyob4/

感谢您的帮助

3 个答案:

答案 0 :(得分:3)

您可以使用text-align: center将图标水平居中,然后将line-height设置为所需的值,使其垂直对齐。

以下是标签的CSS:

text-align: center;
line-height: 32px;

这是一个更新的JSFiddle:http://jsfiddle.net/vzjyyob4/1/

这是一个内联现场演示



.onoffswitch {
  position: relative;
  width: 94px;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}
.onoffswitch-checkbox {
  display: none;
}
.onoffswitch-label {
  display: block;
  overflow: hidden;
  cursor: pointer;
  height: 38px;
  padding: 0;
  line-height: 38px;
  border: 2px solid #59B200;
  border-radius: 38px;
  background-color: #FFFFFF;
  transition: all 0.3s ease-in;
}
.onoffswitch-label:before {
  content: "\f000";
  font-family: FontAwesome;
  display: block;
  width: 30px;
  height: 30px;
  margin: 4px 4px 0 0;
  background-color: #59B200;
  color: #fff;
  position: absolute;
  top: 0;
  bottom: 0;
  right: 52px;
  border: 2px solid #59B200;
  border-radius: 38px;
  transition: all 0.3s ease-in 0s;
  text-align: center;
  line-height: 32px;
}
.onoffswitch-checkbox:checked + .onoffswitch-label {
  background-color: #59B200;
}
.onoffswitch-checkbox:checked + .onoffswitch-label {
  border-color: #59B200;
}
.onoffswitch-checkbox:checked + .onoffswitch-label:before {
  border-color: #fff;
  background-color: #fff;
  color: #59B200;
}
.onoffswitch-checkbox:checked + .onoffswitch-label:before {
  right: 0px;
}

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet" />
<div class="onoffswitch">
  <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
  <label class="onoffswitch-label" for="myonoffswitch"></label>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

只需添加此CSS规则以对齐内容:

.onoffswitch-label:before {
    text-align: center;
}

试试here

答案 2 :(得分:0)

使用line-height属性:

.onoffswitch-label:before {
    text-align: center;
    line-height: 32px;
}

或者是flexbox:

.onoffswitch-label:before {
    display: flex;
    align-items: center;
    justify-content: center;
}