如何将比其父元素宽的元素居中?

发布于 2024-10-31 03:33:41 字数 101 浏览 7 评论 0原文

我目前有一个固定宽度为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

沉溺在你眼里的海 2024-11-07 03:33:41

您可以将子元素放置在 50% 处,然后使用子元素宽度一半的负边距:

.parent { position: relative; overflow: hidden; }
.child { position: absolute; left: 50%; top: 0; margin-left: -475px; }

这样您只需要知道子元素的大小。

You can place the child at 50%, then use a negative margin that is half the width of the child:

.parent { position: relative; overflow: hidden; }
.child { position: absolute; left: 50%; top: 0; margin-left: -475px; }

This way you only need to know the size of the child element.

葮薆情 2024-11-07 03:33:41

我在这里找到了一种使用动态宽度来做到这一点的方法: http://www.greywyvern.com/?post =323

I found a way to do it with dynamical widths here: http://www.greywyvern.com/?post=323

网名女生简单气质 2024-11-07 03:33:41

另一种方法是使用:

   .parent {
     position:relative;
   }
   .child {
     position:absolute;
     left:50%;
     transform:translateX(-50%);
   }

即使您不知道孩子或父母的大小,它也可以工作,
但不适用于较旧的浏览器。

Another way would be to use:

   .parent {
     position:relative;
   }
   .child {
     position:absolute;
     left:50%;
     transform:translateX(-50%);
   }

which works even if you do not know the size of child nor parent,
but will not work in older browsers.

像你 2024-11-07 03:33:41
.container {
    width:900px
}

iframe {
    margin-left:-25px;
    width:950px;
}
.container {
    width:900px
}

iframe {
    margin-left:-25px;
    width:950px;
}
还给你自由 2024-11-07 03:33:41

我假设你的外部容器宽度是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文