Equal height blocks in rows (Unify the height of blocks in rows)

Source: Internet
Author: User

Address: http://css-tricks.com/equal-height-blocks-in-rows/

 

Using jquery, there are many ways to adjust multiple columns to the same height (for example, Rob Glazebrook's method)
). The main idea is to measure the height of each column and then specify the maximum height value for each column.

However, if a block contains multiple rows and you only want to unify the height of each block in each row, what should you do? This problem may occur when a line contains thumbnails of different sizes or text blocks of different sizes.

 

Stephen Akins proposed a clever solution
. The main idea remains unchanged. We need to measure the height of each Div and then specify the maximum height value to each Div. But before that, you need to first view the top position of each Div to know which Div belongs to the same row and then the same height value for this row.

This is the entire process. I (author) will copy Stephen's originalCodeWe made some minor optimizations to make it more efficient.

 

VaR currenttallest = 0, <br/> currentrowstart = 0, <br/> rowdivs = new array (), <br/> $ El, <br/> topposition = 0; <br/> $ ('. blocks '). each (function () {<br/> $ El = $ (this); <br/> toppostion = $ el. position (). top; <br/> If (currentrowstart! = Toppostion) {<br/> // enter a new row. Set the height of all the divs in the previous row. <br/> for (currentdiv = 0; currentdiv <rowdivs. length; currentdiv ++) {<br/> rowdivs [currentdiv]. height (currenttallest); <br/>}</P> <p> // reset the parameter value for the new row <br/> rowdivs. length = 0; // empty the array <br/> currentrowstart = toppostion; <br/> currenttallest = $ el. height (); <br/> rowdivs. push ($ El); <br/>}else {<br/> // Add the next DIV in the same row to the list, and check whether it is higher than the current maximum value <br/> rowdivs. push ($ El); <br/> current Tallest = (currenttallest <$ El. Height ())? ($ El. height (): (currenttallest); <br/>}< br/> // set the uniform height for the DIV in the last row <br/> for (currentdiv = 0; currentdiv <rowdivs. length; currentdiv ++) {<br/> rowdivs [currentdiv]. height (currenttallest); <br/>}< br/> });

 

$ ('. Blocks') can be replaced with any CSS Selector Used to represent elements of the same height.

 
To process the dynamic width, You can encapsulate the above code into a function. When you adjust the window size, you can call it.

 

$ (Window). Resize (function () {<br/> equalizethosemofos (); <br/>}

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.