简体   繁体   English

让孩子遵守父母的宽度

[英]Make children abide by parents width

I have a div nested in two div's . 我有一个div嵌套在两个div's The outermost and innermost div has a set width. 最外面和最里面的div具有设定的宽度。 I want to make the outermost div's width to 0, so that all its children will be 0 too, or at least not visible. 我想将最外面的div's width为0,这样它的所有子元素也将为0,或者至少不可见。
But when I make the outermost div's width to 0, the innermost div is visible. 但是,当我将最外面的div's width为0时,最里面的div是可见的。

When setting the outermost div's width to 0, how can I make the children abide by its' parent rule? 将最外面的div's width设置为0时,如何使子代遵守其父代规则?

JSFiddle JSFiddle

 var outer = document.getElementById('outer'), small = document.getElementById('small'), large = document.getElementById('large'); small.addEventListener('click', function() { outer.style.width = 0; }); large.addEventListener('click', function() { outer.style.width = '300px'; }); 
 #outer { width: 300px; background-color: orange; } #content { background-image: url("http://i.imgur.com/nri7bYd.jpg"); height: 200px; width: 200px; } #content2 { background-color: red; width: 100px; } 
 <div id="outer"> <div id="inner"> <div id="content">This is some content</div> <div id="content2">This is another content</div> </div> </div> <button id="small">width = 0</button> <button id="large">width = 300px</button> 

You need to specify the child div with 100% width. 您需要指定宽度为100%的子div。 You are giving it a specific width (of 200px). 您为其指定了特定宽度(200像素)。 By setting the child to 100% you will effectively say take up as much as my parent has. 通过将孩子设置为100%,您将说出与我父母一样多的孩子。

#content {
    background-image: url("http://i.imgur.com/nri7bYd.jpg");
    height: 200px;
    width:100%;
}

https://jsfiddle.net/9vk44jq9/ https://jsfiddle.net/9vk44jq9/

You can set the CSS overflow property to hidden , which clips everything outside the container. 您可以将CSS overflow属性设置为hidden ,这会将所有内容裁剪到容器外部。

This sort of problem has a lot of subtle variations, so different solutions are called for sometimes. 这种问题有很多细微的变化,因此有时需要不同的解决方案。

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

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