【问题标题】:Make cards and images the same size - how can I do it with CSS?使卡片和图像大小相同 - 我如何使用 CSS 做到这一点?
【发布时间】:2018-03-16 15:25:46
【问题描述】:

我正在使用reactMaterial-ui 用于 Cards。对于Grid,我使用的是 CSS 网格布局。到目前为止,它看起来像这样:

但我的目标是这样的:

我有两个问题:

  1. 我想让所有这些cards 具有相同的高度(415 像素)。我在.BeerListingScroll-info-box 上尝试了 height: 415px 但它不起作用。

  2. 瓶子和小桶的图像大小不同 [小桶 (80px x 160px) 与瓶子 (80px x 317px)]。有什么方法可以让它们在渲染大小上更相似?

-

代码:

BeerListingScroll

import React, { Component } from 'react';
import ReduxLazyScroll from 'redux-lazy-scroll';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { fetchBeers } from '../../actions/';

import BeersListItem from '../../components/BeersListItem';
import ProgressIndicator from '../../components/ProgressIndicator';

import './style.css';

class BeerListingScroll extends Component {
  constructor(props) {
    super(props);

    this.loadBeers = this.loadBeers.bind(this);
  }

  loadBeers() {
    const { skip, limit } = this.props.beers;
    this.props.fetchBeers(skip, limit);
  }

  render() {
    const { beersArray, isFetching, errorMessage, hasMore } = this.props.beers;
    return (
      <div className="container beers-lazy-scroll">
        <ReduxLazyScroll
          isFetching={isFetching}
          errorMessage={errorMessage}
          loadMore={this.loadBeers}
          hasMore={hasMore}
        >
          <div className="BeerListingScroll-wrapper">
            {beersArray.map(beer => (
              <div key={beer.id} className="BeerListingScroll-info-box">
                <BeersListItem beer={beer} />
              </div>
            ))}
          </div>
        </ReduxLazyScroll>
        <div className="row beers-lazy-scroll__messages">
          {isFetching && (
            <div className="alert alert-info">
              <ProgressIndicator />
            </div>
          )}

          {!hasMore &&
            !errorMessage && (
              <div className="alert alert-success">
                All the beers has been loaded successfully.
              </div>
            )}
        </div>
      </div>
    );
  }
}

function mapStateToProps(state) {
  return {
    beers: state.beers,
  };
}

function mapDispatchToProps(dispatch) {
  return bindActionCreators({ fetchBeers }, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(BeerListingScroll);

BeerListingScroll css

.BeerListingScroll-wrapper {
  display: grid;
  margin: 0;
  grid-gap: 10px;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr) ) ;
  background-color: #f7f7f7;
}

.BeerListingScroll-info-box {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  color: #fff;
  border-radius: 5px;
  padding: 20px;
  font-size: 150%;
  width: 320px;
}


/* This applies from 600px onwards */
@media (min-width: 1820px) {
  .BeerListingScroll-wrapper {
    margin: 0 400px;
  }
}

@media (min-width: 1620px) {
  .BeerListingScroll-wrapper {
    margin: 0 300px;
  }
}

@media (min-width: 1366px) {
  .BeerListingScroll-wrapper {
    margin: 0 200px;
  }
}

BeerListItem 是 BeerListingScroll 的子级

import React from 'react';
import Card, { CardContent } from 'material-ui/Card';
import Typography from 'material-ui/Typography';

function BeerListItem(props) {
  return (
    <div>
      <Card raised>
        <CardContent>
          <img src={props.beer.image_url} alt="beer" width="30%" />
          <Typography variant="headline" component="h2">
            {props.beer.name}
          </Typography>
          <Typography component="p">{props.beer.tagline}</Typography>
        </CardContent>
      </Card>
    </div>
  );
}

export default BeerListItem;

github 上的完整项目 -> Github

【问题讨论】:

  • 这可能只是一个 HTML + CSS 问题——您能否也包括在网页上生成的渲染 HTML 代码?
  • 但是真的--找到那个是白色背景的元素,然后把div的高度设置成那个--如果它不会调整,用web开发者工具栏看看有没有样式覆盖你的意图
  • 使用 flexbox - 等高行的理想选择。但由于您还没有创建minimal reproducible example,我投票关闭
  • 您遇到了艺术品问题!每当您更改纵横比以使图像缩放时,您都会遇到图像失真的问题,为了获得真正专业的外观,您需要重构艺术品,因此图像是统一的。
  • 最好的办法是使用 CSS background。它提供了cover 和其他一些不错的、简单的功能。请参阅 MDN 的 Scaling background images

标签: javascript reactjs css material-ui


【解决方案1】:

所以对于图像大小,我得到了很好的answer

我补充说:

.BeerListItem-img {
  height: auto;
  max-height: 250px;
  width: auto;
  max-width: 250px;
}

对于卡片大小,我刚刚在BeerListItem 类中添加了Card,就像这样(.BeerListItem-main-card):

function BeerListItem(props) {
  return (
    <div>
      <Card raised className="BeerListItem-main-card">
        <CardContent>
          <img
            src={props.beer.image_url}
            alt="beer"
            className="BeerListItem-img"
          />
          <Typography variant="headline" component="h2">
            {props.beer.name}
          </Typography>
          <Typography component="p">{props.beer.tagline}</Typography>
        </CardContent>
      </Card>
    </div>
  );
}

这里是那个组件对应的css。

.BeerListItem-main-card {
  width: 320px;
  height: 415px;
}

.BeerListItem-img {
  height: auto;
  max-height: 250px;
  width: auto;
  max-width: 250px;
}

通过这两个更改,我已经成功实现了我的目标。

【讨论】:

    【解决方案2】:

    你应该尝试探索 display:flex;

    这里是一个很棒的代码笔的链接,它可以帮助你实现你想要的:

    https://codepen.io/enxaneta/full/adLPwv

    更具体地说,这里是我创建的一个示例,其中包含您可能想要实现的目标。

    https://jsfiddle.net/dalecarslaw/sxdr3eep/

    以下是您应该关注的代码领域:

      display:flex;
      align-items:space-between;
      justify-content:space-between;
      flex-wrap:wrap;
    

    【讨论】:

      猜你喜欢
      • 2018-12-02
      • 2023-01-30
      • 2023-03-08
      • 2021-01-30
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多