网格模板列 - 项目不在中心

grid-template-columns - items not in center

我正在尝试使用“grid-template-columns”创建我自己的网格,结果是当我调整视图项目时未居中。

我使用这个文档https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns

我被困住了,需要一些建议,我做错了什么。

P.S 在普通桌面视图中看起来不错,但在移动设备中... 下面我附上了截图和我的代码示例。

这里是我正在使用的代码示例:

<!DOCTYPE html>
<html lang="en">
    
    <head>
        <meta charset="utf-8">
        <title>Profile Card</title>
        <link rel="stylesheet" href="css/font-awesome.min.css">       
        <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700' rel='stylesheet'>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
        <style>

.my-grid {
    display: flex;
    justify-content: center;
    align-content: center;
}

.my-container{
    position: relative;
    width: 1200px;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    grid-gap: 20px;
    padding: 20px;
}

.my-container .my-card{
    width: 100%;
    background: #fff;
    border-radius: 20px;
    max-width: 250px;
    box-shadow: 0px 20px 70px 0px rgb(0 0 0 / 21%);
}

.my-container .my-card .imgBx {
    position: relative;
    width: 100%;
    height: 310px;
    overflow: hidden;
}

.my-container .my-card .imgBx img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: 0.5s ease-in-out;
    transform-origin: right;
}

.my-container .my-card:hover .imgBx img {
    transform: scale(1.5);
}

.my-container .my-card .my-content{
    padding: 10px;
}

.my-container .my-card .my-content .my-product-name h3{
    font-size: 18px;
    font-weight: 500;
    color: #333;
    margin: 5px 0;
    text-align: center;
}

.my-container .my-card .my-content .my-rating {
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: center;
    padding: 15px;
}

.my-container .my-card .my-content .my-rating h2{
    font-size: 20px;
    color: #333;
    font-weight: 500;
}

.my-action{
    position: absolute;
    top: 10px;
    right: 10px;
}

.my-action li{
    position: relative;
    list-style: none;
    width: 40px;
    height: 40px;
    background: #fff;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 4px;
    cursor: pointer;
    transition: transform 0.5s;
    transform: translateX(60px);
}

.my-action li:nth-child(2){
    transition-delay: 0.15s;
}

.my-action li:nth-child(3){
    transition-delay: 0.3s;
}

.my-container .my-card:hover .my-action li{
    transform: translateX(0px);
}

.my-action li:hover{
    background: #a7389d;
    color: #fff;
}

.my-action li span{
    position: absolute;
    right: 50px;
    top: 50%;
    transform: translateY(-50%) translateX(-20px);
    white-space: nowrap;
    padding: 4px 60px;
    background: #fff;
    color: #333;
    font-weight: 500;
    font-size: 12px;
    border-radius: 4px;
    pointer-events: none;
    opacity: 0;
    transition: 0.5s;
}

.my-action li:hover span{
    opacity: 1;
    transform: translateY(-50%) translateX(0px);
}

.my-action li span::before{
    content: ' ';
    position: absolute;
    top: 50%;
    right: -4px;
    width: 8px;
    height: 8px;
    background: #a7389d;
    transform: translateY(-50%) rotate(50deg);
}

.app{
    text-align: center;
}

.app h6{
    display: inline-block;
}

.verified-label{
    text-align: center;
}

.verified-label h6{
    display: inline-block;
}

</style>
    </head>

    <body>



<div class="my-grid">
<div class="my-container">

    <div class="my-card">
        <div class="imgBx">
            <img src="/test.jpg">
            <ul class="my-action">
                <li>
                    <i class="fas fa-external-link-alt"></i>
                    <span>View more</span>          
                </li>
                <li>
                    <i class="fas fa-camera"></i>
                    <span>2</span>          
                </li>
                <li>
                    <div class="f32">
                        <a class="flag ru"></a>
                   </div>           
                </li>
            </ul>
        </div>
        <div class="my-content">
            <div class="my-product-name">
                <h3>Test Name</h3>
            </div>
            <div class="app">
                <span class="fas fa-mobile-alt">
                        <h6></h6>
                </span>
            </div>
            <div class="verified-label">
                <span class="fas fa-gem">
                        <h6></h6>
                </span>
            </div>
            <div class="my-rating">
                <div class="rating">
                    <i class="fas fa-fire"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-eye"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-heart"></i>
                    <h2>168</h2>
                </div>
            </div>
        </div>
    </div>
    
        <div class="my-card">
        <div class="imgBx">
            <img src="/test.jpg">
        </div>
        <div class="my-content">
            <div class="my-product-name">
                <h3>Test Name</h3>
            </div>
            <div class="my-rating">
                <div class="rating">
                    <i class="fas fa-fire"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-eye"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-heart"></i>
                    <h2>168</h2>
                </div>
            </div>
        </div>
    </div>
    

