简单HTML文档中的CSS DIV问题

时间:2012-06-19 22:00:37

标签: html css

我有一个2列HTML / CSS布局,我遇到<div>sidebar</div>的问题,其中div不会一直向内扩展(换句话说,存在很大的差距)在“侧边栏”底部和“页脚”div之间,它会导致我想要摆脱的丑陋的白色空间。

sandbox.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>My Title</title>
<link href="twoColLiqLtHdr.css" rel="stylesheet" type="text/css" />
<!--[if lte IE 7]>
    <style>
        .content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */
        ul.nav a { zoom: 1; }  /* the zoom property gives IE the hasLayout trigger it needs to correct extra whiltespace between the links */
    </style>
<![endif]-->
</head>

<body>

<div class="container">
  <div class="header">
  My Header
  </div>
  <div class="sidebar1">
    <ul class="nav">
      <li><a href="#">Link 1</a></li>
      <li><a href="#">Link 2</a></li>
      <li><a href="#">Link 3</a></li>
      <li><a href="#">Link 4</a></li>
    </ul>
    <p>&nbsp;</p>
    </div>
  <div class="content">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent luctus dolor nec ante imperdiet malesuada. Phasellus nec ipsum ipsum. Pellentesque lacus elit, tempor vitae laoreet nec, condimentum vel magna. Mauris imperdiet consectetur egestas. Praesent pellentesque, turpis ultricies elementum pellentesque, felis arcu consequat diam, quis viverra libero nibh a massa. Proin nec lectus at lorem semper hendrerit. Curabitur sed diam ut nisi ultrices vestibulum vel eget leo. Donec dapibus sem vel ipsum vestibulum suscipit. Donec faucibus ipsum eu neque facilisis blandit. Vivamus rhoncus odio in nibh pretium elementum.

Morbi adipiscing odio eu nibh gravida eu cursus risus luctus. Cras malesuada fermentum fermentum. Aliquam neque magna, pellentesque nec lobortis eget, scelerisque eu elit. Curabitur commodo leo porttitor eros commodo a venenatis odio vehicula. Aenean sodales diam ac orci interdum aliquam. Nulla sodales enim ut leo porttitor in mollis est consequat. Morbi lobortis nunc nec mi varius varius. Mauris ac velit eget augue cursus viverra. Mauris ut felis vehicula urna aliquet sodales ut vel purus. Pellentesque sed mi felis. In leo urna, dignissim et vestibulum tristique, lobortis quis est. Nulla tincidunt consequat mi a volutpat. Aenean ut arcu nibh, ut placerat augue. Etiam sollicitudin orci id neque ornare a euismod metus imperdiet.
  </div>
  <div class="footer">
    My Footer
    </div>
</body>
</html>

twoColLiqLtHdr.css:

@charset "UTF-8";
body {
    font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
    background: #699643;
    margin: 0;
    padding: 0;
    color: #000;
}

/* ~~ Element/tag selectors ~~ */
ul, ol, dl { /* Due to variations between browsers, it's best practices to zero padding and margin on lists. For consistency, you can either specify the amounts you want here, or on the list items (LI, DT, DD) they contain. Remember that what you do here will cascade to the .nav list unless you write a more specific selector. */
    padding: 0;
    margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
    margin-top: 0;   /* removing the top margin gets around an issue where margins can escape from their containing div. The remaining bottom margin will hold it away from any elements that follow. */
    padding-right: 15px;
    padding-left: 15px; /* adding the padding to the sides of the elements within the divs, instead of the divs themselves, gets rid of any box model math. A nested div with side padding can also be used as an alternate method. */
}
a img { /* this selector removes the default blue border displayed in some browsers around an image when it is surrounded by a link */
    border: none;
}

/* ~~ Styling for your site's links must remain in this order - including the group of selectors that create the hover effect. ~~ */
a:link {
    color:#414958;
    text-decoration: none; /* unless you style your links to look extremely unique, it's best to provide underlines for quick visual identification */
}
a:hover {
    color:#414958;
    text-decoration: underline;
}

a:visited {
    color: #4E5869;
    text-decoration: underline;
}
a:hover, a:active, a:focus { /* this group of selectors will give a keyboard navigator the same hover experience as the person using a mouse. */
}

/* ~~ this container surrounds all other divs giving them their percentage-based width ~~ */
.container {
    width: 80%;
    max-width: 1260px;/* a max-width may be desirable to keep this layout from getting too wide on a large monitor. This keeps line length more readable. IE6 does not respect this declaration. */
    min-width: 1260px;/* a min-width may be desirable to keep this layout from getting too narrow. This keeps line length more readable in the side columns. IE6 does not respect this declaration. */
    background: #FFF;
    margin: 0 auto; /* the auto value on the sides, coupled with the width, centers the layout. It is not needed if you set the .container's width to 100%. */
    overflow: hidden;
}

/* ~~ the header is not given a width. It will extend the full width of your layout. It contains an image placeholder that should be replaced with your own linked logo ~~ */
.header {
    background: #A9B92E;
}

.sidebar1 {
    float: left;
    width: 20%;
    background: #94c493;
    padding-bottom: 10px;
}
.content {
    padding: 10px 0;
    width: 80%;
    float: left;
}

