液体布局的css - 高度取决于一列的内容

时间:2015-11-11 01:55:55

标签: html css

我正在使用液体布局构建新页面,并在此处推荐screen.css

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB">
<head>
    <link rel="stylesheet" type="text/css" href="screen.css" media="screen" />
</head>
<body>

<div id="header">
    Header
</div>
<div class="colmask leftmenu">
    <div class="colleft">
        <div class="col1">
            col 1
        </div>
        <div class="col2">
            col 2
        </div>
    </div>
</div>
<div id="footer">
    Footer
</div>

</body>
</html>

CSS:

<style media="screen" type="text/css">
/* <!-- */
html,body {
    height:100%;
}

body {
    margin:0;
    padding:0;
    border:0;           /* This removes the border around the viewport in old versions of IE */
    width:100%;
    background:#fff;
    min-width:600px;        /* Minimum width of layout - remove line if not required */
                    /* The min-width property does not work in old versions of Internet Explorer */
    font-size:90%;
}
a {
    color:#369;
}
a:hover {
    color:#fff;
    background:#369;
    text-decoration:none;
}
h1, h2, h3 {
    margin:.8em 0 .2em 0;
    padding:0;
}
p {
    margin:.4em 0 .8em 0;
    padding:0;
}
img {
    margin:10px 0 5px;
}


/* Header styles */
#header {
    clear:both;
    float:left;
    width:100%;
}
#header {
    border-bottom:1px solid #000;
}
#header p,
#header h1,

/* 'widths' sub menu */
#layoutdims {
    clear:both;
    background:#eee;
    border-top:4px solid #000;
    margin:0;
    padding:6px 15px !important;
    text-align:right;
}
/* column container */
.colmask {
    position:relative;  /* This fixes the IE7 overflow hidden bug */
    clear:both;
    float:left;
    width:100%;         /* width of whole page */
    overflow:hidden;        /* This chops off any overhanging divs */
}
/* common column settings */
.colright,
.colmid,
.colleft {
    float:left;
    width:100%;
    position:relative;
}
.col1,
.col2,
.col3 {
    float:left;
    position:relative;
    padding:0 0 1em 0;
    overflow:hidden;
}
/* 2 Column (left menu) settings */
.leftmenu {
    background:#fff;        /* right column background colour */
}
.leftmenu .colleft {
    right:75%;          /* right column width */
    background:#f4f4f4; /* left column background colour */
}
.leftmenu .col1 {
    width:71%;          /* right column content width */
    left:102%;          /* 100% plus left column left padding */
}
.leftmenu .col2 {
    width:21%;          /* left column content width (column width minus left and right padding) */
    left:6%;            /* (right column left and right padding) plus (left column left padding) */
}
/* Footer styles */
#footer {
    clear:both;
    float:left;
    width:100%;
    border-top:1px solid #000;
}
#footer p {
    padding:10px;
    margin:0;
}
/* --> */
</style>

这几乎可以根据需要工作,除了我试图让布局的高度取决于Col 1 - 也就是说:

a)当Col1中的内容不多时,布局应该占据浏览器窗口的整个高度

b)当Col1中的内容多于浏览器窗口的高度时,整个浏览器窗口(不仅仅是Col1)会得到一个垂直滚动条

我尝试过设置:

html,body {
height:100%;
}

但它没有按照描述工作。我本以为这是一个相当普遍的场景,但是在尝试学习CSS /布局时,很难从'坏/过时'中排出'好'的建议

1 个答案:

答案 0 :(得分:1)

您可以使用flex box

执行此操作
body {
  display: flex;          /* make the body a flex box */
  flex-direction: column; /* children go down the screen */
}

.leftmenu {
  flex: 1;                /* resize to fill the space */
  overflow: auto;         /* overflow as needed */
}

在这个小提琴中,调整窗口大小以根据需要显示滚动条:

http://jsfiddle.net/qrf29pop/