0

我有一个 2 列的 HTML/CSS 布局,我遇到了一个问题,<div>sidebar</div>即 div 没有一直扩展到内容的一侧(换句话说,“侧边栏”的底部之间有很大的间隙div 和“页脚” div,它会导致我想摆脱的丑陋的空白。

沙盒.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

4 回答 4

2

啊,让列垂直排列。CSS 的阴暗面。你有两个选择:

  1. 使用一些稍微复杂但有据可查的 CSS。这里的这个家伙有一个很好的模板,你可以使用我以前用过的,而且效果很好。
  2. JavaScript。使用 jQuery,您可以在所有内容呈现后将较小 div 的高度设置为较大 div 的高度。
于 2012-06-19T22:05:57.417 回答
0

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

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

例子。您可能想为.content元素添加一些额外的填充,因为边距在内部不起作用display: table-cell;就像这样

于 2012-06-19T22:11:24.980 回答
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 和更好的浏览器以及任何其他浏览器。

于 2012-06-19T22:13:06.733 回答
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 表格提供的垂直优势的另一种方式。

于 2012-06-19T22:15:56.743 回答