div+css的一个问题:怎么让DIV自动适应高度?

时间:2021-08-28 06:00:16
我想实现的效果是:内容多少和高度不确定,当内容很多时,所有DIV高度随之变高,还想让left和rightDIV的高度跟main的高度一致!

我现在写的代码如下,左右两个DIV不去自动适应高度。(找了很多资料,都不成功)

<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css">
#main{
width:500px;
height:500px;
background-color:#006633;
margin:0 auto;
}
#left{
width:10px;
background-color:#003399;
float:left;
}
.clear{ clear:both;}
#middle{
width:480px;
background-color:#CC6600;
float:left;
}
#right{
width:10px;
background-color:#0066CC;
float:left;
}
</style>
</head>

<body>
<div id="main">
<div id="left"></div>

<div id="middle">
  <p>ddd</p>
  <p>d</p>
  <p>&nbsp;</p>
  <p>d</p>
  <p>d</p>
  <p>sdfsfdadsfa</p>
  <p>sdafdfsa</p>
  <p>sdfa</p>
  <p>sdf</p>
  <p>dsfa</p>
  <p>dsfa</p>
  <p>dfsa</p>
  <p>&nbsp;</p>
</div>

<div id="right"></div>

</div>
</body>
</html>

6 个解决方案

#1


主要是在火狐浏览器里用。

#2



<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css">
#main {
width:500px;
background-color:#006633;
margin:0 auto; overflow:hidden;
}
#left {
width:10px;
background-color:#003399;
float:left; padding-bottom:32767px; margin-bottom:-32727px; 
}
.clear {
clear:both;
}
#middle {
width:480px;
background-color:#CC6600;
float:left; padding-bottom:32767px; margin-bottom:-32727px;
}
#right {
width:10px;
background-color:#0066CC;
float:left; padding-bottom:32767px; margin-bottom:-32727px;
}
</style>
</head>
<body>
<div id="main">
  <div id="left">1 </div>
  <div id="middle">
    <p>ddd </p>
    <p>d </p>
    <p>&nbsp; </p>
    <p>d </p>
    <p>d </p>
    <p>sdfsfdadsfa </p>
    <p>sdafdfsa </p>
    <p>sdfa </p>
    <p>sdf </p>
    <p>dsfa </p>
    <p>dsfa </p>
    <p>dfsa </p>
    <p>&nbsp; </p>
    <p style="height:300px">fdsafdsafads</p>
  </div>
  <div id="right"> 3</div>
</div>
</body>
</html>

#3


#main { position:relative; height:auto !important; height:100%; min-height:100%;}

#4


使用table布局...最简单,并不是所有的布局都要使用div+css的

#5


UP

#6


如果你不介意使用一小段javascript的话..
首先<body onload="test()">
然后 
<script type="text/javascript">   
  function test()   
  {   
    document.all.right.style.height=document.all.main.offsetHeight+"px";  
    document.all.left.style.height=document.all.main.offsetHeight+"px";  
  }   
  </script>

#1


主要是在火狐浏览器里用。

#2



<!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=gb2312" />
<title>无标题文档</title>
<style type="text/css">
#main {
width:500px;
background-color:#006633;
margin:0 auto; overflow:hidden;
}
#left {
width:10px;
background-color:#003399;
float:left; padding-bottom:32767px; margin-bottom:-32727px; 
}
.clear {
clear:both;
}
#middle {
width:480px;
background-color:#CC6600;
float:left; padding-bottom:32767px; margin-bottom:-32727px;
}
#right {
width:10px;
background-color:#0066CC;
float:left; padding-bottom:32767px; margin-bottom:-32727px;
}
</style>
</head>
<body>
<div id="main">
  <div id="left">1 </div>
  <div id="middle">
    <p>ddd </p>
    <p>d </p>
    <p>&nbsp; </p>
    <p>d </p>
    <p>d </p>
    <p>sdfsfdadsfa </p>
    <p>sdafdfsa </p>
    <p>sdfa </p>
    <p>sdf </p>
    <p>dsfa </p>
    <p>dsfa </p>
    <p>dfsa </p>
    <p>&nbsp; </p>
    <p style="height:300px">fdsafdsafads</p>
  </div>
  <div id="right"> 3</div>
</div>
</body>
</html>

#3


#main { position:relative; height:auto !important; height:100%; min-height:100%;}

#4


使用table布局...最简单,并不是所有的布局都要使用div+css的

#5


UP

#6


如果你不介意使用一小段javascript的话..
首先<body onload="test()">
然后 
<script type="text/javascript">   
  function test()   
  {   
    document.all.right.style.height=document.all.main.offsetHeight+"px";  
    document.all.left.style.height=document.all.main.offsetHeight+"px";  
  }   
  </script>