In this topic, we’re going to show you how you can set all elements with a particular class to the highest element height. To do this, we need to loop through all elements with a particular class, get the highest value, then match all other elements to the same height. We can do this using jQuery as follows:
var maxHeight = 0;
$(".some-element").each(function(){
if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
});
$(".some-element").height(maxHeight);
Awesome, we now have set all elements to the same height. We can take this one step further as elements can change in max height when you are shrinking the browser width for example. What we can do here is use the resize function to update the values.
To make things easier, we can create a function, then call the function both on page load and within the resize function as follows:
var maxHeight = 0;
function matchMaxHeight() {
$(".some-element").each(function(){
if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
});
$(".some-element").height(maxHeight);
}
matchMaxHeight();
$(window).resize(function() {
matchMaxHeight();
});
Popular Posts
- Show / Hide div based on dropdown selected using jQuery
- Autosuggestion Select Box using HTML5 Datalist, PHP and MySQL with Example
- Infinite Scrolling on PHP website using jQuery and Ajax with example
- How to Convert MySQL Data to JSON using PHP
- Custom Authentication Login And Registration Using Laravel 8
- Image Lazy loading Using Slick Slider
- Slick Slider Basic With Example
- Google Login or Sign In with Angular Application
- How to change date format in PHP?
- php in_array check for multiple values
- Adaptive Height In Slick Slider
- Slick Slider Center Mode With Example
- How to Scroll to an Element with Vue 3 and ?
- JavaScript Multiple File Upload Progress Bar Using Ajax With PHP
- Slick Slider Multiple Items With Example
Total Views: 105