简体   繁体   English

如何在其父母之外翻译不同大小的子元素,这些元素也具有不同的大小并将它们对齐在父母的顶部

[英]How to translate children elements of different size outside of their parents that have also different size and align them at the top of their parents

At the beginning I need the children be centered in the middle of their parents and then move to the top of the parents.一开始我需要孩子们以父母为中心,然后移动到父母的顶部。

在此处输入图像描述

@TemaniAfif has asnwered the question in a comment but I just put it into an answer in case of future use to someone. @TemaniAfif 在评论中回答了这个问题,但我只是将其放入答案中,以防将来有人使用。

The children are made to have position absolute and are placed with position bottom 100%, this enables them to go outside their parent and they will sit on top.孩子们被制作成绝对的 position 底部 100%,这使他们能够 go 在他们的父母之外,他们将坐在顶部。

Note it is important to tell the system what the position is related to.请注意,告诉系统 position 与什么相关是很重要的。 The parent needs to be given a position (we assume relative is what is required) otherwise the system will look back up the tree to find an element that has its position set.需要给父级一个 position (我们假设相对是必需的),否则系统将向后查找树以查找具有其 position 集的元素。 If nothing is set the absolute positioning will be relative to the body, and that could mean the children just disappear (off the top).如果未设置任何内容,则绝对定位将相对于身体,这可能意味着孩子会消失(离开顶部)。

在此处输入图像描述

 .container { margin-top: 35vmin; width: 100vw; height: 100vh; }.parent { position: relative; display: inline-block; margin: 5vmin; }.parent:nth-child(1) { width: 20vmin; height: 20vmin; background-color: pink; }.parent:nth-child(2) { width: 40vmin; height: 60vmin; background-color: green; }.parent:nth-child(3) { width: 30vmin; height: 20vmin; background-color: blue; }.child { position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%); }.parent:nth-child(1).child { width: 50%; height: 50%; background-color: magenta; }.parent:nth-child(2).child { width: 40%; height: 60%; background-color: lime; }.parent:nth-child(3).child { width: 50%; height: 20%; background-color: cyan; }
 <div class="container"> <div class="parent"> <div class="child"></div> </div> <div class="parent"> <div class="child"></div> </div> <div class="parent"> <div class="child"></div> </div> </div>

UPDATE: the above snippet does not show how to place the children within their parents initially.更新:上面的代码片段最初没有显示如何将孩子放在他们的父母中。 The snippet below does this and allows for a transition between the two states (should this be required).下面的代码片段做到了这一点,并允许在两种状态之间进行转换(如果需要的话)。 In this case we aren't using the bottom positioning but top plus translation as that is smoothly animatable.在这种情况下,我们没有使用底部定位,而是使用顶部加上平移,因为它是平滑动画的。

To center an element, position its top and left at 50%, that is 50% of the parent's height and width, then translate it -50% in the X and Y directions (that is 50% of its height/width).要将元素居中,position 其顶部和左侧为 50%,即父级高度和宽度的 50%,然后在 X 和 Y 方向上平移 -50%(即其高度/宽度的 50%)。 To move it to sit on top, put top 0 and translate it in the Y direction by its whole height.要将其移动到顶部,请将顶部 0 并在 Y 方向上平移其整个高度。

 .container { margin-top: 20vmin; width: 100vw; height: 100vh; }.parent { position: relative; display: inline-block; margin: 5vmin; }.parent:nth-child(1) { width: 20vmin; height: 20vmin; background-color: pink; }.parent:nth-child(2) { width: 40vmin; height: 60vmin; background-color: green; }.parent:nth-child(3) { width: 30vmin; height: 20vmin; background-color: blue; }.child { position: absolute; left: 50%; top: 50%; transform: translateX(-50%) translateY(-50%); transition: all 1s; }.parent:hover.child { top: 0; transform: translateX(-50%) translateY(-100%); }.parent:nth-child(1).child { width: 50%; height: 50%; background-color: magenta; }.parent:nth-child(2).child { width: 40%; height: 60%; background-color: lime; }.parent:nth-child(3).child { width: 50%; height: 20%; background-color: cyan; }
 <h3>HOVER OVER A RECTANGLE TO SEE THE CHILD MOVE</h3> <div class="container"> <div class="parent"> <div class="child"></div> </div> <div class="parent"> <div class="child"></div> </div> <div class="parent"> <div class="child"></div> </div> </div>

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

相关问题 如何在Android的ExpandableList中将不同的孩子添加到不同的父母 - How to add different children to different parents in ExpandableList in android 在父级边界之外转换节点会将最小大小更改为当前大小 - Translating a node outside of parents bounds changes minimum size to current size 如何使用Xpath(或使用替代方法)从不同的父母那里获得不同的节点孩子? - How to get different node children from different parents using Xpath (or what to use instead)? 如何在Expandable ListView父对象中设置不同的图像? - How to set different images in Expandable ListView parents? 具有不同父级的对象之间的JavaFX冲突检测 - JavaFX Collision detection between Objects that have different parents 一个孩子,一个TreeViewer中的不同父母 - Same child, different parents in a TreeViewer 如何在Android的ExpandableListView中为不同的父列表添加不同的子列表? - How to add different child lists for different parents lists in ExpandableListView for Android? 如何解析具有相同名称但父母不同的两个节点? - How do I resolve two nodes which have the same name but under different parents? 如何从2个不同的父级创建Maven模块? - How can I create maven module from 2 different parents? 如何为每个 class 设置不同大小的通用 - How to have different size of generic for each class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM