最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set a 'div' height same with his neighbor? - Stack Overflow

programmeradmin3浏览0评论

I am using bootstrap as my CSS framework. Now I have some dynamic content in the left side div, and some static content in the right div. I want to the height of right side div auto change base on the height of left side div. Is that possible?

<div class="container-fluid">
  <div class="row-fluid">
    <div class="span5 offset1">
    <div class="well">
      Dynamic Content
    </div>
  </div>
  <div class="span5">
    <div class="well" style="height: 330px; overflow-x: hidden; overflow-y: scroll;">
      Static Content
    </div>
  </div>
</div>

I am using bootstrap as my CSS framework. Now I have some dynamic content in the left side div, and some static content in the right div. I want to the height of right side div auto change base on the height of left side div. Is that possible?

<div class="container-fluid">
  <div class="row-fluid">
    <div class="span5 offset1">
    <div class="well">
      Dynamic Content
    </div>
  </div>
  <div class="span5">
    <div class="well" style="height: 330px; overflow-x: hidden; overflow-y: scroll;">
      Static Content
    </div>
  </div>
</div>

Share Improve this question edited Oct 1, 2020 at 14:06 TylerH 21.1k77 gold badges79 silver badges112 bronze badges asked Sep 22, 2013 at 5:17 ChrisChris 6,61110 gold badges46 silver badges62 bronze badges 5
  • Yes. Could you post the code you have? (or better, make a jsFiddle) – chopper Commented Sep 22, 2013 at 5:20
  • it could be done with jquery... – Bhojendra Rauniyar Commented Sep 22, 2013 at 5:22
  • what if the static content is bigger then the dynamic? you want it to scroll? or the dynamic height should grow? – avrahamcool Commented Sep 22, 2013 at 6:20
  • @avrahamcool if the static content is bigger than the dynamic he can just set the min-height of the dynamic equal to the height of the static. Problem solved :) – samrap Commented Sep 22, 2013 at 6:44
  • I asked what behavior he wants.. – avrahamcool Commented Sep 22, 2013 at 6:53
Add a comment  | 

4 Answers 4

Reset to default 8

You could do this

<div class="wrapper">
    <div class="dynamic">
        Dynamic Line <br />
        Dynamic Line <br />      
        Dynamic Line <br />
        Dynamic Line <br />      
        Dynamic Line <br />
        Dynamic Line <br />      
        Dynamic Line <br />
        Dynamic Line <br />      
    </div>
    <div class="static">
        Static<br />
        Static<br />        
    </div>
</div>

CSS

.wrapper {
    position: relative;
    width: 400px;
    overflow: auto;
}

.dynamic {
    position: relative;
    background-color: yellow;
    width: 200px;
}

.static {
    position: absolute;    
    background-color: orange;
    width: 200px;
    float: left;
    left: 200px;
    top: 0px;
    bottom: 0px;
    right: 0px;
}

I think jQuery is the best solution to achieve this. Check out my fiddle:

http://jsfiddle.net/PHjtJ/

$(document).ready(function() {

    var dynamic = $('.dynamic');
    var static = $('.static');

    static.height(dynamic.height());

});

the solution depends on the behavior you want in the case where the static column is larger than the dynamic one. [I actually think you want the second scenario.]

Notice: both case are pure CSS, and without specifying any height whatsoever. also, not specifying a margin of any sort, so you can change the width of the column at your will, without the need to calculate the margin/position again and again..

  1. if you want the dynamic column to enlarge, and to be as the static height: so you actually want the columns to always be a the same height. and that can be easily achieved with CSS table layout styling. in that case: See this Fiddle (the script is only for adding dynamic content, this is a pure CSS SOLUTION)

  2. if you want the dynamic column to stretch only to its content height, and the static one to have the same height + a scroller. in that case: see this Fiddle (again: the script is only for adding dynamic content, this is a pure CSS SOLUTION)

in both cases, the static column grows if the dynamic one become longer.

Use Javascript/jQuery to find which box has the biggest height. Then set the same height to all the divs.

<script type="text/javascript">
    jQuery(document).ready(function($) {
        var description = jQuery("div.description");
        var big =0;
        description.each(function(index, el) {
            if(jQuery(el).height() > big)
                big =jQuery(el).height(); //find the largest height
        });

        description.each(function(index, el) {
            jQuery(el).css("min-height", big+"px"); //assign largest height to all the divs
        });
    });
</script>
发布评论

评论列表(0)

  1. 暂无评论