首页 > 解决方案 > 运行 React 和 Bootstrap 的 CSS 使卡片具有相同的高度

问题描述

我在 React 中使用 Bootstrap 4。目前,第 1 项的卡片比第 2 项短,但我希望两张卡片的高度相同。

我预计将来会在底部添加更多卡片。

在此处输入图像描述

我的 CSS 文件:

.CountryDetail {
    width: 70%;
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    margin-left: 15%;
    margin-right: 15%;
}

.cards {
    background: #fcfcfc;
    margin: 20px 40px;
    transition: 0.4s all;
    width: 800px;
    padding: 20px;
    margin-left: auto;
    margin-right: auto;
}

.icon-size {
    font-size: 50px;
}

.shadow-1 {
    position: relative;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 0;
    box-sizing: border-box;
    box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.13);
}

.shadow-1:hover {
    z-index: 1;
    box-shadow: 0 8px 50px rgba(0, 0, 0, 0.2);
    transform: scale(1.05);
    transition: all 0.5s ease;
}

.vertical-center {
    justify-content: center;
}

.show-hover {
    opacity: 0;
    transform: translateX(-20px);
    transition: 1s all;
}

.cards:hover .show-hover {
    opacity: 1;
    transform: translateX(0px);
    color: blue;
}

.vertical-align {
    display: flex;
    align-items: center;
}

我的 JavaScript 文件:

<div className="CountryDetail">
    <div className="cards shadow-1 container">
        <div className="row vertical-align">
            <div className="col-2 text-center">
                <FontAwesomeIcon className="icon-hover icon-size" icon={faPlane} color="#A9A9A9" />
            </div>
            <div className="col-8">
                <h3>Item 1</h3>
            </div>
            <div className="col-2 text-center">
                <FontAwesomeIcon className="show-hover icon-size" icon={faArrowRight} color="#A9A9A9" />
            </div>
        </div>
    </div>

    <div className="cards shadow-1 container">
        <div className="row vertical-align">
            <div className="col-2 text-center">
                <FontAwesomeIcon className="icon-hover icon-size" icon={faHeart} color="#A9A9A9" />
            </div>
            <div className="col-8">
                <h3>Item 2</h3>
                <span>Under Construction...</span>
                <h2>Testing</h2>
            </div>
            <div className="col-2 text-center">
                <FontAwesomeIcon className="show-hover icon-size" icon={faArrowRight} color="#A9A9A9" />
            </div>
        </div>
    </div>
</div>

我试图在 CSS 中为cards类添加 line-height 或 height ,但它会弄乱卡片内内容的高度。

如果我通过添加最小高度来修改我的“卡片”类:

.cards {
    min-height: 200px;
    background: #fcfcfc;
    margin: 20px 40px;
    transition: 0.4s all;
    width: 800px;
    padding: 20px;
    margin-left: auto;
    margin-right: auto;
}

我会得到这个: 在此处输入图像描述

标签: cssreactjsbootstrap-4

解决方案


根据您对相同高度和居中内容的需要,将您的 CSS 更新为:

.cards {
    display: flex;
    flex-direction: column;
    justify-content: center;
    min-height: 200px;
    background: #fcfcfc;
    margin: 20px 40px;
    transition: 0.4s all;
    width: 800px;
    padding: 20px;
    margin-left: auto;
    margin-right: auto;
}

如果您将卡片设置为display flex,因为您只有一个直接子 ( row),您可以将其垂直居中。


推荐阅读