Jak wyśrodkować iframe poziomo?

Rozważ następujący przykład: ( live demo)

HTML:

<div>div</div>
<iframe></iframe>

CSS:

div, iframe {
    width: 100px;
    height: 50px;
    margin: 0 auto;
    background-color: #777;
}

Wynik:

Tutaj wpisz opis obrazka

Dlaczego iframe nie jest centralnie wyrównany jak div? Jak mógłbym to centralnie wyrównać?

Author: Misha Moroshko, 2011-12-03

8 answers

Dodaj display:block; do iframe css.

 323
Author: Alohci,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-03 10:14:49

Najlepszym i prostszym sposobem wyśrodkowania iframe na swojej stronie jest:

<p align="center"><iframe src="http://www.google.com/" width=500 height="500"></iframe></p>

Gdzie szerokość i wysokość będą wielkością iframe na stronie html.

 26
Author: pedro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-15 14:18:56

HTML:

<div id="all">
    <div class="sub">div</div>
    <iframe>ss</iframe>
</div>

CSS:

#all{
    width:100%;
    float:left;
    text-align:center;
}
div.sub, iframe {
    width: 100px;
    height: 50px;
    margin: 0 auto;
    background-color: #777;

}
 19
Author: mgraph,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-25 16:06:23

Jeśli umieszczasz wideo w ramce iframe i chcesz, aby twój układ był płynny, powinieneś spojrzeć na tę stronę: Fluid Width Video

W zależności od źródła wideo i jeśli chcesz, aby stare filmy były responsywne, Twoja taktyka będzie musiała się zmienić.

Jeśli jest to twój pierwszy film, oto proste rozwiązanie:

<div class="videoWrapper">
    <!-- Copy & Pasted from YouTube -->
    <iframe width="560" height="349" src="http://www.youtube.com/embed/n_dZNLr2cME?rel=0&hd=1" frameborder="0" allowfullscreen></iframe>
</div>

I dodaj ten css:

.videoWrapper {
	position: relative;
	padding-bottom: 56.25%; /* 16:9 */
	padding-top: 25px;
	height: 0;
}
.videoWrapper iframe {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
}

Disclaimer: nic z tego nie jest moim kodem, ale Przetestowałem go i był zadowolony z wyników.

 10
Author: Nohl,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-10-13 18:38:23

Najprostszy kod do wyrównania elementu iframe:

<div align="center"><iframe width="560" height="315" src="www.youtube.com" frameborder="1px"></iframe></div>
 7
Author: Tushar Soni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-18 15:46:40

Możesz umieścić iframe wewnątrz <div>

<div>
    <iframe></iframe>
</div>

Działa, ponieważ znajduje się teraz wewnątrz elementu blokowego.

 6
Author: Sonal Khunt,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-03 21:51:45

Według http://www.w3schools.com/css/css_align.asp , Ustawienie lewego i prawego marginesu na auto określa, że powinny one podzielić dostępny margines równo. Wynik jest elementem wyśrodkowanym:

margin-left: auto;margin-right: auto;
 4
Author: boussac,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-09-25 16:18:40

Możesz spróbować

<h3 style="text-align:center;"><iframe src=""></iframe></h3>

Mam nadzieję, że to przydatne dla ciebie

Link

 1
Author: Kael,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-25 04:02:58