简体   繁体   English

使绝对父 Div 宽度等于总绝对子 Div 的宽度

[英]Make an absolute parent Div Width equal to the total absolute child Div's Width

What I want to do is not fit the childs inside the parent, not want the children to be responsive I just want the parent fit the overall fixed width of his childs to then center it properly我想要做的不是让孩子适应父母内部,不希望孩子做出反应我只希望父母适合孩子的整体固定width ,然后正确居中

What I'm trying to do is:我想做的是:

A widget that can be centered regardless of the contents inside, so if there are 5 childs appears centered, if there's 10 stills centered一个无论里面的内容如何都可以居中的小部件,所以如果有 5 个孩子居中,如果有 10 个静止图像居中

I'm trying to make a "responsive" div with a dynamically created div childs, with those being absolute because they need to overlap themselves a bit我正在尝试使用动态创建的div子项制作一个“响应式” div ,这些子项是absolute的,因为它们需要自己重叠一点

The thing is, the parent doesn't seem to detect their childs at all when putting them on absolute问题是,父母在将孩子置于absolute状态时似乎根本没有发现他们

I know this is somewhat possible because when using overflow the parent knows somehow the overall width我知道这在某种程度上是可能的,因为当使用overflow时,父级以某种方式知道整体width

Is there any way to make the outer parent div 's width equal to the total's width of their childs?有什么方法可以使外部父divwidth等于其孩子的总width

Have been searching all damn morning reviewing lots of questions and none seemed to answer that, I'm aware that most surely this is not the better approach for this and will be kindly grateful knowing other methods整个早上都在搜索,审查了很多问题,但似乎没有人回答,我知道这肯定不是更好的方法,如果知道其他方法,我将不胜感激

What I've GOT:我有什么:

 .grandparent{ position: absolute; width: 100%; height: 50%; bottom: 0; border: solid; }.parent { position: absolute; width: 90%; height: 85%; top: 50%; left: 50%; transform: translate(-50%, -50%); border: red solid; }.child { position: absolute; width: 100px; height: 150px; border: blue solid; }
 <div class="grandparent"> <div class="parent"> <div class="child" style="transform: translate(0em, 16px) rotateZ(-20deg) rotateY(0deg);"></div> <div class="child" style="transform: translate(3.6em, 21px) rotateZ(-10deg) rotateY(0deg);"></div> <div class="child" style="transform: translate(7.2em, 25px) rotateZ(0deg) rotateY(0deg);"></div> </div> </div>

I want to put the Parent (red square) Centered Horizontally , no matter whats inside because childs count may vary我想把父母(红色方块)水平居中,不管里面是什么,因为孩子的数量可能会有所不同

I have finally found the solution without removing the absolute positioning from my child elements !我终于找到了解决方案,而无需从我的子元素中删除absolute定位!

Used overflow to then be able to get the total width of the children with scrollWidth使用overflow然后能够获得具有scrollWidth的孩子的总宽度

var A = document.getElementById('parent').scrollWidth

And knowing the true width of the parent's div I can then center it properly without further problems并且知道parent's div的真实宽度,然后我可以正确地将其居中而不会出现进一步的问题

$('#parent').width(A); or document.getElementById('parent').style.width = A + "px";document.getElementById('parent').style.width = A + "px";

Workin' Snippet工作片段

 var parent = document.getElementById('parent'); parent.style.width = parent.scrollWidth + "px"; var parent2 = document.getElementById('parent2'); parent2.style.width = parent2.scrollWidth + "px";
 .body { height: 500px;}.grandparent{ position: absolute; width: 100%; height: 200px; top: 0; border: solid; }.parent { position: absolute; height: 100%; overflow: visible; top: 50%; left: 50%; transform: translate(-50%, -50%); border: red solid; }.child { position: absolute; width: 100px; height: 150px; border: blue solid; }.grandparent2{ position: absolute; width: 100%; height: 200px; top: 200px; border: solid; }.parent2 { position: absolute; height: 200px; overflow: visible; top: 50%; left: 50%; transform: translate(-50%, -50%); border: red solid; }.h41, .h42 { position: absolute; left:20px; }.h41 { top: 20px; }.h42 { top: 220px;}
 <h4 class="h41">Centered with 5 Childs</h4> <div class="grandparent"> <div id="parent" class="parent"> <div class="child" style="transform: translate(0em, 16px) rotateZ(-20deg) rotateY(0deg);"></div> <div class="child" style="transform: translate(3.6em, 21px) rotateZ(-10deg) rotateY(0deg);"></div> <div class="child" style="transform: translate(7.2em, 25px) rotateZ(0deg) rotateY(0deg);"></div> <div class="child" style="transform: translate(11em, 21px) rotateZ(10deg) rotateY(0deg);"></div> <div class="child" style="transform: translate(15.2em, 25px) rotateZ(20deg) rotateY(0deg);"></div> </div> </div> <h4 class="h42">Centered with 2 Childs</h4> <div class="grandparent2"> <div id="parent2" class="parent2"> <div class="child" style="transform: translate(0em, 16px) rotateZ(-10deg) rotateY(0deg);"></div> <div class="child" style="transform: translate(5em, 21px) rotateZ(10deg) rotateY(0deg);"></div> </div> </div>

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

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