首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我无法使用React bootstrapping水平显示卡片

我无法使用React bootstrapping水平显示卡片
EN

Stack Overflow用户
提问于 2021-05-20 11:09:38
回答 1查看 57关注 0票数 0

我正在尝试建立一个使用反应引导的投资组合网站。我使用了一个卡片组件来显示图像,但它们只会垂直显示。我正在尝试让它们水平显示。我尝试过在Card.js文件中使用一些CSS进行故障排除。没有任何变化。下面是我的代码。

index.css

代码语言:javascript
复制
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

代码语言:javascript
复制
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;

Carousel.js

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

发布于 2021-05-20 14:05:21

我已经在Carousel.js的Row标记中添加了display: inline-flex

文件。

display:inline-flex将flex布局应用于flex项或子项以及容器本身。因此,容器的行为就像一个内联flex元素,就像子元素一样,因此只占用它的项/子元素所需的宽度,而不是整个屏幕的宽度。

Carousel.js代码:

代码语言:javascript
复制
<Container fluid={true}>
  <Row 
    style={{display: "inline-flex"}} 
    className="justify-content-around">            
      {this.makeItems(this.state.items)}
  </Row>
</Container>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67613436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档