CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Question need to make 3 DIVs of equal height

    I have the following HTML + CSS code:

    HTML Code:
    <html>
    <head>
    <title>Sample 3 columns</title>
    <style>
    #wrapper {
    	text-align: left;
    	margin: 0px auto;
    	padding: 0px;
    	border:0;
    	width: 100%;
    }
    
    #header {
    	margin: 0 0 15px 0;
    	background: yellow;
    }
    
    #side-a {
    	float: left;
    	width: 20%;
    }
    
    #side-b {
    	float: right;
    	width: 35%;
    }
    
    #content { 
    	float: left;
    	width: 45%
    }
    
    #footer {
    	clear: both;
    	background: #A2A2A2;
    }
    
    .Sandbox
    {
      border-left:1px solid #bbb;
      border-right:1px solid #bbb;
      border-bottom:1px solid #bbb;
      border-top:1px solid #bbb;
      margin-top:5px;
      margin-left:5px;
      margin-right:5px;
      margin-bottom:10px;
      padding-top:10px;
      padding-left:15px;
      padding-right:15px;
      padding-bottom:10px
    }
    
    </style>
    </head>
    
    <body>
    
    <div id="wrapper">
    
      <div id="header">HEADER</div>
    	
      <div id="container">
    	
        <div id="side-a">
          <div class="Sandbox">LEFT</div>
        </div>
    		
        <div id="content">
          <div class="Sandbox">
            <div class="Sandbox">CONTENT</div>
            <div class="Sandbox">MORE CONTENT</div>
          </div>			
        </div>
    		
        <div id="side-b">
          <div class="Sandbox">RIGHT</div>
        </div>
      </div>
    	
      <div id="footer">FOOTER</div>
    	
    </div>
    
    </body>
    </html>
    What I want is make the left and right boxes equal in height with the middle one. So far I could not find how. Can anybody help? Thank you.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: need to make 3 DIVs of equal height

    Are you looking for a pure CSS solution? If so, you are going to need to change your styling around a bit. I'm not understanding the whole Sandbox class. I don't know why you wouldn't just change the CSS of the parent container.

    Either way, the simplest solution is to use JavaScript to check for the greatest height property, and then match it.

    Code:
    <script type="text/javascript">
    function matchDivHeight(theArray) {
      var tallest = 0, height;
      for (i = 0; i < 2; i++) {
        for (ii = 0; ii < theArray.length; ii++) {
          if (i == 0) {
            height = document.getElementById(theArray[ii]).offsetHeight;
            if (height > tallest) {tallest = height;}
          }
          else {document.getElementById(theArray[ii]).style.height = tallest + 'px';}
        }
      }
    }
    
    // pass an array of div ids as the parameter
    var elements = ['side-a', 'content', 'side-b'];
    window.onload = function() {
      matchDivHeight(elements);
    }
    </script>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: need to make 3 DIVs of equal height

    Well, I'm learning (remember HTML and CSS are not my primary expertise ). So feel free to suggest any solution. A pure CSS solution would be nice.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Dec 2003
    Location
    Northern Ireland
    Posts
    1,362

    Re: need to make 3 DIVs of equal height

    I believe the height property works in most browsers if the div is positioned absolute.

    Code:
    #side-b {
    	position: absolute;
    	float: right;
    	width: 35%;
    }
    will put the right hand div at the top of the page.
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook


    0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010

  5. #5
    Join Date
    May 2002
    Posts
    10,943

    Re: need to make 3 DIVs of equal height

    The height property applies no matter why positioning you give the DOM element. Positioning something absolutely means that it will not conform to any placed objects, but however you position it with coordinates. We don't want this here.

    Ed Eliot covers this topic well in his blog. However, you have a slightly different layout than most. I think that JavaScript is going to be your only escape. Still, take a look at Ed's implementations.

    Here is a simple implementation using JavaScript. Also, I changed the Sandbox style for you.

    Code:
    <html>
      <head>
        <title>Sample 3 columns</title>
        <style>
          #wrapper {
            text-align: left;
            margin: 0px auto;
            padding: 0px;
            border: 0px;
            width: 100%;
          }
    
          #header {
            margin: 0px 0px 15px 0px;
            background: yellow;
          }
    
          #side-a {
            float: left;
            width: 20%;
          }
    
          #side-b {
            float: right;
            width: 35%;
          }
    
          #content { 
            float: left;
            width: 45%
          }
    
          #footer {
            clear: both;
            background: #a2a2a2;
          }
    
          .Sandbox {
            border: 1px solid #bbbbbb;
            margin: 5px 5px 10px 5px;
            padding: 10px 15px 10px 15px;
          }
        </style>
        <script type="text/javascript">
          function matchDivHeight(theArray) {
            var tallest = 0, height;
            for (i = 0; i < 2; i++) {
              for (ii = 0; ii < theArray.length; ii++) {
                if (i == 0) {
                  height = document.getElementById(theArray[ii]).offsetHeight;
                  if (height > tallest) {tallest = height;}
                }
                else {document.getElementById(theArray[ii]).style.height = tallest + 'px';}
              }
            }
          }
    
          // pass an array of div ids as the parameter
          var elements = ['column1','column2','column3'];
          window.onload = function() {
            matchDivHeight(elements);
          }
        </script>
      </head>
    <body>
    
    <div id="wrapper">
      <div id="header">HEADER</div>
    
      <div id="container">
        <div id="side-a">
          <div class="Sandbox" id="column1">LEFT</div>
        </div>
        <div id="content">
          <div class="Sandbox" id="column2">
            <div class="Sandbox">CONTENT</div>
            <div class="Sandbox">MORE CONTENT</div>
          </div>
        </div>
        <div id="side-b">
          <div class="Sandbox" id="column3">RIGHT</div>
        </div>
      </div>
      
      <div id="footer">FOOTER</div>
    </div>
    
    </body>
    </html>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  6. #6
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: need to make 3 DIVs of equal height

    Thanks for the information. I found that blog post myself, and is quite useful, though in my case the div blocks are not that easy to arrange like that. I guess in the end I'll leave them unequal, since my layout is getting more and more complicated and even unequal they are looking good. Anyway, that's for the help. I'll keep in mind the javascript trick for other cases.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured