0

我正在尝试构建一个简单的卡片来显示一个按钮和一个文本。

我无法正确对齐。可能是 CSS 过载过多。

看起来像:

在此处输入图像描述

我只希望内容与文本和文本下方的按钮垂直和水平居中

这是代码:

import React from 'react';
import {Button} from 'react-bootstrap';
import './EmptyTile.css';

class EmptyTile extends React.Component {

    render() {
        return (
            <div className="empty-tile">
                <h1>{this.props.text}</h1>
                <Button bsPrefix="button-empty-tile" href={this.props.url}>Start</Button>
            </div>
        );
    }
}

export default EmptyTile;

和CSS


 .empty-tile {
    width: 240px;
    height: 360px;
    border-radius: 20px;
    box-shadow: 0px 8px 18px 0 rgba(0,0,0,0.14);
    align-items: center;
    text-align: center;
    justify-content: center ;
    display: flex;
}


.empty-tile h1 {
    font-family: Fredoka One;
    font-size: 18px;
    font-weight: normal;
    font-stretch: normal;
    font-style: normal;
    line-height: normal;
    letter-spacing: -0.44px;
    text-align: center;
    color: #cacaca;
    padding-left: 15px;
    padding-right: 15px;
}

.button-empty-tile {
    border-radius: 21px; 
    background-color: #f4f7f8; 
    border-color: #cacaca;
    font-family: Source Sans Pro;
    font-size: 16px;
    text-align: center;
    color: #cacaca;
    padding-top: 5px;
    padding-bottom: 7px;
    padding-left: 20px;
    padding-right: 20px;
}

任何想法 ?代码很简单

4

2 回答 2

3

添加flex-direction: column.empty-tite应该可以解决问题。

.empty-tile {
  width: 240px;
  height: 360px;
  border-radius: 20px;
  box-shadow: 0px 8px 18px 0 rgba(0,0,0,0.14);
  align-items: center;
  text-align: center;
  justify-content: center ;
  display: flex;
  flex-direction: column; // by default, flex-direction is row
}
...

看看这个 Code Sandbox链接,如果这是你要找的。

于 2020-07-15T17:44:35.640 回答
2

添加flex-direction: column到容器中:

 .empty-tile {
    width: 240px;
    height: 360px;
    border-radius: 20px;
    box-shadow: 0px 8px 18px 0 rgba(0,0,0,0.14);
    align-items: center;
    text-align: center;
    justify-content: center ;
    display: flex;
    flex-direction: column;
}

这里有一个片段:

 .empty-tile {
    width: 240px;
    height: 360px;
    border-radius: 20px;
    box-shadow: 0px 8px 18px 0 rgba(0,0,0,0.14);
    align-items: center;
    text-align: center;
    justify-content: center ;
    display: flex;
    flex-direction:column;
}


.empty-tile h1 {
    font-family: Fredoka One;
    font-size: 18px;
    font-weight: normal;
    font-stretch: normal;
    font-style: normal;
    line-height: normal;
    letter-spacing: -0.44px;
    text-align: center;
    color: #cacaca;
    padding-left: 15px;
    padding-right: 15px;
}

.button-empty-tile {
    border-radius: 21px; 
    background-color: #f4f7f8; 
    border-color: #cacaca;
    font-family: Source Sans Pro;
    font-size: 16px;
    text-align: center;
    color: #cacaca;
    padding-top: 5px;
    padding-bottom: 7px;
    padding-left: 20px;
    padding-right: 20px;
}
<div class="empty-tile">
<h1>I am an h1 tag!</h1>
<button class="button-empty-tile">And I am a button under it</button>
</div>

于 2020-07-15T17:38:58.010 回答