考虑下面的例子:(现场演示)
HTML:
<div>div</div>
<iframe></iframe>
CSS:
div, iframe {
width: 100px;
height: 50px;
margin: 0 auto;
background-color: #777;
}
结果:
为什么iframe不像div一样集中对齐?我怎么把它放在中间?
考虑下面的例子:(现场演示)
HTML:
<div>div</div>
<iframe></iframe>
CSS:
div, iframe {
width: 100px;
height: 50px;
margin: 0 auto;
background-color: #777;
}
结果:
为什么iframe不像div一样集中对齐?我怎么把它放在中间?
当前回答
在我的情况下,解决方案是在iframe类添加:
display: block;
margin-right: auto;
margin-left: auto;
其他回答
如果你把一个视频放在iframe中,你想让你的布局是流畅的,你应该看看这个网页:流体宽度视频
取决于视频来源,如果你想让旧视频变得有响应,你的策略将需要改变。
如果这是你的第一个视频,这里有一个简单的解决方案:
< div class = " videoWrapper”> <!——从YouTube复制粘贴——> <iframe width="560" height="349" src="http://www.youtube.com/embed/n_dZNLr2cME?rel=0&hd=1" frameborder="0" allowfullscreen></iframe> < / div >
并添加这个css:
.videoWrapper { 位置:相对; padding-bottom: 56.25%;/* 16:9 */ padding-top: 25 px; 高度:0; } .videoWrapper iframe { 位置:绝对的; 上图:0; 左:0; 宽度:100%; 高度:100%; }
免责声明:这些都不是我的代码,但我已经进行了测试,并对结果感到满意。
你可以把iframe放在<div>中
<div>
<iframe></iframe>
</div>
它可以工作,因为它现在在块元素中。
<iframe src="https://www.facebook.com/plugins/page.php?href=https%3A%2F%2Fwww.facebook.com%2FRishabh-Cars-Jodhpur-110479104559774&tabs=timeline&width=500&height=1200&small_header=false&adapt_container_width=true&hide_cover=false&show_facepile=true&appId" width="500" height="1200" style="border:none;overflow:hidden;display:block;margin:0 auto;"6" allowfullscreen="true" allow="自动播放;clipboard-write;加密介质;画中画;合伙分成应得的" > < / iframe >
使用它和嵌入facebook在iframe中心的HTML页面
添加显示:块;到你的iframe css。
Div, iframe { 宽度:100 px; 高度:50 px; 保证金:0自动; background - color: # 777; } iframe { 显示:块; 边框样式:没有; } < div > div < / div > < iframe的src = "数据:iframe " > < / iframe >
对齐iframe元素的最简单代码:
<div align="center"><iframe width="560" height="315" src="www.youtube.com" frameborder="1px"></iframe></div>