简体   繁体   English

儿童在改变时没有正确调整大小

[英]Child div not resizing properly on change

i am using three div tags like this 我正在使用这样的三个div标签

<div id="main">
    <div id="content">
        <div id='sub'>
            Child div
        </div>
    </div>
</div>

css CSS

#main
{
    width:auto;
    height:auto;
    margin-left:-100px;
}
#content{
    width:auto;
    height:auto;
    border:5px solid blue;
}
#sub
{
 width:100%;
 height:100%;
border:1px solid red;
}

so i kept everything auto. 所以我保持一切自动。 But even when i resize or change the margin of main parent the last child not adapting to it. 但即使我调整大小或改变主要父母的边缘,最后一个孩子也不适应它。

I am not sure what may be the problem. 我不确定可能是什么问题。 But when i set width and height as 100% in resize or margin change event it seems adapting. 但是当我在调整大小或边距更改事件时将宽度和高度设置为100%时,它似乎正在适应。

Can anyone help me out in this? 任何人都可以帮助我吗? And also is there anyway to detect the parent div attribute changes in some event?? 而且还有在某些事件中检测父div属性的变化?

JSFIDDLE 的jsfiddle

Try This 试试这个

 $(document).ready(function() { $('#sub').css({ width: $('#content').width(), height: $('#content').height() }); $('button').click(function() { $('#main').css('margin-left', '0px'); $('#sub').css({ width: $('#content').width(), height: $('#content').height() }); }); }); 
 #main { width: auto; height: auto; margin-left: -100px; } #content { width: auto; height: auto; border: 5px solid blue; } #sub { width: 100%; height: 100%; border: 1px solid red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <div id="main"> <div id="content"> <div id='sub'> Child div </div> </div> </div> <button>margin0</button> 

Working Demo 工作演示

You are setting the width from JS. 你是从JS设置宽度。 Try this. 试试这个。

<div id="main">
    <div id="content">
        <div id='sub'>
            Child div
        </div>
    </div>
</div>
<button>margin0</button>

#main
{
    margin-left:-100px;
}
#content{
    border:5px solid blue;
}
#sub{
    display: block;
    border:1px solid red;
}

$(document).ready(function(){
    $('#sub').css('height',$('#content').height());


$('button').click(function(){
    $('#main').css('margin-left','0px');
});

});



JSFiddle 的jsfiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM