html - React Native : 'top' property behaving as expected, 但 'bottom' 不是

标签 html css reactjs react-native css-position

我的 React Native 应用程序中有以下代码:

<View
  style={{
    width: 50,
    height: 50,
    borderWidth: 1,
  }}
>
  <View style={{
    width: 5,
    height: 5,
    backgroundColor: 'red',
    top: 10,
    left: 10
  }}></View>
</View>

正如预期的那样,这会导致:

enter image description here

但是,如果我将 top 换成 bottom,我会得到:

enter image description here

如果我将子元素更改为 position: absolute,它会按预期工作:

enter image description here

我想知道的:

1) 为什么第二张图中红点在黑色方 block 上方?

2) 由于红点是黑色方 block 的唯一 child ,为什么添加 position: absolute 会改变任何东西?

3) 为什么 top 在所有三幅图像中都按预期运行,而 bottom 仅在第三幅中按预期运行?

最佳答案

在 React-Native 中,每个布局元素默认都是相对定位的,所以相对于初始位置底部的 10px 布局在容器之外,这是正确的行为。

如果您想将点定位在容器的边界上,请将 child 的位置设置为绝对位置。

    <View
      style={{
        width: 50,
        height: 50,
        borderWidth: 1,
        position: 'relative' // by default anyway
      }}
    >
      <View
        style={{
          width: 5,
          height: 5,
          backgroundColor: "red",
          bottom: 10,
          left: 10,
          position: 'absolute'
        }}
      />
    </View>

关于html - React Native : 'top' property behaving as expected, 但 'bottom' 不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59615612/

相关文章:

javascript - 将复杂的 html/css/js 元素添加到单个 Wordpress 页面

html - 使用 AlphaImageLoader 应用 Sprite 图像

javascript - 如何在滚动和使用 jquery 偏移量时减少闪烁

css - 如何使用 CSS 设置水平子菜单的样式?

javascript - 将数据传递给 React 中的 "deeper down"组件

javascript - 浏览器中的 ES6 模块 : Uncaught SyntaxError: Unexpected token import

html - 在 Twitter Bootstrap 的 Carousel 中自定义 "next"和 "previous"箭头

html - 使用 alpha 继承父背景颜色会使子背景颜色不同。为什么?

reactjs - 如何配置 jest jsdom 环境?

css - Reactjs Bootstrap 网格