【问题标题】:Table with fixed border-cells具有固定边框单元的表格
【发布时间】:2014-05-19 22:48:56
【问题描述】:

意图

  1. 我想使用所有可用的水平和垂直空间(也就是说整个浏览器窗口减去边距和页眉等)绘制一个表格。
  2. 调整浏览器窗口大小时,表格应动态调整大小。
  3. 它不应该比页面更宽或更长,如果内容大于单元格,它应该滚动内容。这些单元格在下图中用箭头标记。
  4. 在一个方向上没有箭头的单元格既不能改变它们的大小,也不能在那个方向滚动,而是固定的。因此,表头应该只会变宽或变窄,左右条纹只会改变它们的长度。

.

顺便说一句:我正在寻找纯 CSS/HTML 解决方案?

天真的尝试

我以为它会是 HTML 101:Example,但表格只会增长,直到显示整个内容,高度/宽度大小被忽略 css:

/* structure */
table {
    width: 80%;
    margin-left: 10%;
    height: 80%;
}
.right, .left {
    width: 200px;
}
.thead > td, .tfoot > td {
    height: 200px;
}
.center, .tbody > td {
  overflow: auto;
}

/* content boxes */
#content_lu, #content_ru, #content_lb, #content_rb {
    background-color: yellow;
    height: 200px;
    width: 200px;
}
#content_cu, #content_cb {
    background-color: blue;
    width: 800px;
    height: 200px;
}
#content_lm, #content_rm {
    background-color: green;
    width: 200px;
    height: 800px;
}
#content_cm {
    background-color: red;
    width: 800px;
    height: 800px;
}

和html:

<table>
    <thead><tr>
        <td class="left"><div id="content_lu"></div></td>
        <td class="center"><div id="content_cu"></div></td>
        <td class="right"><div id="content_ru"></div></td>
    </tr></thead>
    <tbody><tr>
        <td class="left"><div id="content_lm"></div></td>
        <td class="center"><div id="content_cm"></div></td>
        <td class="right"><div id="content_rm"></div></td>
    </tr></tbody>
    <tfoot><tr>
        <td class="left"><div id="content_lb"></div></td>
        <td class="center"><div id="content_cb"></div></td>
        <td class="right"><div id="content_rb"></div></td>
    </tr></tfoot>
</table>

Div-Try

我能够满足第一行的所有条件,使用绝对定位的角 div 和使用 margin-left 和 -right 的中心 div 将其装箱到父 div 中。但我不能定位例如最后一个居中的 div 因为绝对定位它不会被父 div 绑定。 Example

css:

/* Structure */
div.main {
   position: relative;
    width: 80%;
    margin-left: 10%;
    height: 80%;
}

/* corners */
div.top.left {
    position: absolute;
    left: 0;        top: 0;
    width: 200px;   height: 200px;
}
div.top.right {
    position: absolute;
    right: 0;        top: 0;
    width: 200px;   height: 200px;
}
div.bottom.left {
    position: absolute;
    left: 0;        bottom: 0;
    width: 200px;   height: 200px;
}
div.bottom.right {
    position: absolute;
    right: 0;       bottom: 0;
    width: 200px;   height: 200px;
}

/* edges */
div.top.center {
    margin-left: 200px;
    margin-right: 200px;
    height: 200px;
    overflow: auto;
}
div.bottom.center {
    margin-left: 200px;
    margin-right: 200px;
    height: 200px;
    overflow: auto;
}
div.middle.left {
    margin-top: 200px;
    margin-bottom: 200px;
    width: 200px;
    overflow: auto;
}
div.middle.right {
    margin-top: 200px;
    margin-bottom: 200px;
    width: 200px;
    overflow: auto;
}

/* center */
div.middle.center {
    margin-top: 200px;
    margin-bottom: 200px;
    margin-left: 200px;
    margin-right: 200px;
    overflow: auto;
}

