在div内部左/右浮动按钮

22

如何使按钮仅在 div 区域内浮动?

以下是我的 CSS 和 HTML 示例。

.test {
  width: 60%;
  display: inline;
  overflow: auto;
  white-space: nowrap;
  margin: 0px auto;
}
<div class='test'>
  <div style='float: left;'>
    <button>test</button>
  </div>
  <div style='float: right;'>
    <button>test</button>
  </div>
</div>

我希望它变成这样。

enter image description here

2个回答

29

将 display:inline 改为 display:inline-block

.test {
  width:200px;
  display:inline-block;
  overflow: auto;
  white-space: nowrap;
  margin:0px auto;
  border:1px red solid;
}

18
你可以在.test中使用justify-content: space-between,如下所示:

.test {
  display: flex;
  justify-content: space-between;
  width: 20rem;
  border: .1rem red solid;
}
<div class="test">
  <button>test</button>
  <button>test</button>
</div>


对于那些想要使用Bootstrap 4的人,可以使用justify-content-between


div {
  width: 20rem;
  border: .1rem red solid;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<div class="d-flex justify-content-between">
  <button>test</button>
  <button>test</button>
</div>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接