简体   繁体   English

在容器中放置子元素

[英]Position child elements in Container

I am trying this: there is a picture element Which is wrapped inside a container div 我正在尝试:有一个图片元素包装在容器div中

<div id="container">
    <div id="a">
        <img src="b.jpg" alt="b" />
    </div>
</div>

Now I want to place the child elements such a way that the top and left of the picture is always 25% of the height & width of the container div. 现在,我要放置子元素,使得图片的顶部和左侧始终是容器div的高度和宽度的25%。 How can I achieve this? 我该如何实现?

If that is the only requirement, you can just do it by adding a padding on the inner div. 如果这是唯一要求,则只需在内部div上添加填充即可。

According to W3C, it those percentages should refer to the dimensions of the outer div. 根据W3C,这些百分比应参考外部div的尺寸。

Like margin properties, percentage values for padding properties refer to the width of the generated box's containing block 与边距属性一样,填充属性的百分比值是指生成的框的包含块的宽度

http://www.w3.org/TR/CSS2/box.html http://www.w3.org/TR/CSS2/box.html

For the height, it will only work if your inner div is the only element inside your outer div (because the inner div will determine the height of the other). 对于高度,它仅在内部div是外部div内的唯一元素时才有效(因为内部div将确定另一个div的高度)。 So for you example, it'll work. 因此,以您为例,它将起作用。

Try this: 尝试这个:

#container {position: relative;}
#container #a {position: absolute; left: 25%; top: 25%;}

Here's a working fiddle: jsFiddle 这是一个有效的小提琴: jsFiddle

Try: 尝试:

#container { position:relative; }
#container img { position:absolute; top:25%; left:25%; }

Live demo: jsFiddle 现场演示: jsFiddle

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

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