【问题标题】:How to fix layering issue with React, CSS and Flexbox如何解决 React、CSS 和 Flexbox 的分层问题
【发布时间】:2018-12-27 01:42:41
【问题描述】:

我试图将两个容器层叠在一起,但它被放置在一列中

我研究了 z-index 和分层,但它似乎不起作用。

我的代码: 类 App 扩展组件 {

渲染() {

let bigContainer = {
  height: '30vh',
}

let containerOne = {
  height:'100%',
  zIndex:1,
  position: 'relative'
}

let containerTwo = {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    zIndex: 100,
    height: '100%',
    position: 'relative'
}

let innerStyleOne = {
    backgroundColor: 'black',
    height: '15vh',
    position: 'relative',
    order: '2',
    width: '100%'
}

let innerStyleTwo = {
    backgroundColor: 'blue',
    height: '15vh',
    position: 'relative',
    order: '2',
    width: '100%'
}

let innerStyleLayer = {
    backgroundColor: 'green',
    position: 'relative',
    height: '25px',
    width: '25px'
}

return (
  <div className="App" style={bigContainer}>
    <div style = {containerOne}>
      <div style={innerStyleOne}> </div>
      <div style={innerStyleTwo}> </div>
    </div>
    <div style = {containerTwo}>
      <div style={innerStyleLayer}></div>
    </div>
  </div>
);

} }

这是我得到的结果

但我试图让绿色方块位于蓝色和黑色条的中间

编辑:将 container2 更改为 position:absolute 后

【问题讨论】:

    标签: css reactjs flexbox


    【解决方案1】:

    使用 位置:相对于 bigContainer 和 位置:containerTwo 的绝对位置。

    【讨论】:

    • 我将位置更改为绝对位置,但它只是移到一边,我添加了一个显示发生了什么的编辑
    • class containerTwo using: top: 50%;左:50%;变换:翻译(-50%,-50%)
    • 这行得通,但为什么我们必须添加顶部、左侧和变换?
    • 对不起。我不太会说英语。你可以在这里看到更多w3schools.com/csSref/pr_class_position.asp
    猜你喜欢
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2021-12-28
    • 2021-08-19
    • 1970-01-01
    • 2021-07-26
    相关资源
    最近更新 更多