简体   繁体   English

子div -margin不忽略父母的填充

[英]Child div -margin is not ignoring parent's padding

I would like inner div to fill the outer div. 我希望内部div填充外部div。 I was told to use a negative padding on the child, which sounds logical but doesn't work in my case. 有人告诉我在孩子身上使用负填充,这听起来很合逻辑,但在我的情况下不起作用。 I believe this is due to the transitions that I am using on the parent and child divs. 我相信这是由于我在父级和子级div上使用的过渡。

I have the child to go from transparent to not transparent on a hover. 我让孩子在悬停时从透明变为不透明。 I have the child's background image transitioning from the parent's background image. 我让孩子的背景图像从父母的背景图像过渡。 I want to add padding to the parent so that I can customize the height of the div, however when I do, the child doesn't fill the parent. 我想向父级添加填充,以便可以自定义div的高度,但是当我这样做时,子级不会填充父级。 When I add a negative margin to the child, there is no effect. 当我给孩子增加负边距时,没有任何效果。

 .outer{ font-size: 2em; border: 2px solid #898080; margin-bottom: 40px; border-radius: 20px; text-shadow: 5px 5px 5px #000000; position: relative; display: inline-block; transition: all 1s ease-in-out; background-color: black; padding: 30px 0px 30px 0px; } /* Scale up the box */ .outer:hover { color: white; border: 2px solid #d8e6ee; transform: scale(1.1, 1.1); transition: background .5 ease; opacity: 1; } #inner{ zoom: 1.0; border-radius: 17px; text-shadow: 5px 5px 5px #000000; background-color: red; opacity: 0; -webkit-transition: opacity 1s linear; -moz-transition: opacity 1s linear; transition: opacity 1s linear; } #inner:hover{ opacity: 1; } 
 <div class="outer"> <div id="inner">Hello World</div> </div> 

Ok, so you are trying to set the dimensions of an element trough the use of padding, but you want the content of that element to ignore the padding. 好的,因此您试图通过填充来设置元素的尺寸,但是您希望该元素的内容忽略填充。 I guess, if you put it that way it becomes quite obvious that padding is not the right tool here. 我想,如果您这样说的话,很明显填充不是这里的正确工具。 All you have to do is set the dimensions of the child, and by definition the parent will follow. 您所要做的就是设置子项的尺寸,并且按照定义,父项将遵循。

In code that would mean, move the padding from the .outer to the #inner. 在这意味着将填充从.outer移到#inner的代码中。

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

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