【问题标题】:Css flex display is not aligning vertically the element inside a div [duplicate]Css flex显示未垂直对齐div内的元素[重复]
【发布时间】:2020-07-15 17:27:13
【问题描述】:

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

我无法正确对齐。可能是 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;
}

有什么想法吗?代码很简单

【问题讨论】:

    标签: javascript html css reactjs flexbox


    【解决方案1】:

    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
    }
    ...
    

    查看此代码沙箱link,如果这是您要查找的内容。

    【讨论】:

      【解决方案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;
      }
      

      这里有一个 sn-p:

       .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>

      【讨论】:

        猜你喜欢
        • 2018-09-24
        • 2012-04-02
        • 2013-04-17
        • 1970-01-01
        • 2017-09-15
        • 2013-04-11
        • 2014-11-10
        • 2011-10-04
        • 2019-03-14
        相关资源
        最近更新 更多