You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

页面内容垂直显示异常求助:添加padding后Grid布局不符预期

两列Grid布局失效,内容垂直排列问题

当前显示效果

我当前的显示效果

预期显示效果

预期的显示效果

问题详情

跟着教程做项目,代码完全一致,但页面显示不对。本该是两列并排的布局,结果第一列内容全垂直排列,加更多文本也只会往下延伸。所有步骤都和教程一样,就是加了padding: 0 19%;之后布局就出问题了,用的是Edge浏览器。

HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
    <!-- boxicons link-->
    <link rel="stylesheet"    href="https://unpkg.com/boxicons@latest/css/boxicons.min.css">
    <!-- remixicon link-->
    <link href="https://cdn.jsdelivr.net/npm/remixicon@3.4.0/fonts/remixicon.css" rel="stylesheet">
    <!-- google fonts-->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Permanent+Marker&family=Poppins:wght@300;400;500;600;700;800;900&display=swap" rel="stylesheet">
</head>
<body>
    <header>
        <a href="#" class="logo">COC-STATS</a>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">About</a></li>
            <li><a href="#">Clans</a></li>
            <li><a href="#">Town hall</a></li>
            <li><a href="#">Troops</a></li>
            <li><a href="#">Contacts</a></li>
        </ul>
        <div class="bx bx-menu" id="menu-icon"></div>
    </header>
    <section class="hero">
        <div class="hero-text">
            <h5>#2 Trending</h5>
            <h4>Lead Supercell</h4>
            <h1>GOBLINS</h1>
            <p>Join millions of players worldwide as you build your village, <br> raise a clan, and compete in epic Clan Wars!! </p>
            <a href="#">Compare</a>
            <a href="#" class="ctaa"><i class="ri-play-fill"></i>Watch Gameplay!</a>
        </div>
        <div class="hero-img">
            <img src="img-removebg.png" alt="">
        </div>
    </section>
    <script src="script.js"></script>
</body>
</html>

CSS代码

*{
    margin: 0;
    padding: 0;
    font-family: 'Poppins', sans-serif;
    list-style: none;
    box-sizing: border-box;
    text-decoration: none;
}
header{
    position: fixed;
    right:0;
    top:0;
    z-index: 1;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 33px 9%;
    background: transparent;
}
.logo{
    font-size: 30px;
    font-weight: 700;
    color: #fff;
}
header ul{
    display: flex;
}
header ul li a{
    color: #fff;
    margin-left: 60px;
    font-size: 15px;
    font-weight: 600;
    border-bottom: 2px solid transparent;
    transition: all .55s ease;
}
header ul li a:hover{
    border-bottom: 2px solid white;
}
#menu-icon{
    color: #fff;
    font-size: 35px;
    z-index: 2;
    cursor: pointer;
    display: none;
}
.hero{
    height: 100%;
    width: 100%;
    min-height: 100vh;
    background: linear-gradient(245.59deg, #4d9559 0%, #38703d 28.53%, #133917 75.52%);
    position: relative;
    display: grid;
    grid-template-columns: repeat(2,1fr);
    align-items: center;
    gap: 2rem;
}
section{
    padding: 0 19%;
}

修复方案

问题根源是你给section设置了padding: 0 19%;,而.hero作为Grid容器,它的可用宽度被padding挤压后,不足以容纳两列1fr的内容,Grid会自动换行,导致内容垂直排列。

推荐两种解决方法:

  1. 将padding移到Grid子元素上(优先选择)
    移除section的padding,给.hero的两个子元素单独加左右内边距:

    /* 移除section的padding */
    section{
        /* padding: 0 19%; */
    }
    
    /* 给hero的子元素添加内边距 */
    .hero-text, .hero-img{
        padding: 0 19%;
    }
    

    这样Grid容器的宽度不会被压缩,两列布局能正常生效,同时也能保持内容的左右间距。

  2. 调整Grid列的宽度计算方式
    如果想保留section的padding,可以修改Grid的列设置,用minmax()确保列不会过小:

    .hero{
        grid-template-columns: repeat(2, minmax(250px, 1fr));
    }
    

内容的提问来源于stack exchange,提问作者Fábio Gonçalves

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 05:27:05