如何在CSS中将此容器居中显示在屏幕中间?

ogq8wdun  于 2022-11-19  发布在  其他
关注(0)|答案(2)|浏览(136)

此CSS显示在滚动页的第二页上。.container_block用于在其框中容纳所有卡片。.cards用于显示卡片,每个单独的卡片由card 1、2、3和4表示。

.container_block {
    width: 80%;
    height: 40%;
    display: block;
    background-color: red;
    margin: auto;
}

.cards {
    display: flex;
    align-items: center;
    justify-content: center;
}

.cards h2 {
    font-size: 25px;
    color: white;
    margin: 15px;
}

.cards p {
    font-size: 20px;
    color: white;
    margin: 15px;
}

.card1 {
    box-shadow: 1px 1px 8px 6px rgba(0,0,0,0.2);
    transition: 0.3s;
    height: 30vh;
    width: 30vh;
    border-radius: 20px;
    margin-top: 4vh;
    margin-right: 5vh;
}

.card2 {
    box-shadow: 1px 1px 8px 6px rgba(0,0,0,0.2);
    transition: 0.3s;
    height: 30vh;
    width: 30vh;
    border-radius: 20px;
    margin-top: 4vh;
    margin-right: 5vh;
}

.card3 {
    box-shadow: 1px 1px 8px 6px rgba(0,0,0,0.2);
    transition: 0.3s;
    height: 30vh;
    width: 30vh;
    border-radius: 20px;
    margin-top: 4vh;
    margin-right: 5vh;
}

.card4 {
    box-shadow: 1px 1px 8px 6px rgba(0,0,0,0.2);
    transition: 0.3s;
    height: 30vh;
    width: 30vh;
    border-radius: 20px;
    margin-top: 4vh;
    margin-right: 5vh;
}

任何关于简化代码的提示也将非常感谢!

qnyhuwrf

qnyhuwrf1#

在.container_block中,尝试将“margin:0;”带“边距:0自动;”

zazmityj

zazmityj2#

只需在 Package 器中用另一个分部和名称 Package container_block分部。
应用以下CSS。
也没有必要使多个卡类,如果你是应用相同的CSS到他们只是使用一个类名在所有部门。

.wrapper{
       width:100%;
       height:100vh;
       display: flex;
       justify-content: center;
       align-items: center;
    }
    .container_block {
        width: 80%;
        height: 40%;
        display: block;
        background-color: red;
        margin: auto;
    }
    
    .cards {
        display: flex;
        align-items: center;
        justify-content: center;
    }
    
    .cards h2 {
        font-size: 25px;
        color: white;
        margin: 15px;
    }
    
    .cards p {
        font-size: 20px;
        color: white;
        margin: 15px;
    }
    
    .card1 {
        box-shadow: 1px 1px 8px 6px rgba(0,0,0,0.2);
        transition: 0.3s;
        height: 30vh;
        width: 30vh;
        border-radius: 20px;
        margin-top: 4vh;
        margin-right: 5vh;
    }

相关问题