【发布时间】:2018-03-16 15:25:46
【问题描述】:
我正在使用react。 Material-ui 用于 Cards。对于Grid,我使用的是 CSS 网格布局。到目前为止,它看起来像这样:
但我的目标是这样的:
我有两个问题:
我想让所有这些
cards具有相同的高度(415 像素)。我在.BeerListingScroll-info-box上尝试了 height: 415px 但它不起作用。瓶子和小桶的图像大小不同 [小桶 (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