Two solutions for two div height consistency (css+js)

Source: Internet
Author: User

Recently modified some of the previously written code, found a point problem.

For example, 2 div needs to maintain a consistent height dynamically.

The CSS approach is: 5 years of careful development of the UI front-end framework!

. Code
  1. <div style="Overflow:hidden">
  2. <div id="left" style="margin-bottom:-10000px; padding-bottom:10000px; " ></div>
  3. <div id="right" style="margin-bottom:-10000px; padding-bottom:10000px; " ></div>
  4. </div>

If the actual height is not greater than 10000px, then this is no problem, if one side of the actual height is greater than 10000px, then the height of the other side can only be defined as 10000px, not in line with the actual use of the demand.

After testing, JS can solve this problem.

The code looks like this: 5 years of well-developed UI front-end framework!

. Code
  1. var height = document.getelementbyid (" left "). Offsetheight > document.getelementbyid (" "Right" ). offsetheight ?   
  2.     document.getelementbyid (" left "). Offsetheight : document.getelementbyid (" right "). offsetheight;  
  3. document.getElementById ("left"). Style.height = document.getElementById ("right"). Style.height = Height + "px";

In addition, in practice, there may be problems with the above values being more or less due to the existence of the padding attribute in the left Div or right div. For example, when the final assignment found that left is higher than right 10px, this situation only manually in the code to reduce 10来 resolved. It's also a less than perfect place.

Two solutions for two div height consistency (css+js)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.