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

CSS布局问题:footer设边框后仅首个p标签被包含,第二个p溢出

解决footer内第二个p标签溢出边框的问题

问题根源

你给footer设置了固定的height: 50px,但两个p标签的总高度(包括浏览器默认的margin、行高)超过了50px,导致第二个p被挤出了footer的红色边框。

几种可行的解决办法

  • 直接移除固定高度:删掉footer的height: 50px,让footer根据内部内容自动撑开高度,这是最简单的处理方式:

    footer {
      border: 2px solid red;
      text-align: center;
      /* 删掉height: 50px */
    }
    
  • 用最小高度替代固定高度:如果想让footer至少保持50px高度,同时能自适应内容高度,把height改成min-height:

    footer {
      border: 2px solid red;
      text-align: center;
      min-height: 50px;
    }
    
  • 调整内部元素间距强行适配:通过缩小p标签的默认外边距,让内容能放进50px高度的footer里,同时用flex布局让内容居中:

    footer {
      border: 2px solid red;
      text-align: center;
      height: 50px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      padding: 0 10px;
    }
    footer p {
      margin: 3px 0; /* 缩小上下外边距 */
      line-height: 1.2; /* 可选:缩小行高 */
    }
    

内容的提问来源于stack exchange,提问作者Ahmed Saif

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 20:48:22