【发布时间】:2020-08-22 12:59:04
【问题描述】:
我在我的第一个 react 应用程序中使用 flexbox 定位我的项目/卡片时遇到了麻烦。
我在卡片中放了一个 div(参见 Card 组件),然后放了一个 display: flex;但似乎我的输出中的每张卡片都像一个块(类似列),它只弯曲我的内容,而不是卡片的每个 div,我想要的是我的 div 是一行。
我也尝试了 flex-direction,但没有任何反应,知道如何制作这些家伙吗?
//My code in my Main//
const data = [
{id: 1, t: 'Card-1', description: 'This is a Card-1'},
{id: 2, t: 'Card-2', description: 'This is a Card-2'},
{id: 3, t: 'Card-3', description: 'This is a Card-3'},
{id: 4, t: 'Card-4', description: 'This is a Card-4'},
{id: 5, t: 'Card-5', description: 'This is a Card-5'},
]
function Main () {
return (
<div>
<div className="main-container">
{data.map(d => {
return (
<Card
key = {d.id}
title = {d.t}
description =
{d.description}
/>
)
})}
</div>
</div>)
export default Main;
//end of my Main//
// My code in my Card //
import React from 'react';
import './style.css';
function Card ({ title, description }) {
return (
<div className="items">
<h2>{title}</h2>
<p>{description}</p>
</div>
)
}
export default Card;
//
//The style.css of my Card //
div.items {
display: flex;
border: 2px solid darkgreen;
margin: 15px;
max-width: 250px;
min-height: 200px;
background-color: deeppink;
flex-direction: column;
}
div.items h2 {
flex: 1;
text-align: center;
}
div.items p {
flex: 1;
text-align: center;
}
//ends here
【问题讨论】:
-
寻求代码帮助的问题必须包含最短代码以便在问题本身中包含重现它所必需的最好是Stack Snippet。见How to create a Minimal, Reproducible Example
-
...但如果您不想要列,请删除
flex-direction: column; -
如果你想让卡片彼此相邻,你需要在“行”元素(容器)上显示 flex
-
对不起,我不知道它是堆栈溢出的新功能。我把我所有的代码都放在那里,因为我认为这都是必要的。感谢您的提示!
-
当我尝试 display:flex 时它们并不相邻。
标签: javascript css reactjs flexbox css-position