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

前端响应式布局故障求助:卡片、图片及按钮适配问题

订单摘要卡片响应式适配故障排查与修复

问题描述

在Frontend Mentor完成的订单摘要卡片项目,在笔记本电脑及部分手机屏幕显示正常,但部分屏幕出现布局故障:图片过宽、按钮及卡片布局错乱。

故障根源分析

  1. 容器宽度逻辑冲突:.container同时设置width: max(100%, 60%)与重复的max-width: 400px,小屏设备下容器宽度超出预期,引发内部元素溢出。
  2. 浮动布局清除方式错误:.panel中直接使用content: ""; display: table; clear: both;,这是伪元素的清除浮动写法,直接写在元素上无效,导致浮动元素无法被正确包裹。
  3. 图片比例控制缺失:仅设置图片max-width: 100%但未限制高度,部分屏幕比例下图片拉伸变形。
  4. 媒体查询权重失效:媒体查询写在全局样式之前,导致响应式样式被全局样式覆盖。

修复后的完整代码

HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="icon" type="image/png" sizes="32x32" href="./images/favicon-32x32.png">
  <link rel="stylesheet" href="colors.css">
  <title>Frontend Mentor | Order summary card</title>
</head>
<body>
<div class="container">
    <div class="card">
      <img src="images/illustration-hero.svg" alt="avatar icon">
      <div class="content">
        <h1>Order Summary</h1>
        <p>
          You can now listen to millions of songs, audiobooks, and podcasts on any 
          device anywhere you like!
        </p>
        <div class="panel">
          <img src="images/icon-music.svg" alt="music icon">
          <div class="p-text">
            <b>Annual Plan</b><br>
            $59.99/year
          </div>
          <a href="">Change</a>
        </div>
        <button class="b-payment">Proceed to Payment</button>
        <button class="b-cancel">Cancel Order</button>
      </div>
    </div>
    <div class="attribution">
      Challenge by <a href="https://www.frontendmentor.io?ref=challenge" target="_blank">Frontend Mentor</a>. 
      Coded by <a href="https://linktr.ee/shafni50">safni-nazar</a>.
    </div>
</div>
</body>
</html>

CSS代码(嵌套CSS)

@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@400;700&family=Red+Hat+Display:wght@500;900&display=swap');

:root {
    --very-pale-blue: hsl(225, 100%, 98%);
    --pale-blue:  hsl(225, 100%, 94%);
    --desatured-blue: hsl(224, 23%, 55%);
    --bright-blue: hsl(245, 75%, 52%);
    --dark-blue: hsl(223, 47%, 23%);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background-color: var(--pale-blue);
    background-image: url(images/pattern-background-desktop.svg);
    background-repeat: no-repeat;
    background-size: contain;
    min-height: 100vh;
    display: flex;
    font-family: 'Red Hat Display', sans-serif;
    text-align: center;
    align-items: center;
    justify-content: center;
}

.container {
    width: 90%;
    max-width: 400px;
    padding: 1rem;
    margin: 0 auto;
}

.card {
    background-color: #fff;
    box-shadow: rgba(149, 157, 165, 0.2) 0px 10px 50px;
    border-radius: 15px;
    overflow: hidden;

    & h1 {
        font-size: 24px;
        color: var(--dark-blue);
        margin-bottom: 1rem;
    }

    & p {
        font-size: 15px;
        color: var(--desatured-blue);
        margin-bottom: 1.5rem;
        line-height: 1.6;
    }

    & img {
        width: 100%;
        height: auto;
    }

    .content {
        padding: 2rem;

        .panel {
            width: 100%;
            margin-bottom: 2rem;
            padding: 1rem;
            border-radius: 10px;
            background-color: var(--very-pale-blue);
            display: flex;
            align-items: center;
            gap: 1rem;

            .p-text {
                flex: 1;
                text-align: left;
            }

            & img {
                width: 48px;
                height: 48px;
                border-radius: 50%;
                object-fit: cover;
            }

            & b {
                color: var(--dark-blue);
                font-weight: 900;
            }

            & a {
                color: var(--bright-blue);
                font-weight: bold;
                font-size: 14px;

                &:hover {
                    opacity: 0.6;
                    text-decoration: none;
                }
            }
        }
    }

    .b-payment {
        cursor: pointer;
        border: none;
        font-weight: 800;
        color: var(--very-pale-blue);
        background-color: var(--bright-blue);
        padding: 1rem;
        border-radius: 10px;
        width: 100%;
        box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 50px 0 rgba(0,0,0,0.19);

        &:hover {
            transition: all 0.3s;
            opacity: 0.7;
        }
    }

    .b-cancel {
        border: none;
        margin-top: 1.5rem;
        cursor: pointer;
        font-weight: 800;
        color: var(--desatured-blue);
        background: transparent;
        width: 100%;
        padding: 1rem 0;

        &:hover {
            color: var(--dark-blue);
        }
    }
}

.attribution { 
    padding-top: 1rem;
    font-size: 11px; 
    text-align: center; 

    & a{
        color: hsl(228, 45%, 44%);
    }
}

@media screen and (max-width: 375px) {
    body {
        background-image: url(images/pattern-background-mobile.svg);
    }

    .panel .p-text,
    .panel a {
        font-size: 14px;
    }
}

@media screen and (max-width: 330px) {
    .panel .p-text,
    .panel a {
        font-size: 12px;
    }
}

@media screen and (max-width: 300px) {
    .panel .p-text,
    .panel a {
        font-size: 10px;
    }
}

关键修复点说明

  • 简化.container宽度逻辑为width:90%,避免计算冲突
  • 用Flex布局替代浮动实现.panel横向排列,彻底解决浮动布局的不稳定问题
  • 给.card添加overflow: hidden,确保顶部图片圆角正确生效
  • 将媒体查询移至全局样式之后,保证响应式样式优先级
  • 给取消按钮增加上下内边距,优化小屏点击区域
  • 合并body重复样式声明,精简代码结构

内容的提问来源于stack exchange,提问作者Safni Nazar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 15:53:17