是什么导致<div>比孩子高?

时间:2017-03-23 00:07:18

标签: css css3

是什么导致div .btn-group比其所有子元素更高?

有关应用的CSS和可视化演示,请参阅JSFiddle

<div class="top-box p-sm">
  <div role="toolbar" class="btn-toolbar">
    <div class="btn-group red">
      <button type="button" class="btn btn-default">Save</button>
      <div class="tooltip-button-helper" aria-describedby="ttRename">
        <button type="button" class="btn btn-default"
          style="pointer-events: none;">Rename</button>
      </div>
    </div>
  </div>
</div>

&#13;
&#13;
.btn-group {
  background-color: red;
}

.tooltip-button-helper {
  display: inline-block;
  cursor: not-allowed;
}

// fix styling of buttons in ButtonGroups and ButtonToolbars which got broken by inserting another div
.btn-toolbar>.tooltip-button-helper>.btn {
  margin-left: 5px;
}

.btn-group>.tooltip-button-helper:first-child:not(:last-child)>.btn:not(.dropdown-toggle) {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}

.btn-group>.tooltip-button-helper:last-child:not(:first-child)>.btn,
.btn-group>.tooltip-button-helper:not(:first-child)>.dropdown-toggle {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}

.btn-group .btn+.tooltip-button-helper>.btn,
.btn-group .tooltip-button-helper>.btn+.btn-group,
.btn-group .tooltip-button-helper>.btn-group+.btn,
.btn-group .tooltip-button-helper>.btn-group+.btn-group {
  margin-left: -1px;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="top-box p-sm">
  <div role="toolbar" class="btn-toolbar">
    <div class="btn-group">
      <button type="button" class="btn btn-default">Save</button>
      <div class="tooltip-button-helper" aria-describedby="ttRename">
        <button type="button" class="btn btn-default" style="pointer-events: none;">Rename</button>
      </div>
    </div>
  </div>
</div>
What causes the div .btn-group (with the dark red background) to be taller than its child elements?
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:4)

它是inline-block元素的垂直对齐方式。如果将它们设置为vertical-align: top,它将摆脱底部的空间。 https://jsfiddle.net/jdn58g40/3/

&#13;
&#13;
.btn-group {
  background-color: red;
}

.tooltip-button-helper {
  display: inline-block;
  cursor: not-allowed;
}

// fix styling of buttons in ButtonGroups and ButtonToolbars which got broken by inserting another div
.btn-toolbar>.tooltip-button-helper>.btn {
  margin-left: 5px;
}

.btn-group>.tooltip-button-helper:first-child:not(:last-child)>.btn:not(.dropdown-toggle) {
  border-top-right-radius: 0;
  border-bottom-right-radius: 0;
}

.btn-group>.tooltip-button-helper:last-child:not(:first-child)>.btn,
.btn-group>.tooltip-button-helper:not(:first-child)>.dropdown-toggle {
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
}

.btn-group .btn+.tooltip-button-helper>.btn,
.btn-group .tooltip-button-helper>.btn+.btn-group,
.btn-group .tooltip-button-helper>.btn-group+.btn,
.btn-group .tooltip-button-helper>.btn-group+.btn-group {
  margin-left: -1px;
}

button, .tooltip-button-helper {
  vertical-align: top !important;
}
&#13;
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="top-box p-sm">
  <div role="toolbar" class="btn-toolbar">
    <div class="btn-group">
      <button type="button" class="btn btn-default">Save</button>
      <div class="tooltip-button-helper" aria-describedby="ttRename">
        <button type="button" class="btn btn-default" style="pointer-events: none;">Rename</button>
      </div>
    </div>
  </div>
</div>
What causes the div .btn-group (with the dark red background) to be taller than its child elements?
&#13;
&#13;
&#13;