/* ~~ This grouped selector gives the lists in the .content area space ~~ */
.content ul, .content ol { 
    padding: 0 15px 15px 40px; /* this padding mirrors the right padding in the headings and paragraph rule above. Padding was placed on the bottom for space between other elements on the lists and on the left to create the indention. These may be adjusted as you wish. */
}

/* ~~ The navigation list styles (can be removed if you choose to use a premade flyout menu like Spry) ~~ */
ul.nav {
    list-style: none; /* this removes the list marker */
    border-top: 1px solid #666; /* this creates the top border for the links - all others are placed using a bottom border on the LI */
    margin-bottom: 15px; /* this creates the space between the navigation on the content below */
}
ul.nav li {
    border-bottom: 1px solid #666; /* this creates the button separation */
}
ul.nav a, ul.nav a:visited { /* grouping these selectors makes sure that your links retain their button look even after being visited */
    padding: 5px 5px 5px 15px;
    display: block; /* this gives the link block properties causing it to fill the whole LI containing it. This causes the entire area to react to a mouse click. */
    text-decoration: none;
    background: #E8EEC7;
    color: #000;
}
ul.nav a:hover, ul.nav a:active, ul.nav a:focus { /* this changes the background and text color for both mouse and keyboard navigators */
    background: #FDEAA6;
    color: #000;
}

/* ~~ The footer ~~ */
.footer {
    padding: 10px 0;
    background: #A9B92E;
    position: relative;/* this gives IE6 hasLayout to properly clear */
    clear: both; /* this clear property forces the .container to understand where the columns end and contain them */
}

/* ~~ miscellaneous float/clear classes ~~ */
.fltrt {  /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
    float: right;
    margin-left: 8px;
}
.fltlft { /* this class can be used to float an element left in your page. The floated element must precede the element it should be next to on the page. */
    float: left;
    margin-right: 8px;
}
.clearfloat { /* this class can be placed on a <br /> or empty div as the final element following the last floated div (within the #container) if the #footer is removed or taken out of the #container */
    clear:both;
    height:0;
    font-size: 1px;
    line-height: 0px;
}




/* style the results tables */
#stylized p {
    font-size:11px;
    color:#666666;
    margin-bottom:20px;
    border-bottom:solid 1px #b7ddf2;
    padding-bottom:10px;
}
table.decorated {
  margin: 1em 1em 1em 2em;
  background: whitesmoke;
  border-collapse: collapse;
}
table.decorated th, table.decorated td {
  border: 1px silver solid;
  padding: 0.2em;
}
table.decorated th {
  background: gainsboro;
  text-align: left;
}
table.decorated caption {
  margin-left: inherit;
  margin-right: inherit;
}
table.decorated tr:hover {
   background: lightsteelblue !important;
}


/* Create the scrollable "boxes" */
.scrollArea {
width: 200px; 
height: 300px;
padding-left: 5px;
padding-right: 5px; 
border-color: #6699CC;
border-width: 1px; 
border-style: solid;
float: left; 
overflow: auto;
}
</style>

4 个答案:

答案 0 :(得分:2)

啊,让列垂直排列。 CSS的黑暗面。您有两种选择:

  1. 使用一些稍微复杂但有据可查的CSS。 This fellow here有一个很好的模板,您可以使用我之前使用过的,它效果很好。
  2. 的JavaScript。使用jQuery,您可以在渲染完所有内容后将较小div的高度设置为较大div的高度。

答案 1 :(得分:0)

除非您需要支持IE7或更低版​​本,否则请停止使用浮动进行布局:

.sidebar1 {
    display:table-cell;
    width: 20%;
    background: #94c493;
    padding-bottom: 10px;
}
.content {
    padding: 10px 0;
    width: 80%;
    display: table-cell;
}

Example。您可能需要在.content元素中添加一些额外的填充,因为边距在display: table-cell;like this内不起作用。

答案 2 :(得分:0)

如果你不关心IE7,你可以这么简单:

<style>
    div.sidebar_ {
        display:        table-cell;
        width:          20%;
        background:     red;
    }

    div.content_ {
        display:        table-cell;
        width:          80%;
        background:     blue;
    }
</style>

<div class="sidebar_">
    Sidebar
</div>
<div class="content_">
    Content<br>
    Content<br>
    Content<br>
    Content<br>
</div>

适用于IE8以及更好的浏览器。

答案 3 :(得分:0)

在某些情况下,可以通过将bg图像添加到父容器中,或者通过将列拆分为header-content-footer部分并将每个部分的页脚放在单独的容器中来伪造等高的列(并执行如果需要,标题部分也相同)。如果列具有固定宽度,您将有更多可用选项。

<div class="container-with-bg-image-for-all-columns">
    <div class="col1"></div>
    <div class="col2"></div>
    <div class="col3"></div>
</div>

...或

<div class="content-container">
    <div class="col1-content"></div>
    <div class="col2-content"></div>
    <div class="col3-content"></div>
</div>
<div class="footer-container">
    <div class="col1-footer"></div>
    <div class="col2-footer"></div>
    <div class="col3-footer"></div>
</div>

如果需求足够大,请不要忘记为列使用HTML表的选项。这是完全控制列大小和对齐的唯一简便方法。我没有测试display:table和其他与表相关的显示值,但这可能是获得HTML表所提供的垂直优势的另一种方法。