JSFiddle - React, Tailwind, and code Playground
HTML
<div class='autoHeightWidth'>
<input type='button' id='autoCheck' value='Auto?'/>
<div class='fixedHeightAutoWidth'>
<input type='button' id='fixedCheck' value='Auto?'/>
<div class='autoHeightFixedWidth'>
<input type='button' id='fixedCheck' value='Auto?'/>
<div class='fixedHeightWidth'>
<input type='button' id='fixedCheck' value='Auto?'/>
</div>
</div>
</div>
</div>
CSS
.autoHeightWidth{
width: auto;
height: auto;
border: 1px solid black;
}
.fixedHeightAutoWidth{
width: auto;
margin: 50px;
height: 400px;
border: 1px solid black;
}
.autoHeightFixedWidth{
width: 200px;
height: auto;
margin: 25px auto;
border: 1px solid black;
}
.fixedHeightWidth{
height: 150px;
width: 150px;
margin: 20px;
border: 1px solid black;
}
JavaScript
$.fn.isAuto = function(dimension){
if (dimension == 'width'){
var originalWidth = this.innerWidth();
var marginLeft = parseInt(this.css('margin-left'));
var testMarginWidth = marginLeft+50;
this.css('margin-left', testMarginWidth);
var newWidth = this.innerWidth();
this.css('margin-left', marginLeft);
if(newWidth<originalWidth){
return true;
}
else{
return false;
}
}
else if(dimension == 'height'){
var originalHeight = this.height();
this.append('<div id="test"></div>');
var testHeight = originalHeight+500;
$('#test').css({height: testHeight});
var newHeight = this.height();
$('#test').remove();
if(newHeight>originalHeight){
return true;
}
else{
return false;
}
}
};
$('input').click(function(){
if($(this).parent().isAuto('height')){
$(this).parent().append('This div height is auto<br>');
}
else{
$(this).parent().append('This div height is fixed<br>');
}
if($(this).parent().isAuto('width')){
$(this).parent().append('This div width is auto');
}
else{
$(this).parent().append('This div width is fixed');
}
});