css - react : Relative positioning of two divs within div

标签 css reactjs

我有一个容器 div,其中包含另外两个 div。我希望容器中的两个 div 分别距离容器 div 的左侧 10% 和右侧 10%。但是,只有左边的做我想要的,而右边的没有。为什么会这样?

App.js

import React, { Component } from "react";
import logo from "./logo.svg";
import "./App.css";

class App extends React.Component {
  render() {
    return (
      <div className="screenDiv">
        <div className="topContainer" />
        <div className="botContainer">
          <div className="LeftDiv" />
          <div className="RightDiv" />
        </div>
      </div>
    );
  }
}

export default App;

应用.css

.screenDiv {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.topContainer {
  width: 100%;
  height: 80%;
  background-color: #c6e2f4;
}

.botContainer {
  width: 100%;
  height: 20%;
  background-color: #f9aff9;
}

.LeftDiv {
  width: 5%;
  height: 20%;
  position: relative;
  top: 50%;
  left: 10%;
  background-color: green;
}

.RightDiv {
  width: 5%;
  height: 20%;
  position: relative;
  top: 50%;
  right: 10%;
  background-color: #ff0000;
}

最佳答案

我们可以为此使用 flexbox。 我们只需要在你的 css 中修改这个类,其余的将正常工作 -:

.botContainer {
  width: 100%;
  height: 20%;
  background-color: #f9aff9;
  display: flex;
  justify-content: space-between;
}

这是一个工作演示 -> https://stackblitz.com/edit/react-kuusz8

关于css - react : Relative positioning of two divs within div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59353246/

相关文章:

html - 带有垂直子/下拉菜单的水平 CSS 菜单。请帮助

css - reactJS SCSS 背景图像不可见

javascript - 检查某个类的对象的值是否已被更改

html - 背景图片属性

html - 如何在图像上叠加 Logo ?使用 CSS

javascript - 如何在 next.js 中实现带重定向的链接?

javascript - react 未处理的拒绝(不变违规)

javascript - 将动态输入表单中的数据存储到对象(React)

reactjs - Redux-observable:史诗般的 Jest 测试失败

html - HTML 文本对齐困难