gpt4 book ai didi

css - 如何垂直对齐 <iframe>?

转载 作者:太空宇宙 更新时间:2023-11-04 15:11:14 25 4
gpt4 key购买 nike

我想要做的就是获取标签并将其与页面垂直对齐。我不知道是否必须将标签放在另一个标签中才能工作。任何帮助将不胜感激。提前致谢!

代码

<!DOCTYPE html>
<html lang=en-US>
<head>
<title></title>
<style type="text/css">

iframe {
margin: 0 auto;
display: block;
border: 1px solid #ccc;
}

</style>
</head>

<body>
<iframe width="800" height="500"">

</iframe>

</body>
</html>

最佳答案

给你。

http://jsfiddle.net/UFMP7/1/

诀窍是使用嵌套元素的 margin 和 top offset 属性。

在这种情况下,给定一个 300px 的父 div (A),我将嵌套的 div (B) 偏移了 300 的 50%,即 150。因此 B 位于 A 容器顶部下方 150px 处.然而,我们还没有完成。为了使 B 的中心与 A 的中心匹配,我们需要将 B 高度的负 50% 应用到 margin-top 属性。这使它居中并且数学检查出来了。

如果您知道所有内容的尺寸(以像素为单位),那就更容易了。

您可以随意更改 A 的宽度或高度。它会动态居中。

div#a
{
width: 300px;
height: 300px;
border-width:1px;
border-style: solid;

/* important stuff below */
display: inline-block;
}

div#b
{
width: 60px;
height: 60px;
background-color: red;

/* important stuff below */
position: relative;
margin: -30px auto 0 auto;
top: 50%;
}

因此,我建议将您的 iframe 包装在一个 div 中。它给了你更多的控制权。再一次,我是那些过多的 div 包装器之一......

关于css - 如何垂直对齐 &lt;iframe&gt;?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com