div内的左/右浮动按钮

Moh*_*ril 10 html css

我怎样才能让按钮浮动只在div区域?

这是我的示例css和html ..

HTML

<div class='test'>
  <div style='float: left;'>
    <button>test</button>
  </div>
  <div style='float: right;'>
    <button>test</button>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS

.test {
  width:60%;
  display:inline;
  overflow: auto;
  white-space: nowrap;
  margin:0px auto;
}
Run Code Online (Sandbox Code Playgroud)

或者你可以在这里看到现场样本 - > http://jsfiddle.net/ELAVS/

我希望它像这样..

在此输入图像描述

Ahm*_*Din 20

更改显示:内联显示:内联块

.test {
  width:200px;
  display:inline-block;
  overflow: auto;
  white-space: nowrap;
  margin:0px auto;
  border:1px red solid;
}
Run Code Online (Sandbox Code Playgroud)


Pen*_*Liu 15

您可以使用justify-content: space-between.test像这样:

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


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

div {
  width: 20rem;
  border: .1rem red solid;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)