</div>
</div>
        <script src="js/jquery-1.12.1.min.js"></script>
        <script src="js/pCard_script.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js"></script>

    </body>
    
</html>

请在我的容器中添加 justify-items: center class。

<!DOCTYPE html>
<html lang="en">
    
    <head>
        <meta charset="utf-8">
        <title>Profile Card</title>
        <link rel="stylesheet" href="css/font-awesome.min.css">       
        <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700' rel='stylesheet'>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
        <style>

.my-grid {
    display: flex;
    justify-content: center;
    align-content: center;
}

.my-container{
    position: relative;
    width: 1200px;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    grid-gap: 20px;
    padding: 20px;
    justify-items: center;
}

.my-container .my-card{
    width: 100%;
    background: #fff;
    border-radius: 20px;
    max-width: 250px;
    box-shadow: 0px 20px 70px 0px rgb(0 0 0 / 21%);
}

.my-container .my-card .imgBx {
    position: relative;
    width: 100%;
    height: 310px;
    overflow: hidden;
}

.my-container .my-card .imgBx img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: 0.5s ease-in-out;
    transform-origin: right;
}

.my-container .my-card:hover .imgBx img {
    transform: scale(1.5);
}

.my-container .my-card .my-content{
    padding: 10px;
}

.my-container .my-card .my-content .my-product-name h3{
    font-size: 18px;
    font-weight: 500;
    color: #333;
    margin: 5px 0;
    text-align: center;
}

.my-container .my-card .my-content .my-rating {
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: center;
    padding: 15px;
}

.my-container .my-card .my-content .my-rating h2{
    font-size: 20px;
    color: #333;
    font-weight: 500;
}

.my-action{
    position: absolute;
    top: 10px;
    right: 10px;
}

.my-action li{
    position: relative;
    list-style: none;
    width: 40px;
    height: 40px;
    background: #fff;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 4px;
    cursor: pointer;
    transition: transform 0.5s;
    transform: translateX(60px);
}

.my-action li:nth-child(2){
    transition-delay: 0.15s;
}

.my-action li:nth-child(3){
    transition-delay: 0.3s;
}

.my-container .my-card:hover .my-action li{
    transform: translateX(0px);
}

.my-action li:hover{
    background: #a7389d;
    color: #fff;
}

.my-action li span{
    position: absolute;
    right: 50px;
    top: 50%;
    transform: translateY(-50%) translateX(-20px);
    white-space: nowrap;
    padding: 4px 60px;
    background: #fff;
    color: #333;
    font-weight: 500;
    font-size: 12px;
    border-radius: 4px;
    pointer-events: none;
    opacity: 0;
    transition: 0.5s;
}

.my-action li:hover span{
    opacity: 1;
    transform: translateY(-50%) translateX(0px);
}

.my-action li span::before{
    content: ' ';
    position: absolute;
    top: 50%;
    right: -4px;
    width: 8px;
    height: 8px;
    background: #a7389d;
    transform: translateY(-50%) rotate(50deg);
}

.app{
    text-align: center;
}

.app h6{
    display: inline-block;
}

.verified-label{
    text-align: center;
}

.verified-label h6{
    display: inline-block;
}

</style>
    </head>

    <body>



<div class="my-grid">
<div class="my-container">

    <div class="my-card">
        <div class="imgBx">
            <img src="/test.jpg">
            <ul class="my-action">
                <li>
                    <i class="fas fa-external-link-alt"></i>
                    <span>View more</span>          
                </li>
                <li>
                    <i class="fas fa-camera"></i>
                    <span>2</span>          
                </li>
                <li>
                    <div class="f32">
                        <a class="flag ru"></a>
                   </div>           
                </li>
            </ul>
        </div>
        <div class="my-content">
            <div class="my-product-name">
                <h3>Test Name</h3>
            </div>
            <div class="app">
                <span class="fas fa-mobile-alt">
                        <h6></h6>
                </span>
            </div>
            <div class="verified-label">
                <span class="fas fa-gem">
                        <h6></h6>
                </span>
            </div>
            <div class="my-rating">
                <div class="rating">
                    <i class="fas fa-fire"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-eye"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-heart"></i>
                    <h2>168</h2>
                </div>
            </div>
        </div>
    </div>
    
        <div class="my-card">
        <div class="imgBx">
            <img src="/test.jpg">
        </div>
        <div class="my-content">
            <div class="my-product-name">
                <h3>Test Name</h3>
            </div>
            <div class="my-rating">
                <div class="rating">
                    <i class="fas fa-fire"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-eye"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-heart"></i>
                    <h2>168</h2>
                </div>
            </div>
        </div>
    </div>
    

</div>
</div>
        <script src="js/jquery-1.12.1.min.js"></script>
        <script src="js/pCard_script.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js"></script>

    </body>
    
</html>

