[英]How to make flex-direction: column; properly. My code doesn't work
I'm making an ul tag inside a div, and I want them to be under each other.我在 div 中制作了一个 ul 标签,我希望它们在彼此之下。
I couldn't think of any other way to do it than this:除了这个,我想不出任何其他方法来做到这一点:
This is my CSS :这是我的CSS :
.bulletpoint-wrapper {
height: 170px;
width: 685px;
background: #04b4f4;
display: flex;
flex-flow: column wrap;
}
.bulletpoints {
color: #001c42;
list-style: none;
margin: auto;
}
.bullets {
height: 25px;
width: 25px;
}
...and this is my JSX : ...这是我的JSX :
import React from 'react'
import 'components/layout/TextMedia.css'
const Bulletpoints = props => {
return props.bulletpoints.map((bullet, index) => (
<div className="bulletpoint-wrapper">
<ul key={index} class="bulletpoints">
<li>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 50 50"
className="bullets"
>
<path d="M14.402 4C8.666 4 4 8.666 4 14.402v21.196C4 41.334 8.666 46 14.402 46h21.196C41.334 46 46 41.334 46 35.598V14.402C46 8.666 41.333 4 35.596 4H14.402zm2.125 9h16.946A3.531 3.531 0 0137 16.527v16.946A3.532 3.532 0 0133.47 37H16.528A3.531 3.531 0 0113 33.473V16.527A3.531 3.531 0 0116.527 13zM19 19v12h12V19H19z" />
</svg>
{bullet.text}
</li>
</ul>
</div>
));
}
export default Bulletpoints
Wrap your whole .map()
function with a div
, and make it flexbox
, and then set flex-direction
to column
.用
div
包裹整个.map()
函数,并使其成为flexbox
,然后将flex-direction
设置为column
。 Lastly, delete margin:auto;
最后,删除
margin:auto;
Something like this should work:这样的事情应该工作:
const Bulletpoints = props => {
return (
<div style={{ display: "flex", flexDirection: "column" }}>
{props.bulletpoints.map((bullet, index) => (
<div className="bulletpoint-wrapper">
<ul key={index} class="bulletpoints">
<li>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 50 50"
className="bullets"
>
<path d="M14.402 4C8.666 4 4 8.666 4 14.402v21.196C4 41.334 8.666 46 14.402 46h21.196C41.334 46 46 41.334 46 35.598V14.402C46 8.666 41.333 4 35.596 4H14.402zm2.125 9h16.946A3.531 3.531 0 0137 16.527v16.946A3.532 3.532 0 0133.47 37H16.528A3.531 3.531 0 0113 33.473V16.527A3.531 3.531 0 0116.527 13zM19 19v12h12V19H19z" />
</svg>
{bullet.text}
</li>
</ul>
</div>
))}
</div>
);
};
Why do you use flex at all?你为什么要使用 flex? A div usually loads downwards (default behavior is downwards).
div 通常向下加载(默认行为是向下)。
Change .bulletpoint
to ul
might solve your problem.将
.bulletpoint
更改为ul
可能会解决您的问题。
You code worked as-is, but the rendered HTML should repeat <li>
not the whole <ul><li>... </li></ul>
block您的代码按原样工作,但呈现的 HTML 应该重复
<li>
而不是整个<ul><li>... </li></ul>
块
relevant JSX / ReactJS :相关的JSX / ReactJS :
const Bulletpoints = props => {
return (
<>
<div className="bulletpoint-wrapper">
<ul class="bulletpoints">
{props.bulletpoints.map((bullet, index) => (
<li key={index}>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 50 50"
className="bullets"
>
<path d="M14.402 4C8.666 4 4 8.666 4 14.402v21.196C4 41.334 8.666 46 14.402 46h21.196C41.334 46 46 41.334 46 35.598V14.402C46 8.666 41.333 4 35.596 4H14.402zm2.125 9h16.946A3.531 3.531 0 0137 16.527v16.946A3.532 3.532 0 0133.47 37H16.528A3.531 3.531 0 0113 33.473V16.527A3.531 3.531 0 0116.527 13zM19 19v12h12V19H19z" />
</svg>
{bullet.text}
</li>
))}
</ul>
</div>
</>
);
};
working stackblitz here在这里工作堆栈闪电战
You need your map to be inside the ul as you only want one ul tag.您需要将地图放在 ul 内,因为您只需要一个 ul 标签。
<div className="bulletpoint-wrapper">
<ul className="bulletpoints">
{props.bulletpoints.map((bullet, index) => (
<li key={index}>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 50 50"
className="bullets"
>
<path d="M14.402 4C8.666 4 4 8.666 4 14.402v21.196C4 41.334 8.666 46 14.402 46h21.196C41.334 46 46 41.334 46 35.598V14.402C46 8.666 41.333 4 35.596 4H14.402zm2.125 9h16.946A3.531 3.531 0 0137 16.527v16.946A3.532 3.532 0 0133.47 37H16.528A3.531 3.531 0 0113 33.473V16.527A3.531 3.531 0 0116.527 13zM19 19v12h12V19H19z" />
</svg>
{bullet.text}
</li>
))}
</ul>
</div>
You should also remove flex and flex-flow您还应该删除 flex 和 flex-flow
Check out this sandbox: https://codesandbox.io/s/react-example-vgtxq看看这个沙箱: https : //codesandbox.io/s/react-example-vgtxq
Inside of .map()
you are repeating the whole <ul>
tag, instead of just a loop for the <li>
tag.You can use the below css to achieve your design.在
.map()
您正在重复整个<ul>
标签,而不仅仅是<li>
标签的循环。您可以使用下面的 css 来实现您的设计。
You can check below code and this working stackblitz demo.您可以查看下面的代码和这个有效的stackblitz演示。
CSS CSS
.bulletpoint-wrapper {
width: 100%;
background: #04b4f4;
}
.bulletpoints li {
display: inline-flex;
width: 100%;
margin-top: 5px
}
.bulletpoints {
color: #001c42;
list-style: none;
margin: auto;
padding: 15px 20px;
}
.bullets {
height: 25px;
width: 25px;
margin-right: 15px;
}
React Component反应组件
const Bulletpoints = props => {
return (
<div className="bulletpoint-wrapper">
<ul class="bulletpoints">{
props.bulletpoints.map((bullet, index) => (
<li key={index}>
<svg
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
viewBox="0 0 50 50"
className="bullets">
<path d="M14.402 4C8.666 4 4 8.666 4 14.402v21.196C4 41.334 8.666 46 14.402 46h21.196C41.334 46 46 41.334 46 35.598V14.402C46 8.666 41.333 4 35.596 4H14.402zm2.125 9h16.946A3.531 3.531 0 0137 16.527v16.946A3.532 3.532 0 0133.47 37H16.528A3.531 3.531 0 0113 33.473V16.527A3.531 3.531 0 0116.527 13zM19 19v12h12V19H19z" />
</svg>
{bullet.text}
</li>))}
</ul></div>)
}
class App extends Component {
constructor() {
super();
this.state = {
bullet: [{
text: 'A'
}, {
text: 'B'
}, {
text: 'C'
}]
};
}
render() {
return <Bulletpoints bulletpoints={this.state.bullet} />
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.