javascript - 我无法使用 React 引导水平显示卡片
问题描述
我正在尝试使用 React Bootstrapping 构建一个投资组合网站。我使用卡片组件来显示图像,但它们只会垂直显示。我试图让它们水平显示。我尝试使用 Card.js 文件中的一些 CSS 进行故障排除。没有任何变化。下面是我的代码。
索引.css
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New",
monospace;
}
.g-card {
margin: 20px;
}
.g-card-image {
border-radius: 10px;
width: 300px;
height: 480px;
box-shadow: 0px 0px 3px 1px #ccc;
}
.g-card-info {
margin-top: 10px;
min-height: 100px;
}
.g-card-title {
font-size: 24px;
margin: 0px;
}
.g-card-sub-title {
font-size: 16px;
margin: 0px;
}
Card.js
import React from 'react';
import CardInfo from './Cardinfo';
function Card(props) {
return (
<div className="d-inline-block g-card" >
<img className="g-card-image" src={props.item.imgSrc} alt={props.item.imgSrc} />
{ props.item.selected && <CardInfo title={props.item.title} subTitle={props.item.subTitle} link={props.item.link} /> }
</div>
)
}
export default Card;
轮播.js
import React from 'react';
import Card from './Card';
import EricDP from '../assets/images/EricDP.jpg';
import Github from '../assets/images/Github.png';
import Trailhead from '../assets/images/Trailhead.png';
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
class Carousel extends React.Component {
constructor(props) {
super(props);
this.state = {
items: [
{
id: 0,
title: 'LinkedIn',
subTitle: 'Check out my LinkedIn!',
imgSrc: EricDP,
link: '',
selected: false
},
{
id: 1,
title: 'Github',
subTitle: 'See my projects on Github, including this site!',
imgSrc: Github,
link: '',
selected: false
},
{
id: 2,
title: 'Trailhead',
subTitle: 'See my Trailhead profile and salesforce skills.',
imgSrc: Trailhead,
link: '',
selected: false
}
]
}
}
handleCardClick = (id, card) => {
let items = [...this.state.items];
items[id].selected = items[id].selected ? false : true;
items.forEach(item => {
if (item.id !== id) {
item.selected = false;
}
});
this.setState({
items
});
}
makeItems = (items) => {
return items.map(item => {
return <Card item={item} onClick={(e => this.handleCardClick(item.id, e))} key={item.id} />
})
}
render() {
return (
<Container fluid={true}>
<Row className="justify-content-around">
{this.makeItems(this.state.items)}
</Row>
</Container>
)
}
}
export default Carousel;
解决方案
我已添加display: inline-flex
到 Carousel.js 中的 Row 标记
文件。
display:inline-flex
将 flex 布局应用于 flex 项或子项以及容器本身。因此,容器就像子元素一样充当内联 flex 元素,因此仅占用其项目/子元素所需的宽度,而不是屏幕的整个宽度。
Carousel.js 代码:
<Container fluid={true}>
<Row
style={{display: "inline-flex"}}
className="justify-content-around">
{this.makeItems(this.state.items)}
</Row>
</Container>
推荐阅读
- flask - 用于生产的 K8s 上的普通 Flask 服务器与基于 UWSGI 的服务器?
- c++ - 获取/检查进程 win32 的内部 kernel32 状态(为了安全使用 TerminateThread )
- swift - 未显示归档催化剂应用程序的 os_log 消息
- python-3.x - 为什么它应该显示它属于哪个 IP 类时却显示无
- r - 有没有办法选择两个日期之间的行?
- javascript - Javascript:如何将字符串与字符串数组进行比较
- php - Symfony 3.4 - 从命令切换到动态数据库
- php - 使表格中的电子邮件地址可点击,打开发送电子邮件
- flutter - 如何在飞镖列表中的地图中添加特定键的值?
- vba - 循环访问 MS Access 子窗体并获取列可见性