【问题标题】:CSS 3 Column Liquid Layout Dynamic Same Height ColumnCSS 3列液体布局动态等高列
【发布时间】:2013-10-12 16:18:25
【问题描述】:

我想知道如何制作一个液体(15%,70%,15%) 3 列 css 布局具有动态等高列,其中每列动态匹配最长列的高度(换句话说:根据内容每列,如果第 1 列长于 2 和 3,则第 2 列和第 3 列应自动与第 1 列高度相同)有没有办法做到这一点,我看过圣杯:http://alistapart.com/article/holygrail 它说它不适用于等高的列。我想知道我是否可以修改我的 css 代码来做到这一点。

CSS 代码:

/*    Generated by http://www.cssportal.com    */

/*@import url("/robotics/css/reset.css");*/

html,body {
    background:url(background.jpg') no-repeat center center fixed;
    -webkit-background-size: cover; /* For WebKit*/
    -moz-background-size: cover;    /* Mozilla*/
    -o-background-size: cover;      /* Opera*/
    background-size: cover;         /* Generic*/
    font-family: Verdana, Arial, Helvetica, sans-serif;
    /*font-size: 13px;*/
    color:#FFFFFF;
    text-align:center;


}
ul {
text-align:center;
margin-left: -40px;
}
ul li {
    display:block;
font-size:10pt;
padding: 0px 15px 0px 15px;
}
ul li a{
margin: 0 auto;
}
ul li a:link {
color:#fff;
text-decoration:none;
}
ul li a:visited {
color:#fff;
text-decoration:none;
}
ul li a:hover{
color:#fff;
text-decoration:none;
}
ul li a:active{
color:#fff;
text-decoration:none;
}


p {
font-size: 10pt;
    padding: 10px;
}

#wrapper {
    width: 100%;
    min-width: 768px;
    /*max-width: 900px;*/
    margin: 0 auto;
}

#headerwrap {
    width: 100%;
    float: left;
    margin: 0 auto;
}

#header {
    height: 100px;
    /*border-radius: 10px;*/
    /*border: 1px solid #FFFFFF;*/
    margin: 5px;
}
#header img {
width: 70%;
    height: 100%;
float:left;
margin-left:15%;    

}
#contentliquid {
    float: left;
    width: 100%;
}

#contentwrap {
    margin-left: 15%;
    margin-right: 15%;
    float:left;
    width:70%;

}

#content {
    border-radius: 10px;
    border: 1px solid #FFFFFF;
    margin: 5px;
    height: 500px;
}

#leftcolumnwrap {
    width: 15%;
    margin-left:-100%;
    float: left;

}

#leftcolumn {
    border-radius: 10px;
    border: 1px solid #FFFFFF;
    margin: 5px;height: 500px;
}

#rightcolumnwrap {
    width: 15%;
    margin-left: -15%;
    float: left;
}

#rightcolumn {
    border-radius: 10px;
    border: 1px solid #FFFFFF;
    margin: 5px;height: 275px;
}

#footerwrap {
    width: 100%;
    float: left;
    margin: 0 auto;
    clear: both;
}

#footer {
    height: 100px;
    border-radius: 10px;
    border: 1px solid #FFFFFF;
    margin: 5px;
}

HTML 页面:

<html>

<head>
    <link rel="stylesheet" type="text/css" href="page.css">
    <title>Sample</title>
</head>
<body>
<div id="wrapper">
<div id="headerwrap">
<div id="header">
    <p>This is the header.</p>
</div>
</div>
<div id="contentliquid"><div id="contentwrap">
<div id="content">
    <p>This is the center column. Please make me the same height as everyone else!</p>
</div>
</div></div>
<div id="leftcolumnwrap">
<div id="leftcolumn">
    <p>This is the left column. Please make me the same height as everyone else!</p>
</div>
</div>
<div id="rightcolumnwrap">
<div id="rightcolumn">
    <p>This is the right column. Please make me the same height as everyone else!</p>
</div>
</div>
<div id="footerwrap">
<div id="footer">
    <p>This is the footer.</p>
</div>
</div>
</div>

有没有办法让所有列动态的高度相同?

【问题讨论】:

    标签: css multiple-columns fluid-layout


    【解决方案1】:

    您应该尝试使用display: table-cell;(这需要将父元素设置为display: table; 表格单元格元素始终共享其容器的高度,并且它们的容器(如果未另外设置)将始终具有其最大高度孩子。

    看看这个小提琴的例子:

    http://jsfiddle.net/kLMtb/

    您的 html 可能还需要重新格式化,我在该示例中更改了一些内容,因此请看一下。首先,中心列需要放在 html 的左右列之间。

    看看这个关于css表格显示属性的解释:

    http://ajaxian.com/archives/display-table

    【讨论】:

    • 感谢您的帮助!不过,我必须问,是否有办法让第三列(右侧)随着页面滚动而浮动,并使边框只与该列需要的一样大?
    【解决方案2】:

    我知道有两种方法可以实现等高的列。

    1) CSS tables

    FIDDLE

    标记:

    <div id="header">
        <p>This is the header.</p>
    </div>
    
    
    <div class="wpr">
        <div id="leftcolumn">
            <p>This is the left column. Please make me the same height as everyone else!</p>
        </div>
        <div id="contentliquid">
            <p>Some content</p>
        </div> 
    
        <div id="rightcolumn">
            <p>This is the right column. Please make me the same height as everyone else!</p>
        </div>
    </div>
    <div id="footer">
        <p>This is the footer.</p>
    </div>
    

    CSS

    #header {
        height: 100px;
        background: orange;
    }
    .wpr
    {
        display:table;
    }
    #leftcolumn
    {
        width: 200px;
        background: aqua;
        display:table-cell;
    }
    #rightcolumn
    {
        width: 200px;
        background: pink;
        display:table-cell;
    }
    
    #contentliquid {
        background: yellow;
        overflow:hidden;
        display:table-cell;
    }
    
    #footer
    {
        clear:both;
        background: green;
    }
    

    2) Faux columns

    需要带有repeat-y的背景图片(阅读上面的文章)。

    类似这样的:

    background: #ccc url(../images/bg_birch_800.gif) repeat-y 50% 0;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-13
      • 2014-02-18
      • 2013-03-26
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 2011-07-23
      相关资源
      最近更新 更多