首页 > 解决方案 > set margin around child div when parent's width is set using javascript

问题描述

The site I am working on: https://tricycle-203819.appspot.com/ I would like to replicate the effect on this site where there's multiple sections, and the child divs have margins around the sections: https://www.newyorker.com/

On my site, the div container-spice's width and margin to the left and right is set using javascript on load, and on window size. I would like the children of left-container, which is a child of container-spice, to have a margin on the right that is of some constant pixel.

Changing the padding-right and margin-right property of left-container do not achieve this effect. I would like to avoid changing the properties of the children of left-container if possible, since they will be reused elsewhere.

标签: javascripthtmlcsstwitter-bootstrapbootstrap-4

解决方案


margin-rightonleft-container 不起作用的原因是因为它right-container不尊重边距,因为它的位置是固定的。但是,您可以添加一个padding-righttoleft-container以实现在两个容器之间创建间隙的所需效果。


推荐阅读