自动调整div高度

20

只有设置精确的高度时,浏览器才会显示div。但是我想创建一个根据内容可调整大小的div。已尝试height:auto和height:100%,但无效。

我的div看起来像这样。它是侧边栏和内容的背景div。

.wrapper
    {
        width: 80%;
        height:200px;
        max-width: 1260px;
        min-width: 780px;
        margin: 0 auto;     
        background-image:url(core/design/img/transfff.png);
        -moz-border-radius: 15px 15px 0px 0px;
        border-radius: 15px 15px 0px 0px;
    }

更新 我的HTML看起来像这样

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style type="text/css">

body {
    font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif;
    background-image: url(core/design/img/bg.png);
    background-position:top left;
    background-size:100%;
    background-color:#fff;
    background-repeat:no-repeat;
    margin: 0;
    padding: 0;
    color: #000;
}


ul, ol, dl { 
    padding: 0;
    margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
    margin-top: 0;
    padding-right: 15px;
    padding-left: 15px; 
a img { 
    border: none;
}

a:link {
    color:#414958;
    text-decoration: underline; 
}
a:visited {
    color: #4E5869;
    text-decoration: underline;
}
a:hover, a:active, a:focus { 
    text-decoration: none;
}


.container {
    width: 80%;
    max-width: 1260px;
    min-width: 780px;
    margin: 0 auto; 
}

.wrapper
{
    width: 80%;
    height:200px;
    max-width: 1260px;
    min-width: 780px;
    margin: 0 auto; 
    background-image:url(core/design/img/transfff.png);
    -moz-border-radius: 15px 15px 0px 0px;
    border-radius: 15px 15px 0px 0px;
    overflow: visible
}

.header {
padding:20px;
}

.sidebar1 {
    float: left;
    width: 20%;

    padding-bottom: 10px;
}
.content {
    padding: 10px 0;
    width: 60%;
    float: left;
}
.sidebar2 {
    float: left;
    width: 20%;

    padding: 10px 0;
}

.content ul, .content ol { 
    padding: 0 15px 15px 40px; 
}


ul.nav {
    list-style: none; 
    border-top: 1px solid #666; 
    margin-bottom: 15px; 
}
ul.nav li {
    border-bottom: 1px solid #666; 
}
ul.nav a, ul.nav a:visited{
    padding: 5px 5px 5px 15px;
    display: block; 
    text-decoration: none;
    color: #000;
}
ul.nav a:hover, ul.nav a:active, ul.nav a:focus {
    background: #6F7D94;
    color: #FFF;
}

/* ~~The footer ~~ */
.footer {
    padding: 10px 0;
    position: relative;
    clear: both;
}


.fltrt {  
    float: right;
    margin-left: 8px;
}
.fltlft { 
    float: left;
    margin-right: 8px;
}
.clearfloat { /
    clear:both;
    height:0;
    font-size: 1px;
    line-height: 0px;
}
-->
</style><!--[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"><a href="#"><img src="core/design/img/logo.png" alt="Insert Logo Here" name="Insert_logo" width="438px" height="95" id="Insert_logo" style=" display:block; margin:0 auto;" /></a> 
    <!-- end .header --></div>
    <div class="wrapper">
  <div class="sidebar1">
</div>
  <div class="content">
    </div>
  <div class="sidebar2">
  </div>
    </div>
  <div class="footer">
  </div>
</div>
</body>
</html>

它的内容是什么意思?背景图片吗? - ChristopheCVB
请问您能否分享一下您使用 div.wrapper 的方式?是像这样 <div class="wrapper">内容内容</div> 还是这样 <div class="wrapper"/>内容内容 - Thor Jacobsen
问题已更新,请重新检查。 - Tural Ali
5个回答

80

div标签会根据其内容自然地调整大小。

如果您在div上没有设置高度,则它将扩展以包含其内容。

这个规则的一个例外是当div包含浮动元素时。如果是这种情况,您需要额外做一些工作来确保包含div(容器)清除浮动。

以下是一些方法:

#wrapper{
overflow:hidden;
}
或者
#wrapper:after
{
content:".";
display:block;
clear:both;
visibility:hidden;
}


如果您在 div 上未设置高度,则它将扩展以包含其内容。从 CSS 中删除高度后,内容后面的 div 完全消失了。 - Tural Ali
发布你的HTML代码。你可能做了一些不太对的事情。 - Jamie Dixon
1
从你的HTML代码来看,没有任何内容。问题是当高度未设置时,背景图片不显示吗?即使没有内容,你是否期望div元素能够扩展到背景图片的高度? - Jamie Dixon

4
请确保您的 div 内容以 clear:both 样式结束。

3

下面是该问题的最新解决方案:

在你的CSS文件中编写以下类名为.clearfix以及伪选择器:after

.clearfix:after {
content: "";
display: table;
clear: both;
}

然后,在您的HTML中,将.clearfix类添加到父Div中。例如:

<div class="clearfix">
    <div></div>
    <div></div>
</div>

它应该一直有效。您可以将类名称为.group而不是.clearfix,这样会使代码更具语义性。请注意,在Content的双引号""之间的值中不需要添加点或甚至空格。
来源:http://css-snippets.com/page/2/

2
根据这篇文章,你需要为包含div的元素指定高度,以使100%高度生效。这对你有用吗?

2
如Jamie Dixon先前所述,浮动的<div>会脱离正常流程。所有仍在正常流程中的内容将完全忽略它,并不为其留出空间。
尝试在每个<div>元素周围放置不同颜色的边框border:solid 1px orange;,以查看它们的作用。您可以从删除浮动并在div内放置一些虚拟文本开始。然后逐个样式化它们以获得所需的布局。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接