【问题标题】:Responsive CSS table layout make one cell act like a row响应式 CSS 表格布局使一个单元格像一行一样
【发布时间】:2015-07-21 04:08:10
【问题描述】:

我有一个布局设计,我试图通过display:table-cell 完成。然而,这可能不是最好的选择,因为它会给我带来问题。

期望的结果:

我希望在较小的屏幕(小于 992 像素)上具有这样的布局:

+----+----+
|    |    |
+----+----+
|         |
+---------+

但随后在更大的屏幕(大于 992 像素)上具有这种布局:

+--+--+--+
|  |  |  |
+--+--+--+

需要注意的是,它们可能不具有相同大小的内容,我希望它们垂直拉伸到相同的高度。

我目前正在使用:

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

.cell {
    display:table-cell;
    width:50%;
    position:relative;
    padding:0px 10px;
    border:1px solid #000;
}

我的问题是为了让第三个单元格环绕,我需要给它自己的行。但随后,单元格将仅展开以适合前 50%(在第一个单元格下方),而不是拉伸 100%。可以通过将第二行设为自己的表格来解决此问题,但事实证明,以正确的屏幕尺寸并排放置两个表格也很困难。

如果有更好的方法使 div 匹配高度,那将是理想的。我正在使用引导程序,所以使用它的网格很容易,只要我可以使它们都具有相同的高度。

注意:人造列不适用于我的场景,因为背景的颜色/间距不合适。

【问题讨论】:

    标签: html css responsive-design


    【解决方案1】:

    您可以简单地使用table-layout:fixed 来确保单元格的宽度相等。

    对于@media 查询部分,使用display:table-caption 作为最后一个单元格,并设置caption-side:bottom 来指定位置。

    JSFiddle: http://jsfiddle.net/7kpcf46r/(调整输出帧大小并查看)

    .table {
        display: table;
        table-layout: fixed;
        border-collapse: collapse;
        width: 100%;
    }
    .cell {
        display: table-cell;
        border: 1px solid red;
    }
    @media (max-width: 768px) {
        .cell:last-child {
            display: table-caption;
            caption-side: bottom;
        }
    }
    <div class="table">
        <div class="cell">One Line</div>
        <div class="cell">Two<br/>Lines</div>
        <div class="cell">More<br/>Than two<br/>Lines</div>
    </div>

    【讨论】:

    • 我很高兴 :) 这对我来说是一个非常经典的问题。我相信它将来会得到更多的点击,如果你能改进帖子标题并简化问题会更好。
    • 当然,虽然这是我能想到的最好的。如果您有任何建议,我很乐意更新。
    • @Pangloss,刚刚通过你的回答,你认为像iahchemicals.com 这样的网站可以做出响应,也许不是 100% .. 但在一定程度上?你觉得呢?
    【解决方案2】:

    您可以使用媒体查询根据屏幕宽度设置内容样式,例如,给定 HTML:

    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
    

    CSS:

    /* Setting the defaults, to apply regardless of
       screen width: */
    html, body {
        /* removing margin and padding, setting
           font-size to 0 in order to prevent
           new-lines creating space between sibling
           elements: */
        margin: 0;
        padding: 0;
        font-size: 0;
    }
    
    /* resetting the font-size for all descendants of
       the body element: */
    body * {
        font-size: 16px;
    }
    
    div.cell {
        /* border-box, causes the browser to include
           the width of the borders and padding within
           the assigned width of the element: */
        box-sizing: border-box;
        /* instead of table-cell, causing elements
           to default to displaying side-by-side: */
        display: inline-block;
        /* the default behaviour is to show at
           50% of the width of the parent (body)
           element: */
        width: 50%;
        margin: 0;
        padding: 0;
        height: 40vh;
        border: 2px solid #000;
    }
    
    /*
       Note: in this demo I'm using screen widths of
             of 600px (min/max) to demonstrate the
       principles within the constraints of JS Fiddle's
       limited viewport. In your project, obviously, use
       your required width of 992px.
    */
    
    /* in screen-widths less than 600px
       the last-child has a width of 100%: */
    @media all and (max-width: 600px) {
        div.cell:last-child {
            width: 100%;
        }
    }
    
    /* in screen-widths greater than 600px
       div.cell elements have a width of 33%
       (including the last-child) to display
       side-by-side across the viewport: */
    @media all and (min-width: 600px) {
        div.cell {
            width: 33%;
        }
    }
    

    JS Fiddle demo.

    参考资料:

    【讨论】:

    • 我已经在使用媒体查询来检测 992px 的屏幕尺寸。您的示例的问题是您定义了元素的高度。我需要根据最高的内容来调整高度。
    • 您能否展示足够多的您自己的代码,以便我们重现您的问题?想想 minimal / "MCVE" 代码。
    • 不认为代码是描述问题的必要条件。我已经更新了你的小提琴jsfiddle.net/sharf224/kq0g38oq/2 来说明我的意思。当前两个单元格连续时,我希望它们具有相同的高度。第三个单元格无关紧要。当所有 3 个并排时,它们应该都是相同的高度。
    • @sharf:这三个元素是否在某种包装器中?并且代码(几乎)总是必要的。我建议在您的问题中添加所述代码,以及指向您上面提供的 JS Fiddle 的链接,或者您认为可能更好地展示您的情况的另一个链接。
    【解决方案3】:

    你可以这样做: http://jsfiddle.net/z1kwrm8p/2/

    HTML:

    <div class="table">
        <div class="cell" id="c1">ABCD</div><!--
     --><div class="cell" id="c2">EFGH</div><!--
     --><div class="cell" id="c3">XYZ PQRS ABCD EFGH IJKL MNOP QRSTUVWXYZ</div>
    </div>
    

    CSS:

    .table {
        width:100%;
        display:block;
        background:#009933;
    }    
    
    .cell {
        display:inline-block;
        width:50%;
    }
    
    
    #c1 {
        background:#99ccff;
    }
    
    #c2 {
        background:#ff9966;
    }  
    
    #c3 {
        width:100%;
        background:#66FF66;
    }
    
    @media only screen and (max-width: 991px) {
      /* rules that only apply for canvases narrower than 992px */
        .table {
            display:table;
        }
    
        .cell {
            display:table-cell;
            width:33.3%;
        }    
    
        #c3 {
            width:33.3%;
        }
    }
    

    【讨论】:

    • 这非常接近,但是在折叠视图中(顶部 2 个,底部 1 个)如果这两个中的一个比另一个高,您会遇到相同的问题,即它们的高度不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2013-06-25
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多