简体   繁体   English

CSS Flex 属性不适用于我的 React 组件

[英]CSS Flex property not working with my React Component

I'm working with a React project that requires me to use Flex in my styling.我正在处理一个需要我在样式中使用 Flex 的 React 项目。 All have done is just not working have used almost all the flex property and yet it seems not to be working.所做的一切只是不起作用,几乎使用了所有 flex 属性,但它似乎不起作用。 My expected output is the image below我的预期输出是下图在此处输入图片说明

But this is what I'm getting below但这就是我在下面得到的在此处输入图片说明

Also, my Barchart Component is this另外,我的条形图组件是这样的

import React from "react";
const tenHighestPopulation = [
  { country: "World", population: 7693165599, percentage: 0 },
  { country: "China", population: 1377422166, percentage: 0 },
  { country: "India", population: 1295210000, percentage: 0 },
  { country: "USA", population: 323947000, percentage: 0 },
  { country: "Indonesia", population: 258705000, percentage: 0 },
  { country: "Brazil", population: 206135893, percentage: 0 },
  { country: "Pakistan", population: 194125062, percentage: 0 },
  { country: "Nigeria", population: 186988000, percentage: 0 },
  { country: "Bangladesh", population: 161006790, percentage: 0 },
  { country: "Russian", population: 146599183, percentage: 0 },
  { country: "Japan", population: 126960000, percentage: 0 },
];
export default function Bargroup() {

  for (var i = 0; i < tenHighestPopulation.length; i++) {
    const max = Math.max.apply(
      Math,
      tenHighestPopulation.map(function (o) {
        return o.population;
      })
    );
    // console.log(max);
    // we do the conversion here
    tenHighestPopulation[i].percentage =
      Math.round((tenHighestPopulation[i].population / max) * 100) + "%";
  }

  return (
    <div className="percent" >
      {tenHighestPopulation.map((countries, index) => (
        <div key={index}  className="details">
          <div className="country"> {countries.country}</div>
       <div className="content"  style={{
              backgroundColor: "yellow",
              width: `${countries.percentage}`,
            }}></div> 
          <div className="population"> {countries.population}</div>
        </div>
      ))}
    </div>
  );
}

And my style sheet is this我的样式表是这样的

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body{
   
    font-family: 'Montserrat';
    /* display: flex;
    justify-content: center;
    text-align: center; */
    height: 100vh;
    width: 100vw;
}
ul li{
    list-style: none;
}

.background_div{
   width: 100px;
   height: 100px;
}
.hexa_center{
    padding-top: 40px; 
}


.percent{
display: flex;
flex-direction: column;


}

.details{
 display: flex;
justify-content: flex-start;   
margin: 10px;
   
   }
   .country{
    
   }
   .content{
    
   }
   .population{
    
   }

App.js应用程序.js

import React from "react";
import Numbers from "./Numbers";
import Hexa from "./Hexa";
import "./Style.css";
import Bargroup from "./Bargroup";

export default function App() {
  // const numbers = [1]

  const numbers = [];
  for (let i = 0; i <= 31; i++) {
    numbers.push(i);
    if (i % 2 === 0) {
    }
  }

  const hexaColor = () => {
    let str = "0123456789abcdef";
    let color = "";
    for (let i = 0; i < 6; i++) {
      let index = Math.floor(Math.random() * str.length);
      color += str[index];
    }
    return "#" + color;
  };

  return (
    <div className="container">
      <div className="child">
        <div className="child_content">
          <h1> React</h1>
          <h2></h2>
        </div>
        <div className="ul">
          <ul>
            <Numbers className="block" numbers={numbers} />
          </ul>
        </div>

        <div className="child_content">
          <h1> React</h1>
          <h2> Colors</h2>
        </div>
        {new Array(32).fill(0).map((item, i) => {
          return <Hexa key={i} hexaColor={hexaColor} className="block" />;
        })}

        <div className="child_content">
          <h1> React</h1>
          <h2> Population</h2>
          <h3>Ten Most Populated Countries</h3>
          <Bargroup />
        </div>
      </div>
     
    </div>
  );
}

I can not see your imported sheet in main file.我在主文件中看不到您导入的工作表。 Under:在下面:

import React from "react";

Put:放:

import './pathToYourCss.css';

It's not so much the list that needs to be in a flex box (column), but the list item:需要在弹性框(列)中的列表与其说是列表,还不如说是列表项:

display: flex; flex-flow: row nowrap; .details, .population: flex-shrink: 0; content-wrapper: flex-grow: 2;

This should get you close.这应该让你接近。 Cheers干杯

Try to use this for the count element:尝试将其用于计数元素:

 Flex: 1; Justify-self: end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM