html - 使用显示 :flex 时将 div 右对齐

标签 html css flexbox

我里面有主要的 div 和 2 个 div。

为了让它们保持在同一行,我对所有 div 使用了 display: flex

但这禁止我在右边的 div 上使用 float:right

我想要的是将“我的按钮”div移动到屏幕的右侧

<div style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: gray;height:30px;">
  <div style="float:left;line-height:30px;">Contact Details</div>
  <button type="button" class="edit_button" style="float: right;">My Button</button>
</div>

这是 fiddle : http://jsfiddle.net/xQgSm/

最佳答案

你只需要使用 flexbox 中可用的对齐属性

.container {
  border-bottom-width: 1px;
  border-bottom-style: solid;
  border-bottom-color: gray;
  height: 30px;
  display: flex;
  align-items: center; /* center vertically */
  justify-content: space-between; /* maximum space between items*/
}
<div class="container">
  <div>Contact Details</div>

  <button type="button" class="edit_button">My Button</button>

</div>

或者..

.container {
  border-bottom-width: 1px;
  border-bottom-style: solid;
  border-bottom-color: gray;
  height: 30px;
  display: flex;
  align-items: center;
}

.edit_button {
  margin-left: auto;
  /* align to right */
}
<div class="container">
  <div>Contact Details</div>

  <button type="button" class="edit_button">My Button</button>

</div>

关于html - 使用显示 :flex 时将 div 右对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45881495/

相关文章:

javascript - list 应用程序一次添加三项而不是一项

javascript - 如何将错误标签放入程式化的 div 中?

javascript - 是否可以在 Ext.grid.column.Action 中垂直对齐

javascript - 相对于起始拖动位置跟踪鼠标 HTML5、Ember

html - 有没有办法创建没有绝对位置的水平子导航?

javascript - JQuery .hover() 和 .bind() 试图控制动画行为

php - 数据库不会使用 php 表单更新值

html - 如何摆脱 Chromium Flexbox 中不需要的空间?

html - 当上面有多行文本时如何对齐多个div

css - 怎么修? IE、flexbox/grid 和 img 显示内联