【问题标题】:Css table-cell rule and auto width overflowingCSS表格单元格规则和自动宽度溢出
【发布时间】:2014-10-14 05:36:41
【问题描述】:

我在使用 display: table 和 display: table-cell 时遇到问题。

提琴:http://jsfiddle.net/5q51sbqb/1/

我有一个带有 display:table 的 div;并在这两个 div 中显示:table-cell;

左边的 div (.t1) 是固定宽度,右边的 div(.t2) 应该占据容器边缘的剩余空间。

我的问题在于在右侧 div(.t2) 中添加一个长 div (2000px)。我基本上需要内容窗口与其父窗口保持相同的宽度,而不会超出容器的范围,以允许滚动其中的内容。

请记住,这需要不使用固定宽度,因为容器和 t2 都是响应式的。而且我还必须使用表格和表格单元格显示属性:(

所以基本上 .t2 div 的子项在我需要它们以适应容器宽度时超出容器(无需在内容窗口上设置固定宽度......和 ​​.t2 div 上)

我被难住了。

HTML

        </div>
    </div>
    <div class="table-cell t2">
        <div id="content-window">
            <div id="content"></div>
        </div>
   </div>
</div>
</div>

CSS

#container{
   width: 600px;
   height: 600px;
   margin: 0px auto;
   background-color:green;
   padding: 2px;
}

#table{
   display:table;
   width:100%;
}

.table-cell{
   display:table-cell;
   height: 300px;
   padding:2px;
}

.t1{
   width: 100px;
   background-color: red;
}
.t2{
   width:auto;
   background-color: blue;
}

#content-window{
   width:100%;
   overflow:scroll;
}

#content{
   width: 2000px;
   height: 50px;
   background-color:yellow;
}

【问题讨论】:

    标签: html css


    【解决方案1】:

    既然您巧妙地创建了#content-window,请将其设置为position: absolute;,这样就不会弄乱单元格的自动宽度。请记住将.t2 设置为position: relative,这样#content-window 可能会使用右侧表格单元格的包含空间填充它的宽度和高度。

    提示:如果您希望它仅水平滚动,请使用overflow-x。

    #container{
        width: 600px;
        height: 600px;
        margin: 0px auto;
        background-color:green;
        padding: 2px;
    }
    
    #table {
        display:table;
        width:100%;
    }
    
    .table-cell{
        display:table-cell;
        height: 300px;
        padding:2px;
    }
    
    .t1 {
        width: 100px;
         background-color: red;
    }
    .t2 {
        position: relative;
        background-color: blue;
    }
    
    #content-window{
        position: absolute;
        width:100%;
        height: 100%;
        overflow: hidden;
        overflow-x: scroll;
        height:50px;
    }
    
    #content{
        width: 2000px;
        height: 50px;
        background-color:yellow;
    }
    

    在这里查看:http://jsfiddle.net/5q51sbqb/4/

    【讨论】:

    • 太棒了,先生,非常感谢。
    猜你喜欢
    • 2017-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 2013-08-24
    • 2011-01-27
    相关资源
    最近更新 更多