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

如何强制表格始终嵌套在div容器内?

解决表格超出容器的问题

问题描述

表格列头为「Classes/Instâncias/Nomes/Observações」,当前无论在全屏(4列显示)还是小屏场景下,表格都会超出外层div容器,需要强制表格完全处于容器内部。

代码调整方案

1. 修正HTML表格属性

原HTML中table标签的属性写法有误,调整为CSS属性控制形式,确保表格宽度继承容器:

<div class="recent-grid">
    <div class="projects">
        <div class="card">
            <div class="card-header">
                <h2 class="text-light">O que ocorre durante</h2>
            </div>

            <div class="card-body">
                <table class="fixed-table">
                    <thead>
                        <tr>
                            <td>Classes</td>
                            <td>Instâncias</td>
                            <td>Nomes</td>
                            <td>Observações</td>
                        </tr>
                    </thead>
                    <tbody> 
                        {% for obj in objetos_during %}
                        <tr>
                            <td>{{ obj.classe_inst }}</td>
                            <td>{{ obj.instancia }}</td>
                            <td>{{ obj.nome }}</td>
                            <td>{{ obj.obs }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

2. 调整CSS样式

针对表格和容器添加关键样式,确保表格不溢出、内容自动换行:

.recent-grid{
  background-color: #8b0000;
  border-radius: 30%;
  margin: 20px;
  overflow: hidden; /* 确保容器边界生效,内部元素不溢出 */
}

.card {
  position: relative;
  display: flex;
  flex: auto;
  flex-direction: column;
  min-width: 0;
  word-wrap: break-word;
  background-color: #8b0000;
  background-clip: border-box;
  border: 1px solid rgba(0, 0, 0, 0.125);
  border-radius: 0.25rem;
  box-shadow: 8px 8px 5px black;
}

.card-header {
  padding: 0.5rem 1rem;
  margin-bottom: 0;
  background-color: rgba(0, 0, 0, 0.03);
  border-bottom: 1px solid rgba(0, 0, 0, 0.125);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.card-header button{
  background-color: rgb(220, 20, 60);
  border-radius: 5px;
}

.card-header:first-child {
  border-radius: calc(0.25rem - 1px) calc(0.25rem - 1px) 0 0;
}

.card-body {
  display: block;
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  -ms-overflow-style: -ms-autohiding-scrollbar;
  padding: 0.5rem; /* 避免表格贴容器边缘 */
}

/* 固定布局表格样式 */
.fixed-table {
  caption-side: bottom;
  border-collapse: collapse;
  width: 100%;
  table-layout: fixed; /* 强制列宽根据容器自动分配 */
}

th {
  text-align: inherit;
  text-align: -webkit-match-parent;
}

thead,
tbody,
tfoot,
tr,
td,
th {
  border-color: inherit;
  border-style: solid;
  border-width: 0;
  background-color: #F0F0F0;
}

tbody tr{
  background-color: #F0F0F0;
  border-top: 1px solid #0000;
  border-bottom: 1px solid #0000;
}

thead td{
  font-weight: 700;
  border-top: 1px solid #400002;
  border-bottom: 1px solid #400002;
}

td{
  padding: .5rem 1rem;
  font-size: .9rem;
  color:#000;
  word-wrap: break-word; /* 长文本自动换行 */
  white-space: normal;
}

thead tr{
  border-top: 1px solid #F0F0F0;
  border-bottom: 1px solid #F0F0F0;
}

关键调整说明

  • 表格添加table-layout: fixed和width: 100%,让宽度严格继承容器,列宽自动分配
  • 给td设置换行属性,避免长文本撑爆列宽
  • 容器添加overflow: hidden,防止内部元素溢出边界
  • 给.card-body加内边距,优化视觉间距

内容的提问来源于stack exchange,提问作者Guilherme B.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 10:18:36