justify-items 沿行(内联)轴对齐网格项目。具体来说,此 属性 允许您在特定位置为网格容器(而非网格本身)内的项目设置对齐方式。

<!DOCTYPE html>
<html lang="en">
    
    <head>
        <meta charset="utf-8">
        <title>Profile Card</title>
        <link rel="stylesheet" href="css/font-awesome.min.css">       
        <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700' rel='stylesheet'>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/>
        <style>

.my-grid {
    display: flex;
    justify-content: center;
 }

.my-container{
    position: relative;
    width: 1200px;
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    grid-gap: 20px;
    padding: 20px;
    justify-items:center;
}

.my-container .my-card{
    width: 100%;
    background: #fff;
    border-radius: 20px;
    max-width: 250px;
    box-shadow: 0px 20px 70px 0px rgb(0 0 0 / 21%);
}

.my-container .my-card .imgBx {
    position: relative;
    width: 100%;
    height: 310px;
    overflow: hidden;
}

.my-container .my-card .imgBx img {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: 0.5s ease-in-out;
    transform-origin: right;
}

.my-container .my-card:hover .imgBx img {
    transform: scale(1.5);
}

.my-container .my-card .my-content{
    padding: 10px;
}

.my-container .my-card .my-content .my-product-name h3{
    font-size: 18px;
    font-weight: 500;
    color: #333;
    margin: 5px 0;
    text-align: center;
}

.my-container .my-card .my-content .my-rating {
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: center;
    padding: 15px;
}

.my-container .my-card .my-content .my-rating h2{
    font-size: 20px;
    color: #333;
    font-weight: 500;
}

.my-action{
    position: absolute;
    top: 10px;
    right: 10px;
}

.my-action li{
    position: relative;
    list-style: none;
    width: 40px;
    height: 40px;
    background: #fff;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 4px;
    cursor: pointer;
    transition: transform 0.5s;
    transform: translateX(60px);
}

.my-action li:nth-child(2){
    transition-delay: 0.15s;
}

.my-action li:nth-child(3){
    transition-delay: 0.3s;
}

.my-container .my-card:hover .my-action li{
    transform: translateX(0px);
}

.my-action li:hover{
    background: #a7389d;
    color: #fff;
}

.my-action li span{
    position: absolute;
    right: 50px;
    top: 50%;
    transform: translateY(-50%) translateX(-20px);
    white-space: nowrap;
    padding: 4px 60px;
    background: #fff;
    color: #333;
    font-weight: 500;
    font-size: 12px;
    border-radius: 4px;
    pointer-events: none;
    opacity: 0;
    transition: 0.5s;
}

.my-action li:hover span{
    opacity: 1;
    transform: translateY(-50%) translateX(0px);
}

.my-action li span::before{
    content: ' ';
    position: absolute;
    top: 50%;
    right: -4px;
    width: 8px;
    height: 8px;
    background: #a7389d;
    transform: translateY(-50%) rotate(50deg);
}

.app{
    text-align: center;
}

.app h6{
    display: inline-block;
}

.verified-label{
    text-align: center;
}

.verified-label h6{
    display: inline-block;
}

</style>
    </head>

    <body>



<div class="my-grid">
<div class="my-container">

    <div class="my-card">
        <div class="imgBx">
            <img src="/test.jpg">
            <ul class="my-action">
                <li>
                    <i class="fas fa-external-link-alt"></i>
                    <span>View more</span>          
                </li>
                <li>
                    <i class="fas fa-camera"></i>
                    <span>2</span>          
                </li>
                <li>
                    <div class="f32">
                        <a class="flag ru"></a>
                   </div>           
                </li>
            </ul>
        </div>
        <div class="my-content">
            <div class="my-product-name">
                <h3>Test Name</h3>
            </div>
            <div class="app">
                <span class="fas fa-mobile-alt">
                        <h6></h6>
                </span>
            </div>
            <div class="verified-label">
                <span class="fas fa-gem">
                        <h6></h6>
                </span>
            </div>
            <div class="my-rating">
                <div class="rating">
                    <i class="fas fa-fire"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-eye"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-heart"></i>
                    <h2>168</h2>
                </div>
            </div>
        </div>
    </div>
    
        <div class="my-card">
        <div class="imgBx">
            <img src="/test.jpg">
        </div>
        <div class="my-content">
            <div class="my-product-name">
                <h3>Test Name</h3>
            </div>
            <div class="my-rating">
                <div class="rating">
                    <i class="fas fa-fire"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-eye"></i>
                    <h2>168</h2>
                </div>
                <div class="rating">
                    <i class="fas fa-heart"></i>
                    <h2>168</h2>
                </div>
            </div>
        </div>
    </div>
    

</div>
</div>
        <script src="js/jquery-1.12.1.min.js"></script>
        <script src="js/pCard_script.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.min.js"></script>

    </body>
    
</html>