table height设为100%后高度没有全屏显示

100%高度布局
本文介绍了如何在HTML页面中实现table和div元素100%的高度布局,并提供了具体的代码示例。对于table,需要设置html和body的高度为100%;而对于div,则需要额外确保body高度为100%,在某些浏览器中还需设置html高度。
What does 100% height means?

Setting the 100% height of the table means the table will occupy 100% of the available space vertically. Here available space means space of it's parent control. If the height of the parent control is not 100% then height of the table couldn't be 100%.

If you are trying to set 100% height of the table directly contained the body of a html page the you need to first set the height of the body and html. The below code will work perfectly fine for the 100% table height.

[color=red]关键是下面前两行[/color]


<html style="height: 100%;">

<body style="height: 100%;">

<table style="height: 100%;">

<tr>

<td>....</td>

</tr>

</table>

</body>

</html>


If you are using css then html, body { height: 100%; }

This will work in almost all browser


[color=red]上面设置后在ie6下正常,在ie8的兼容模式下也可以,但用ie8或firefox、opera等还是不行,参看下面,将指定的网页是遵循什么标准语句删除即可。[/color]


table和div设置height:100%无效的完美解决方法

刚接触网页排版的新手,常出现这种情况:设置table和div的高height="100%"无效,使用CSS来设置height:"100%"也无效,为什么会这样呢?解决height:100%无效,table和div的解决方法并不相同。

首先说一下table,他比较容易解决,当我们使用Dreamweaver来制作网页,新建一张网页,通常在代码头部会有类似以下的代码:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

没错,你猜对了,问题就出在这里,你试着把这短代码删除,然后再刷新一下网页,你就会看到所有table以你的设置height="100%"的展示! 这段代码是告诉浏览器你的网页是遵循什么标准的,如上面的“W3C”标准,删除掉一般是不影响的。


下面说一下div,div和table一样,如果要实现width:100%是很容易的,但要div的height:"100%",它就不大听话了,其实不是它不听话,是你不知道让它听话的方法。如下代码:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>测试</title> </head> <body> <div style="height:100%"></div> </body> </html>

就算和Table一样去掉头部的那段代码也不能100%显示,原因很简单,你让div的height="100%",执行网页时,css先执行到,而整个网页中的内容还没有完全载入,是获取不到div外面的<body>等的高度的,所以height="100%"也就不能如愿显示了。加上 body{height:100%} 就轻松解决啦,一开始就让body以100%显示,他的下级div自然就100%的,不过对于FF浏览器还应该把HTML也先给height:100%,即 html,body{height:100%}

html代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> New Document </title> <style type="text/css">  html { height: 100%}  body {height:100%;margin:0px;padding:0px}  table{height: 100%;width: 100%;} </style> </head>

<body> <table border=1> <tr> <td> <ul> <li>12 <li>34 </ul> </td> <td> <iframe name="" src="" width="100%" height="100%"></iframe> </td> </tr> </table> </body> </html> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> New Document </title> <style type="text/css"> html { height: 100%} body {height:100%;margin:0px;padding:0px} table{height: 100%;width: 100%;} </style> </head>

<body> <table border=1> <tr> <td> <ul> <li>12 <li>34 </ul> </td> <td> <iframe name="" src="" width="100%" height="100%"></iframe> </td> </tr> </table> </body> </html>
要实现 `el-dialog` 全屏后内部 `el-table` 表格高度自适应,可以通过以下几种常见的解决方案: ### 方案一:使用 CSS 计算高度 通过 CSS 的 `calc()` 函数来动态计算表格高度,减去对话框头部、底部以及可能的边距等高度,使表格高度自适应。 ```vue <template> <el-dialog :visible.sync="dialogVisible" title="Full Screen Dialog" :fullscreen="isFullscreen"> <template #content> <el-table :data="tableData" style="width: 100%; height: calc(100vh - 120px);"> <!-- 表格列定义 --> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> </el-table> </template> <template #footer> <span class="dialog-footer"> <el-button @click="dialogVisible = false">Cancel</el-button> <el-button type="primary" @click="dialogVisible = false">Confirm</el-button> </span> </template> </el-dialog> </template> <script> export default { data() { return { dialogVisible: false, isFullscreen: true, tableData: [ { name: 'John', age: 20 }, { name: 'Jane', age: 25 } ] }; } }; </script> ``` 在上述代码中,`calc(100vh - 120px)` 表示表格高度为视口高度减去 120px,120px 可根据实际对话框头部和底部的高度进行调整。 ### 方案二:使用 JavaScript 动态计算高度 在对话框全屏状态改变时,通过 JavaScript 动态计算表格高度。 ```vue <template> <el-dialog :visible.sync="dialogVisible" title="Full Screen Dialog" :fullscreen="isFullscreen" @fullscreen-change="handleFullscreenChange"> <template #content> <el-table :data="tableData" :style="{ height: tableHeight + 'px' }"> <!-- 表格列定义 --> <el-table-column prop="name" label="Name"></el-table-column> <el-table-column prop="age" label="Age"></el-table-column> </el-table> </template> <template #footer> <span class="dialog-footer"> <el-button @click="dialogVisible = false">Cancel</el-button> <el-button type="primary" @click="dialogVisible = false">Confirm</el-button> </span> </template> </el-dialog> </template> <script> export default { data() { return { dialogVisible: false, isFullscreen: true, tableData: [ { name: 'John', age: 20 }, { name: 'Jane', age: 25 } ], tableHeight: 0 }; }, methods: { handleFullscreenChange(fullscreen) { if (fullscreen) { const dialogHeader = this.$el.querySelector('.el-dialog__header'); const dialogFooter = this.$el.querySelector('.el-dialog__footer'); const headerHeight = dialogHeader ? dialogHeader.offsetHeight : 0; const footerHeight = dialogFooter ? dialogFooter.offsetHeight : 0; this.tableHeight = window.innerHeight - headerHeight - footerHeight - 20; // 20 为边距 } else { // 非全屏状态下的高度处理 this.tableHeight = 300; } } } }; </script> ``` 在上述代码中,`handleFullscreenChange` 方法在对话框全屏状态改变时被调用,通过获取对话框头部和底部的高度,动态计算表格高度
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值