【问题标题】:Auto Adjust Div Height自动调整 div 高度
【发布时间】:2013-11-12 10:43:39
【问题描述】:

我希望右侧的 div 与左侧的 div 的高度相匹配。右侧的 div 应始终与左侧的 div 高度相同。左侧的 div 会自动调整其高度。两个 div 应始终具有相同的高度。一张图片值一千个字,所以请看下面的图片。

这是解释我的问题的图片。

http://i44.tinypic.com/24pcpwj.jpg

JSFIDDLE

http://jsfiddle.net/zFqNH/

HTML

 <div style="width:1053px;">
 <section id="inside" style="overflow: hidden; width:310px; float:right; border-left:0;"> 
 <ul>
 <p class="title">Funny</p>

<li class="list">
    <img src="http://cdn.dashburst.com/wp-content/uploads/2013/01/Grumpy-Cat.jpg"      width="100" height="67" style="float:left;padding-right:15px;" /></a>
    <h3 style="font-size:20px;">Grumpy Cat</h3>        
</li>
 </ul>
 </section>

 <section id="inside">   
 <h2 class="subheading">Grumpy Cat<br/><p style="font-size:14px; margin-top:0px; font-weight:normal;"></p></h2><h2 style="border-bottom: 1px solid #dddddd; padding-top:10px;"></h2>                                    
 <p class="section"></p>                              

 <img src="http://cdn.dashburst.com/wp-content/uploads/2013/01/Grumpy-Cat.jpg" border="0" style="display:block; margin: 0 auto;" height="396" width="700">
 <br/>                                      
     <img src="http://cdn.dashburst.com/wp-content/uploads/2013/01/Grumpy-Cat.jpg" border="0" style="display:block; margin: 0 auto;" height="396" width="700">
 <br/>
         <img src="http://cdn.dashburst.com/wp-content/uploads/2013/01/Grumpy-Cat.jpg" border="0" style="display:block; margin: 0 auto;" height="396" width="700">
 <br/>

 </div></div></section>

CSS

  #inside { 
width:700px; 
border-right: 1px solid #dddddd; 
border-left: 1px solid #dddddd; 
min-height: 600px; 
overflow: hidden; 
padding-bottom:15px;    
  }
  .title{
width:300px;
padding-top:3px;
padding-bottom:7px;
color:#fff;
height:20px;    
margin-top:125px; 
background:#2c2c2c;         
font-size: 18px;
  }
  .section{
padding-left:30px; 
padding-bottom:15px; 
margin-bottom: 1px; 
font-size: 28px;
color: #657B83;
  }
  .subheading {
background:#fff;    
font-size:50px;         
height:100px;
position: relative; 
  }

  .bottom {
border-top: 1px solid #dddddd;    
border-right: 1px solid #dddddd;    
padding-top: 20px;
text-align: center;
padding-bottom: 10px;   
width:1053px;       
position:relative;
margin: 0 auto; 
font-size:15px;
color: #fff;    
  }
  .list{
width:300px; 
border-bottom: 1px solid #dddddd;
padding-bottom:10px; 
display: inline-block; 
margin-top:20px;
  }

【问题讨论】:

    标签: html height match


    【解决方案1】:

    在两个div下面插入javascript,用document.getElementById获取左边div的高度,然后用document.getElementById改变右边div的高度。

    <script>
      var height = document.getElementById('div1').style.height;
      document.getElementById('div2').style.height = height;
    </script>
    

    文档:
    http://www.w3schools.com/jsref/prop_style_height.asp

    【讨论】:

    • 这不太行。我试过了,它把一切都搞砸了。有没有只使用 CSS 的解决方案?
    • 仅使用 CSS 无法做到这一点,您需要使用 JavaScript 或 jQuery。您的 div id 需要彼此唯一。请查看针对此类似问题的 jQuery 答案stackoverflow.com/questions/3275850/…
    • 你能告诉我这个网站是怎么做的吗?本网站的侧边栏一直向下延伸。 theverge.com/2013/11/1/5053878/…
    • 他们使用类似jsfiddle.net/keenydev/ufpzQ/6 的方式来实现。将两个 div(左 div 和右 div)嵌套在另一个 div(主 div)中,并将左/右 div 的高度设置为 100%。在两个 div 上向左浮动并在左侧 div 上放置一个右边框
    • 非常感谢!我会接受你的回答,因为你一直在帮助我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2015-04-09
    • 2015-08-20
    • 2015-11-03
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多