如何使用 CSS 移除 iframe 的邊框?


使用 CSSiframe 中移除邊框在您不想在網頁上整合其他來源的內容(如 YouTube 影片、其他網頁等)時顯示不需要的邊框時非常有用。iframe 是一個內聯框架,允許我們在當前 HTML 文件中嵌入另一個文件。

在本文中,我們將討論使用 CSS 從 iframe 中移除邊框的各種方法。我們有一個 iframe 視窗,它在我們的 HTML 文件中顯示其他網頁的內容。我們的任務是移除 iframe 視窗周圍的邊框。

移除 iframe 邊框的方法

以下是使用 CSS 從 iframe 中移除邊框的方法列表,我們將在本文中逐步解釋並提供完整的示例程式碼。

使用 frameborder 屬性移除 iframe 邊框

要從 iframe 中移除邊框,我們使用了 frameborder 屬性。HTML frameborder 屬性指定是否顯示 iframe 邊框。

  • 我們使用 CSS heightwidth 屬性設定了 iframe 視窗的尺寸。
  • 我們使用了 **frameborder="0"** 屬性來移除 iframe 的邊框。

示例

這是一個完整的示例程式碼,實現了上述步驟,使用 frameborder 屬性從 iframe 中移除邊框。

<html>
<head>
    <title>
        Remove border from IFrame using CSS
    </title>
    <style>
        iframe {
            height: 200px;
            width: 400px;
        }
    </style>
</head>
<body>
    <h3>
        Removing border from IFrame using CSS
    </h3>
    <p>
        In this example we have used 
        <strong>frameborder</strong> attribute to 
        remove the border from IFrame using CSS.
    </p>
    <iframe src="/css/index.htm" frameborder="0">
    </iframe>
</body>
</html>

使用 border 屬性移除 iframe 邊框

在這種方法中,我們將使用 border 屬性,該屬性可新增或移除元素周圍的邊框。在這種方法中,我們將對 iframe 使用 border 屬性。

  • 我們使用 CSS height 和 width 屬性設定了 iframe 視窗的尺寸。
  • 我們使用了 CSS **"border: none;"** 屬性來移除 iframe 的邊框。

示例

這是一個完整的示例程式碼,實現了上述步驟,使用 CSS border 屬性從 iframe 中移除邊框。

<html>
<head>
    <title>
        Remove border from IFrame using CSS
    </title>
    <style>
        iframe {
            border: none;
            height: 200px;
            width: 400px;
        }
    </style>
</head>
<body>
    <h3>
        Removing border from IFrame using CSS
    </h3>
    <p>
        In this example we have used CSS
        <strong>border</strong> property to
        remove the border from IFrame using CSS.
    </p>
    <iframe src="/css/index.htm"></iframe>
</body>
</html>

使用 border-width 屬性移除 iframe 邊框

在這種方法中,我們將使用 border-width 屬性,該屬性指定任何元素的邊框寬度。

  • 我們使用 CSS height 和 width 屬性設定了 iframe 視窗的尺寸。
  • 我們使用 CSS **"border-width: 0;"** 屬性將邊框寬度值設定為 0,從而移除 iframe 的邊框。

示例

這是一個完整的示例程式碼,實現了上述步驟,使用 CSS border-width 屬性從 iframe 中移除邊框。

<html>
<head>
    <title>
        Remove border from IFrame using CSS
    </title>
    <style>
        iframe {
            height: 200px;
            width: 400px;
            border-width: 0;
        }
    </style>
</head>
<body>
    <h3>
        Removing border from IFrame using CSS
    </h3>
    <p>
        In this example we have used CSS
        <strong>border-width</strong> property to
        remove the border from IFrame using CSS.
    </p>
    <iframe src="/css/index.htm"></iframe>
</body>
</html>

結論

在本文中,我們瞭解瞭如何使用 CSS 從 iframe 中移除邊框。我們討論了三種從 iframe 中移除邊框的方法:使用 **frameborder** HTML 屬性(其值可以是 0 或 1),使用 CSS **border** 和使用 **border-width** 屬性。CSS border-width 屬性不會從 iframe 中移除邊框,而是由於寬度為零而隱藏邊框,並且在某些瀏覽器中 frameborder CSS 屬性已棄用,因此建議使用 CSS border 屬性。

更新於: 2024年8月5日

7K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.