我正在使用React,而Negotiation
,frontend
和food
的组件具有从另一个组件传递的元素。
如何设置此样式,以便每个元素(Negotiation
,Frontend
和food
)彼此分开,但仍然在同一列中,旁边有新闻排列?
我的JavaScript:
class Course extends React.Component {
render() {
return (
<div>
<div className="coursecontent">
<h3>{this.props.coursename}</h3>
<h4> {this.props.status} {this.props.progress}</h4>
</div>
<button className="coursecontent">Start exercise</button>
</div>
);
}
}
class Welcomebox extends React.Component {
render() {
return <h1>Welcome Naomi</h1>;
}
}
ReactDOM.render(<Welcomebox />, document.getElementById('welcomebox'));
class Coursebox extends React.Component {
render() {
return (
<div className="box-field">
<Course coursename="Negotiation" progress= "20%" status="Progress"/>
<Course coursename="Frontend" progress="56%" status="Progress"/>
<Course coursename="Food" status="Progress" progress="43%"/>
</div>
);
}
}
class Newsbox extends React.Component {
render() {
return (
<div className="box-field" className="newsbox">
<h3>News</h3>
</div>
);
}
}
class Dashboardbox extends React.Component {
render() {
return (
<div className="dashboardbox">
<Coursebox />
<Newsbox />
</div>
);
}
}
ReactDOM.render(<Dashboardbox />, document.getElementById('dashboardbox'));
我的CSS:
.box-field,
.newsbox {
width: 45%;
background-color: lightgrey;
font-family: arial;
margin-left: 30px;
height: 80%;
padding: 5px 10px 10px 10px;
border-radius: 10px;
display: inline-block;
}
所以基本上,在每个Course
元素之间我想要空格(最好设置为Margin
),我希望Newsbox
组件与Coursebox
对齐成分
答案 0 :(得分:24)
将新Newsbox
组件带到Coursebox
import Coursebox from './Coursebox';
import Newsbox from './Newsbox'
class ContainerRow extends React.Component {
render(){
return (
<div className='rowC'>
<Coursebox />
<Newsbox />
</div>
);
}
}
CSS
.rowC{display:flex; flex-direction:row;}
答案 1 :(得分:0)
如果您希望课程组件之间存在样式差异,则可以在调用组件时使用props传递className。或者如果你使用bootstrap,你可以通过“well”或“panel”类。
例如;
class Course extends React.Component {
render() {
return (
<div class="panel panel-default">
<div class="panel-heading">{this.props.coursename}</div>
<div class="panel-body">
<h4> {this.props.status} {this.props.progress}</h4>
<button className="coursecontent">Start exercise</button>
</div>
</div>
</div>
);
}
}
答案 2 :(得分:0)
你在这里。
const ParentDiv = styled.div`
& {
width: 100%;
}
`;
const ChildDiv = styled.div`
& {
display: inline-block;
vertical-align: text-top;
margin: 0 auto;
}
`;