Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery css max-height not working

I've tested this code for set .css('max-height','auto') css property in JQuery but it's not working, and when I set a dimension like .css('max-height','93px') it's working fine, how can I set it as auto ?

HTML:

<div class="holder">
    <div>DIVISION</div>
    <div>DIVISION</div>
    <div>DIVISION</div>
    <div>DIVISION</div>
    <div>DIVISION</div>
    <div>DIVISION</div>
    <div>DIVISION</div>
</div>
<br />
<input type="button" onclick="test();" value="CHECK" />

CSS:

.holder{
    max-height:40px;
    background:red;
    padding:10px;
    overflow:hidden;
}
.holder div{
    background:blue;
    margin:3px 0;
}

js:

function test(){
    var hldr=$('.holder'); //get element

    alert('before set: ' + $('.holder').css('max-height')); //alert before set

    /* SET */
    hldr.css('max-height','auto');

    alert('after set: ' + $('.holder').css('max-height'));//alert after set
}

http://jsfiddle.net/silverlight/23UCV/2/

like image 793
Omid Avatar asked Mar 11 '14 22:03

Omid


People also ask

How max-height works in CSS?

The max-height CSS property sets the maximum height of an element. It prevents the used value of the height property from becoming larger than the value specified for max-height .

How do you override max-height in CSS?

maxHeight = 'none'; it will apply the none value to the max-height property of the element, overriding any other valid max-height properties in CSS selection rules that match that element.

How do I set dynamic height in jQuery?

Answer: Use the JavaScript height() method You can set the height of a <div> box dynamically using the jQuery height() method.

How does max-height work?

The max-height property defines the maximum height of an element. If the content is larger than the maximum height, it will overflow. How the container will handle the overflowing content is defined by the overflow property. If the content is smaller than the maximum height, the max-height property has no effect.


2 Answers

Changing max-height to height fixes your problem.

Demo

like image 35
Omri Aharon Avatar answered Oct 17 '22 04:10

Omri Aharon


I have also faced the same problem.

I have tried the maxHeight property. Now the problem has been resolved

$('#exampleId').css({"maxHeight":"100px"});
like image 63
Ramkee Avatar answered Oct 17 '22 05:10

Ramkee