【问题标题】:Flexbox align center with another element in right of it [duplicate]Flexbox将中心与右侧的另一个元素对齐[重复]
【发布时间】:2018-07-11 17:51:10
【问题描述】:

我想实现看起来像的页面

它有next button,必须在页面中间的absolute position,在它旁边有skip button,文本必须以next button的文本居中。 我正在努力使用flexbox 来实现这一点,也许有人可以帮助我。 到目前为止我的代码是这样的

组件:

  <View style={{ alignSelf: 'center', position: 'absolute' }}>
    <Button
      buttonStyle={styles.nextButton}
      onPress={this._onPress}
      activeOpacity={0.9}
    >
      <Text style={styles.nextText}>NEXT</Text>
    </Button>
    <Text
      style={styles.skipButton}
      onPress={this._onSkipPress}
    >
      SKIP
    </Text>
  </View>

风格:

  nextButton: {
    backgroundColor: 'white',
    height: 52,
    width: 128,
    borderRadius: 26,
    bottom: 24,
    elevation: 1
  },
  skipButton: {
    marginRight: 20,
    fontSize: 14
  },

我知道这是react-native的代码,但是如果你能用css解决它,我相信我可以把它转换成react-native。 注意:请仅使用flexbox

【问题讨论】:

  • 分享完整代码
  • @TemaniAfif 完成,但它在 react-native

标签: css react-native flexbox


【解决方案1】:

您可以为按钮创建一个div 元素,并在其上和最后一个按钮上使用position: absolute

.page {
  height: 100vh;
  background: #4AA1CC;
  position: relative;
}
.buttons {
  position: absolute;
  left: 50%;
  bottom: 50px;
  transform: translateX(-50%);
}
button {
  color: white;
  background: none;
  border: none;
}
.buttons button:first-child {
  padding: 15px 30px;
  width: 100px;
  background: #32D6B6;
  border: 1px solid black;
  border-radius: 30px;
}
.buttons button:last-child {
  position: absolute;
  right: -60px;
  top: 50%;
  transform: translateY(-50%);
}
<div class="page">
  <div class="buttons">
    <button>Next</button>
    <button>Skip</button>
  </div>
</div>

【讨论】:

  • 下一个按钮必须在页面的中心,跳过按钮将在它的右边。
  • @2r83 查看更新。
  • 很酷,但你能用flexbox 做到吗,因为我的问题是只使用flexbox
猜你喜欢
  • 1970-01-01
  • 2019-08-21
  • 1970-01-01
  • 1970-01-01
  • 2015-09-05
  • 2015-12-08
  • 2022-06-18
  • 2010-09-20
  • 2017-09-07
相关资源
最近更新 更多