/* Content boxes */
#content_lu, #content_ru, #content_lb, #content_rb {
    background-color: yellow;
    height: 200px;
    width: 200px;
}
#content_cu, #content_cb {
    background-color: blue;
    width: 800px;
    height: 200px;
}
#content_lm, #content_rm {
    background-color: green;
    width: 200px;
    height: 800px;
}
#content_cm {
    background-color: red;
    width: 800px;
    height: 800px;
}

html:

<div class="main">
    <div class="top    left">  <div id="content_lu"></div></div>
    <div class="top    center"><div id="content_cu"></div></div>
    <div class="top    right"> <div id="content_ru"></div></div>
    <div class="middle left">  <div id="content_lm"></div></div>
    <div class="middle center"><div id="content_cm"></div></div>
    <div class="middle right"> <div id="content_rm"></div></div>
    <div class="bottom left">  <div id="content_lb"></div></div>
    <div class="bottom center"><div id="content_cb"></div></div>
    <div class="bottom right"> <div id="content_rb"></div></div>
</div>

【问题讨论】:

  • 您是在直接询问您的问题的答案!!,而不是展示您尝试了什么,如果您向我们展示您所做或尝试过的事情,我们将不胜感激,这样我们就可以开始帮助您那里
  • @hoffmann:你的意思是:jsfiddle.net/abhitalks/VsA33/1
  • webkit-scroll 是您的答案。但并非所有浏览器都支持。但是您可以通过 css2 实现这一点,通过创建两个元素,外部元素 `overflow:hidden' 和内部元素设置为 'overflow:scroll'
  • @AtalShrivastava 什么是“webkit-scroll”?我只发现 webkit-overflow-scrolling 似乎只对触摸设备很重要。
  • @abhitalks 该示例不会垂直滚动,也不会垂直扩展。

标签: html css html-table


【解决方案1】:

这是使用绝对定位实现布局的一种方式。

对于 HTML:

<div class="wrapper">
    <div class="left">
        <div class="top">top</div>
        <div class="mid">
            <p>Lorem ipsum dolor sit amet...</p>
        </div>
        <div class="bot">bot</div>
    </div>
    <div class="center">
        <div class="top">
            <p>Lorem ipsum dolor sit amet...</p>
        </div>
        <div class="mid">
            <p>Lorem ipsum dolor sit amet...</p>
        </div>
        <div class="bot"><p>Lorem ipsum dolor sit amet...</p></div>
    </div>
    <div class="right">
        <div class="top">top</div>
        <div class="mid">
            <p>Lorem ipsum dolor sit amet...</p>
        </div>
        <div class="bot">bot</div>
    </div>
</div>

对于 CSS:

body, html {
    height: 100%;
    margin: 0;
    padding: 0;
}
.wrapper {
    border: 1px dotted gray;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}
.left {
    position: absolute;
    left: 0;
    width: 200px;
    top: 0;
    bottom:0;
    background-color: yellow;
}
.center {
    position: absolute;
    left: 200px;
    right: 200px;
    top: 0;
    bottom:0;
}
.right {
    position: absolute;
    right: 0;
    width: 200px;
    top: 0;
    bottom:0;
    background-color: yellow;
}
.top {
    position: absolute;
    background-color: cyan;
    top: 0;
    left: 0;
    height: 50px;
    width: 100%;
}
.mid {
    position: absolute;
    top: 50px;
    bottom: 50px;
    width: 100%;
}
.bot {
    position: absolute;
    background-color: cyan;
    bottom: 0;
    left: 0;
    height: 50px;
    width: 100%;
}
p {
    margin: 0;
    height: 100%;
    overflow: auto
}

查看演示:http://jsfiddle.net/audetwebdesign/sfrXp/

注意:我将高度和某些位置偏移设置为 50 像素而不是 200 像素,因为 jsFiddle 面板有点小,但这个概念仍然有效。

要处理溢出单元格的内容,请将overflow: autoheight: 100% 设置为保存内容的块级元素,在本例中为p

您可能还需要将height: 100% 分配给bodyhtml 标记以定义根(屏幕)元素的垂直尺寸。

【讨论】:

  • 这正是我想要的。我修改了您的代码,使其看起来像我的两个示例。 cssdesk.com/G3N4r
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多