如何将比其父元素宽的元素居中?
我目前有一个固定宽度为 900px 的 div。
我想添加一个固定宽度为 950px 的子 iframe,并且我希望它与中心完美对齐。
那怎么办呢?
谢谢。
I currently have a div with a fixed width of 900px.
I'd like to add a child iframe with a fixed width of 950px, and I'd like it to be perfectly aligned to the center.
How can that be done?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以将子元素放置在 50% 处,然后使用子元素宽度一半的负边距:
这样您只需要知道子元素的大小。
You can place the child at 50%, then use a negative margin that is half the width of the child:
This way you only need to know the size of the child element.
我在这里找到了一种使用动态宽度来做到这一点的方法: http://www.greywyvern.com/?post =323
I found a way to do it with dynamical widths here: http://www.greywyvern.com/?post=323
另一种方法是使用:
即使您不知道孩子或父母的大小,它也可以工作,
但不适用于较旧的浏览器。
Another way would be to use:
which works even if you do not know the size of child nor parent,
but will not work in older browsers.
我假设你的外部容器宽度是 950px,子 iframe 宽度是 900px 那么你可以使用 margin:0px auto;为你的CSS子容器
I assume your outer container width is 950px and child iframe width is 900px then you can use margin:0px auto; for your child container in css