V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Alander
V2EX  ›  程序员

html table width 问题

  •  
  •   Alander · 2021-11-05 14:41:07 +08:00 · 894 次点击
    这是一个创建于 901 天前的主题,其中的信息可能已经有所发展或是发生改变。

    遇到个问题,关于 html 中同时设置 table 元素的 table-layout: fixed; 和 width: 100%;的问题:

    实际结果如下: 如果 .container { width: 600px }, 那么

    1. 如果不设置 width 属性,则 table 的宽度就是 600px
    2. 如果设置 table 的 width: 100%; 则 table 的宽度是 col 的宽度之和 1920px;

    我的疑问是:

    1. 这个 width: 100% 是怎么计算的?
    2. 当 width: auto | 100% | 具体像素值; 遇上 table-layout: fixed | auto; 又是怎么计算宽度的?

    有什么资料可以提供查阅吗?我现在只知道会有这样的效果,但是不知道为什么,找了挺久资料也没找到,有了解的大佬可以跟我指个路我去看看?

    
    <style>
    .container {
    	width: 600px;
    }
    table {
        table-layout: fixed;
        width: 100%; // 这里切换是否设置宽度
    }
    </style>
    <div class="container">
    	<table class="table">
    		<col style="width: 30px">
    		<col style="width: 90px">
    		<col style="width: 200px">
    		<col style="width: 200px">
    		<col style="width: 600px">
    		<col style="width: 300px">
    		<col style="width: 500px">
    		<thead class="thead">
    			<tr>
    				<th>
    					#
    				</th>
    				<th>
    					姓名
    				</th>
    				<th>
    					年龄
    				</th>
    				<th>
    					职业
    				</th>
    				<th>
    					住址
    				</th>
    				<th>
    					兴趣爱好
    				</th>
    				<th>
    					其他
    				</th>
    			</tr>
    		</thead>
    		<tbody>
    		</tbody>
    	</table>
    </div>
    
    2 条回复    2021-11-05 15:45:46 +08:00
    akaxiaok339
        1
    akaxiaok339  
       2021-11-05 15:06:36 +08:00
    Alander
        2
    Alander  
    OP
       2021-11-05 15:45:46 +08:00
    @akaxiaok339 感谢感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5351 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:13 · PVG 15:13 · LAX 00:13 · JFK 03:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.