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

论坛开发CSS问题:让白色背景延伸至灰色背景右侧(带间隙)

解决论坛投票模块白色背景宽度问题

要让白色的.poll-content延伸至灰色背景右侧并保留5-10px间隙,问题大概率出在父容器的默认内边距/外边距限制了宽度,或者.poll-content的宽度未正确适配父容器。试试以下两种方案:

方案一:抵消父容器的内边距

如果.poll-container或它的上层容器存在左右内边距(比如常见的10px),可以通过负外边距抵消,同时调整自身内边距保证内容间距:

.poll-container {
    margin-bottom: 10px;
}

.poll-content {
    background-color: #fff;
    padding: 10px 20px; /* 左右内边距设为20px,保留内容与边缘的10px间隙 */
    border-radius: 5px;
    margin-left: -10px; /* 抵消父容器左侧内边距 */
    margin-right: -10px; /* 抵消父容器右侧内边距 */
}

方案二:直接设置适配宽度

如果父容器没有额外内边距,直接通过calc()计算宽度来保留间隙:

.poll-container {
    margin-bottom: 10px;
}

.poll-content {
    background-color: #fff;
    padding: 10px;
    border-radius: 5px;
    width: calc(100% - 20px); /* 父容器宽度减去20px,左右各留10px间隙 */
    margin: 0 auto; /* 保证左右间隙对称 */
}

额外检查点

  • 确认上层容器没有设置overflow: hidden,否则负外边距可能会被截断
  • 如果页面未全局设置盒模型,建议加上以下代码避免宽度计算偏差:
* {
    box-sizing: border-box;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 15:52:34