简体   繁体   English

在CSS中,如何使表扩展以填充容器div?

[英]In CSS, how can I make a table expand to fill a container div?

I want to "carve out" spaces on the screen by creating several container DIVs, then fill in the content in each of the DIVs. 我想通过创建几个容器DIV来“分割”屏幕上的空格,然后填写每个DIV中的内容。 in one case, I need to fill up with a table. 在一个案例中,我需要填写一张桌子。

the innermost part of the table are jpeg <img> . 表的最里面部分是jpeg <img> I do not want to use fixed pixel sizes anywhere, so that the content will shrink nicely in different size browsers. 我不希望在任何地方使用固定像素大小,因此内容将在不同大小的浏览器中很好地缩小。

After repeated trial and errors, and some reading on the internet, I figured out that to let the whole chain container-div--->table-->tbody-->tr-->td-->img fit nicely in size, I must make sure that each of these elements have the following attributes "height:100%; width:100; position:relative" (the img position itself can be absolute ). 经过反复试验和错误,以及互联网上的一些阅读,我想通了让整个链容器 - div ---> table - > tbody - > tr - > td - > img非常适合大小,我必须确保这些元素中的每一个都具有以下属性"height:100%; width:100; position:relative" (img position本身可以是绝对的)。

This works fine with a table of 1 row only. 这适用于仅有1行的表。 but if I have multiple rows, it looks that the row height is calculated naiively from its parent, which is tbody, because if I have 2 rows, each row is still as high as the tbody. 但是如果我有多行,它看起来行高度是从它的父项(即tbody)中计算出来的,因为如果我有2行,那么每行仍然和tbody一样高。 but even after I manually divide the height of TR by 2, it does not exactly fit into the container. 但即使我手动将TR的高度除以2,它也不完全适合容器。

So how can I make the table shrink automatically to the surrounding container? 那么如何让表自动缩小到周围的容器?

    <style type="text/css">

div {
border : 2px solid red;
}

#bottom-banner {
height:40%;
}

#bottom-banner tbody {
position:relative;
height:100%;
width:100%;
}

#bottom-banner tr {
max-height:100%;
width:100%;
position:relative;
height:100%;
}


<style type="text/css">

div {
border : 2px solid red;
}

#bottom-banner {
height:40%;
}

#bottom-banner tbody {
position:relative;
height:100%;
width:100%;
}

#bottom-banner tr {
max-height:100%;
width:100%;
position:relative;
height:100%;
}
#bottom-banner table {
position:relative;
height:100%;
width:100%;
}

#bottom-banner div {
position:relative;
height:100%;
width:100%;
}

#bottom-banner img {
height:100%;
position:absolute;
right:0;
left:0;
margin:auto;
top:0;
bottom:0;
width:auto;
}
</style>

<div id="bottom-banner">

<table><tbody>
<tr>
<td >
<div><img src="11a2.jpg"/></div>
</td>
</tr>

<tr>
<td >
<div><img src="11a2.jpg"/></div>
</td>
</tr>

</tbody></table>
</div>

Just set the width and height of your table to 100%. 只需将表格的宽度和高度设置为100%即可。 But the parent div of the table must have an assigned height if the cells are empty they will not take up any vertical space without a height specified. 但是如果单元格为空,则表格的父div必须具有指定的高度,如果没有指定高度,它们将不会占用任何垂直空间。

 div { width: 100%;height: 500px;border: 5px yellow solid; } table { width: 100%;height: 100%;border: 3px red double; } 
 <div> <table> <tr> <td bgcolor="#2062AF"></td> <td bgcolor="#05BDB0"></td> <td bgcolor="#FEFCC2"></td> <td bgcolor="#FAA72A"></td> <td bgcolor="#CD3450"></td> </tr> </table> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM