我正在寻找一个常见用例的Flexbox布局最佳实践解决方案.

example

在本例中,我想使用Flexbox使分数向右浮动.我想用:

position: absolute;
right: 0;

但问题是我们不能使用中心对齐外盒.

我能想到的另一种方法是制作另一个flex box来包装图像并命名零件,然后创建一个外部flex box来使用

justifyContent: space-between;

以制作预期的布局.

推荐答案

使用justify-content: space-between它会将元素推到侧面:

.flex {
 display: flex;
 justify-content: space-between;
}

.flex-grow {
  flex-grow: 1;
}

.one {
  border: 1px solid black;
  width: 20px; height: 20px;
}

.two {
  border: 1px solid black;
  width: 20px; height: 20px;
}

.three {
  border: 1px solid black;
  width: 20px; height: 20px;
}
Growing middle element
<div class="flex">
  <div class="one"></div><div class="two flex-grow"></div><div class="three"></div>
</div>

Without growing element
<div class="flex">
  <div class="one"></div><div class="two"></div><div class="three"></div>
</div>

Without middle element
<div class="flex">
  <div class="one"></div><div class="three"></div>
</div>

请参阅本精彩教程,轻松了解flex box的行为:https://css-tricks.com/snippets/css/a-guide-to-flexbox/

React-native相关问答推荐

RTK 查询显示互联网何时关闭?

打开屏幕后自动显示键盘

找不到变量:firebase/react native(expo)上的 IDBIndex

_react2.default.PropTypes.function 未定义

React Native Ios错误 - ENOENT:no such file or directory, uv_cwd (null)

No component found for view with name "ARTShape"

为什么会出现此错误:Invariant Violation: Cannot update during an existing state transition

为了回调 URL 的目的,在 Expo 中运行的 React Native 元素的 info.plist 在哪里?

React-Native:显示加载屏幕直到加载 webview

修复错误:路由 'Home' 的组件必须是 React 组件

React native Refresh 有效,但下一次调用仍使用最后一个令牌

使用像 React Navigation 这样的基于 JS 的导航解决方案优缺点?

如何判断 React Native 中的 AsyncStorage 中是否存在密钥? getItem() 总是返回一个promise对象

无法解析模块 react/lib/ReactUpdates

使用离线包在 iOS 设备上运行 react-native 应用程序

加载远程图像失败后加载本map像

运行时尚未准备好进行调试

对未计算文件的本机Bundle sha-1 做出react(无法从assets index.android.bundle 加载脚本)

如何在 react-native 中重叠

发布未使用 JavaScript 代码更新的 APK