display:table-cell 探究

本文探讨了CSS中table-cell属性的特点及应用,包括实现等高布局、垂直居中布局及左固定右自适应布局的方法。

table-cell这个家伙在国外的网站中偶有露头,天朝由于IE6、7这两个货泛滥成灾,难有发挥,那么,这个家伙到底能干些什么呢?先让我们来研究下table,那些年曾经使用的table布局为何如此辉煌荡漾呢?她的特点有哪些呢?抛弃table的兼容性、seo、加载等与本文无关的内容不谈,只看属性,那么就两个特点:

1.同行等高。

2.宽度自动调节。

那么table-cell是不是具备这个特点呢?答案是yes,为什么呢?css中有一个有意思的规则“创建匿名表格元素”。拿table-cell来扯,就是,当某个元素被设置为display:table-cell的时候,如果她的父节点不是display:table-row,爷爷节点不是display:table,那么下面就是见证奇迹的时候,这个儿子生出了他的爸爸和爷爷(浏览器会自动创建者两个匿名盒对象)虽然你找不到你的father 和 grandfather,但这确实发生了,这真的是不可思议的事情,哪里不可思议,没有掌声最不可思议。^_^

那么下面来看几种情况,帮助我们了解这个不可思议的事情。我先编写代码如下:

复制代码
<style type="text/css">
    div{padding:10px 0;}
    .classtd,
    td{height:34px; padding:10px; margin:10px; border:1px solid #ccc; vertical-align:middle;}
    .classtd{display:table-cell; border-color:#cc0;}
</style>
<div class="classtd">tom</div>
<div class="classtd">jack</div>
<div>普通 div</div>
<div class="classtd">angel</div>

<div>======= 上面是div 下面是table ========</div>
<table cellpadding="0" cellspacing="0">
    <tr>
        <td class="dtc">tom</td>
        <td class="dtc">jack</td>
    </tr>
</table>
<table style="margin-top:10px;" cellpadding="0" cellspacing="0">
    <tr>
        <td>angel</td>
    </tr>
</table>
复制代码

然后,我预测浏览器显示如下:

好吧,我承认我先看了效果,然后就可以下结论了:tom 和 jack 搞基,生出了父亲和爷爷(浏览器会创建一个表格来包裹相邻的display:table-cell元素),表现和第一个表格相同。angel自己生出了父亲和爷爷表现和第二个表格相同。

既然是这样,那么想了解table-cell,就是变相了解表格的td了。那就回到了前面所说的两个特点:同行等高,宽度自动调节。

既然是这样,那么我们就可以拿这个货来作等高布局

复制代码
<style type="text/css">
    .classtd{padding:10px; margin:10px; border:1px solid #ccc; vertical-align: top;}
    .classtd{display:table-cell; border-color:#cc0;}
</style>
<div class="classtd">
    <p>大人。<br />其实我觉得大家别问元芳,元芳不是神人,<br />也不会武功,也许还是个智障,<br />我就不信我在这里黑元芳<br />他会突然飞檐走壁来到我身后<br />把我的头按在键盘上yu7jhklhgjkfgt;/.";. yujh bnujm798u7jrtb5 tq1qwsewrt5
    </p>
</div>
<div class="classtd"><p>我和左边等高</p></div>
复制代码

把这货和vertical-align:middle搞在一起可以进行大小不固定元素的垂直居中布局(还有多行文本垂直居中)

复制代码
<style type="text/css">
    .classtd{ display: table-cell; padding:10px;margin:10px;border:1px solid #ccc;}
    .classtd div{ display: inline-block; vertical-align: middle;}
</style>
<div class="classtd">
    <div style="padding:40px 80px 10px 10px; background: #639146; color:#fff;">div+css</div>
    <div style="padding:60px 80px 10px 10px; background: #2B82EE; color:#fff;">javascript</div>
    <div style="padding:70px 80px 10px 10px; background: #F57900; color:#fff;">HTML5</div>
    <div style="padding:80px 80px 10px 10px; background: #BC1D49; color:#fff;">CSS3</div>
</div>
复制代码

利用列宽度自动调节这个特点可以作左固定右自适应布局

复制代码
<style type="text/css">
    .left{float:left; width:260px; padding:10px; margin-right:10px; border:1px solid #ccc;}
    .classtd{ display: table-cell; width:3000px; padding:10px; border:1px solid #ccc;}
</style>
<div class="left">我是左边栏目</div>
<div class="classtd">
    我是自适应的右边
</div>
复制代码

有童鞋可能会对这个布局中的width:3000px感到迷惑。那么下面就贴上这个布局的原理

display:table-cell 元素生成的匿名table默认table-layout:auto。宽度将基于单元格内容自动调整。所以设置width:3000px的用途是尽可能的宽的意思。这样就可以达到自适应的效果。

http://www.cnblogs.com/StormSpirit/archive/2012/10/24/2736453.html   原文链接

### 解决方案 在 `display: table-cell` 布局下,滚动条无法正常滚动的问题通常与以下因素有关:容器的尺寸限制、表格布局的特性以及内容溢出的处理方式。以下是解决该问题的具体方法和思路: #### 1. 确保父容器具有明确的高度 在使用 `display: table-cell` 的布局时,如果父容器没有明确的高度定义,可能会导致滚动条无法正常工作。需要为父容器设置一个固定高度,并启用 `overflow-y: auto` 或 `overflow-y: scroll`。 ```css .parent-container { height: 300px; /* 设置固定高度 */ overflow-y: auto; /* 启用纵向滚动 */ display: table; /* 父容器需要是 table */ } ``` #### 2. 避免 `table-cell` 元素的宽度超出限制 `table-cell` 元素会根据内容自动调整宽度,这可能导致滚动条无法正常显示。可以通过 `max-width` 和 `word-break` 属性限制单元格内容的宽度,避免内容溢出。 ```css .table-cell-element { max-width: 200px; /* 设置最大宽度 */ word-break: break-all; /* 自动换行 */ display: table-cell; } ``` #### 3. 使用伪元素或额外的包裹层实现滚动 如果直接在 `table-cell` 上添加滚动条效果不理想,可以考虑将内容包裹在一个额外的 `div` 中,并为该 `div` 设置滚动属性。 ```html <div class="parent-container" style="display: table; height: 300px;"> <div class="scrollable-content" style="display: table-cell; height: 100%; overflow-y: auto;"> <!-- 内容 --> <p>长内容...</p> <p>长内容...</p> <p>长内容...</p> </div> </div> ``` #### 4. 确保表头固定时,内容区域独立滚动 如果需要实现表头固定而内容区域滚动的效果,可以将表头和内容分别放在两个独立的容器中,并为内容容器设置滚动属性[^4]。 ```html <div class="table-container" style="height: 300px; overflow-y: auto;"> <div class="table-header" style="display: table-row;"> <div style="display: table-cell; border: 1px solid black;">表头1</div> <div style="display: table-cell; border: 1px solid black;">表头2</div> </div> <div class="table-body"> <div style="display: table-row;"> <div style="display: table-cell; border: 1px solid black;">内容1</div> <div style="display: table-cell; border: 1px solid black;">内容2</div> </div> <div style="display: table-row;"> <div style="display: table-cell; border: 1px solid black;">内容3</div> <div style="display: table-cell; border: 1px solid black;">内容4</div> </div> </div> </div> ``` #### 5. 调整 `el-table` 的样式以适配滚动条 对于基于 `el-table` 的场景,可以参考引用中的解决方案,将表格的主体部分改为块级元素并设置滚动条边距[^1]。 ```css ::v-deep .el-table__body { width: calc(100% - 22px); padding-right: 30px; display: block; overflow-y: auto; height: 300px; /* 设置固定高度 */ } ``` ### 注意事项 - 如果 `display: table-cell` 的布局复杂度较高,建议结合 JavaScript 动态调整容器尺寸。 - 在某些情况下,`table-cell` 布局可能不如 `flexbox` 或 `grid` 布局灵活,可以根据具体需求选择更合适的布局方式。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值