按钮元素内的块元素意外发生

时间:2015-06-22 22:43:43

标签: css

我正在尝试创建一个按钮,在等待响应时显示加载微调器。但是有一些奇怪的事情发生在我根本不理解的地方。

我有以下带有一堆CSS的HTML:

<button type="submit" disabled="true" class="btn btn-blue btn-loading">
  <div class="btn-loading-text">Update profile</div>
  <div class="btn-loading-spinner"></div>
</button>

如果你注释掉微调元素,那么“更新配置文件”会在中心对齐,即使我没有要求它。

* {
  box-sizing: border-box;
}
.btn-loading {
  border-radius: 2px;
  font-size: 13px;
  font-family: arial, helvetica, sans-serif;
  text-decoration: none;
  display: inline-block;
  font-weight: bold;
  outline: 0;
  background: #f5f5f5 !important;
  border: 1px solid #ddd !important;
  color: #aaa !important;
  cursor: default !important;
  overflow: hidden;
  height: 40px;
}
.btn-loading-text {
  float: left;
  margin: 0px 15px 0px 15px;
}
.btn-loading-spinner {
  float: left;
  height: 25px;
  width: 25px;
  margin: 7px 15px 6px -5px;
  position: relative;
  animation: rotation .9s infinite linear;
  border-left: 3px solid #ddd;
  border-right: 3px solid #ddd;
  border-bottom: 3px solid #ddd;
  border-top: 3px solid #aaa;
  border-radius: 100%;
}
@keyframes rotation {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(359deg);
  }
}
<button type="submit" disabled="true" class="btn-loading">
  <div class="btn-loading-text">Update profile</div>
  <!--<div class="btn-loading-spinner"></div>-->
</button>

但是当旋转器元素出现时,它会突然出现在顶部。我不知道发生了什么。

* {
  box-sizing: border-box;
}
.btn-loading {
  border-radius: 2px;
  font-size: 13px;
  font-family: arial, helvetica, sans-serif;
  text-decoration: none;
  display: inline-block;
  font-weight: bold;
  outline: 0;
  background: #f5f5f5 !important;
  border: 1px solid #ddd !important;
  color: #aaa !important;
  cursor: default !important;
  overflow: hidden;
  height: 40px;
}
.btn-loading-text {
  float: left;
  margin: 0px 15px 0px 15px;
}
.btn-loading-spinner {
  float: left;
  height: 25px;
  width: 25px;
  margin: 7px 15px 6px -5px;
  position: relative;
  animation: rotation .9s infinite linear;
  border-left: 3px solid #ddd;
  border-right: 3px solid #ddd;
  border-bottom: 3px solid #ddd;
  border-top: 3px solid #aaa;
  border-radius: 100%;
}
@keyframes rotation {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(359deg);
  }
}
<button type="submit" disabled="true" class="btn-loading">
  <div class="btn-loading-text">Update profile</div>
  <div class="btn-loading-spinner"></div>
</button>

1 个答案:

答案 0 :(得分:1)

按钮元素的内容与中间垂直对齐。

当你只有.btn-loading-text时,该元素高16像素,按钮高38像素,所以.btn-loading-text与中间对齐。

但是,当你还包括高度为38px的.btn-loading-spinner(包括边框和边距)时,按钮的内容与最高的元素一样高,所以38px。所以与中间的对齐并不明显。

如果您想将每个元素与中间对齐,而不是将内容整体对齐,则可以使用display: inline-block代替float: leftvertical-align: middle

.btn-loading-text, .btn-loading-spinner {
  float: none; /* Initial value */
  display: inline-block;
  vertical-align: middle;
}

&#13;
&#13;
* {
  box-sizing: border-box;
}
.btn-loading {
  border-radius: 2px;
  font-size: 13px;
  font-family: arial, helvetica, sans-serif;
  text-decoration: none;
  display: inline-block;
  font-weight: bold;
  outline: 0;
  background: #f5f5f5 !important;
  border: 1px solid #ddd !important;
  color: #aaa !important;
  cursor: default !important;
  overflow: hidden;
  height: 40px;
}
.btn-loading-text, .btn-loading-spinner {
  display: inline-block;
  vertical-align: middle;
}
.btn-loading-text {
  margin: 0px 15px 0px 15px;
}
.btn-loading-spinner {
  height: 25px;
  width: 25px;
  margin: 7px 15px 6px -5px;
  position: relative;
  animation: rotation .9s infinite linear;
  border-left: 3px solid #ddd;
  border-right: 3px solid #ddd;
  border-bottom: 3px solid #ddd;
  border-top: 3px solid #aaa;
  border-radius: 100%;
}
@keyframes rotation {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(359deg);
  }
}
&#13;
<button type="submit" disabled="true" class="btn-loading">
  <div class="btn-loading-text">Update profile</div>
  <div class="btn-loading-spinner"></div>
</button>
&#13;
&#13;
&#13;