Home >Web Front-end >CSS Tutorial >Why Is My Iframe Not Centering? (The `display` Property Fix)

Why Is My Iframe Not Centering? (The `display` Property Fix)

DDD
DDDOriginal
2024-10-29 04:05:29473browse

Why Is My Iframe Not Centering? (The `display` Property Fix)

Horizontal Centering of Iframes

If you encounter an iframe that is not horizontally centered despite using margin: 0 auto;, the issue may lie in the display property of the iframe. By default, iframes have an inline display, which prevents them from aligning with block-level elements like divs.

To resolve this issue and center the iframe horizontally, add the following CSS rule:

<code class="css">iframe {
    display: block;
}</code>

This change sets the display property of the iframe to block, allowing it to behave like a block-level element. Consequently, the iframe will now correctly align horizontally with the surrounding div.

Therefore, the following updated CSS code will center both the div and the iframe horizontally:

<code class="css">div, iframe {
    width: 100px;
    height: 50px;
    margin: 0 auto;
    background-color: #777;
}

iframe {
    display: block;
    border-style: none;
}</code>

The above is the detailed content of Why Is My Iframe Not Centering? (The `display` Property Fix). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn