Thẻ HTML <iframe>


Thí dụ

Khung nội tuyến được đánh dấu như sau:

<iframe src="https://www.w3schools.com" title="W3Schools Free Online Web Tutorials"></iframe>

Thêm các ví dụ "Hãy tự mình thử" bên dưới.


Định nghĩa và Cách sử dụng

Thẻ <iframe>chỉ định một khung nội tuyến.

Khung nội tuyến được sử dụng để nhúng tài liệu khác vào tài liệu HTML hiện tại.

Mẹo: Sử dụng CSS để tạo kiểu <iframe> (xem ví dụ bên dưới). 

Mẹo: Một phương pháp hay là luôn bao gồm thuộc tính tiêu đề cho <iframe>. Điều này được sử dụng bởi trình đọc màn hình để đọc nội dung của <iframe> nó là gì.


Hỗ trợ trình duyệt

Element
<iframe> Yes Yes Yes Yes Yes

Thuộc tính

Attribute Value Description
allow   Specifies a feature policy for the <iframe>
allowfullscreen true
false
Set to true if the <iframe> can activate fullscreen mode by calling the requestFullscreen() method
allowpaymentrequest true
false
Set to true if a cross-origin <iframe> should be allowed to invoke the Payment Request API
height pixels Specifies the height of an <iframe>. Default height is 150 pixels
loading eager
lazy
Specifies whether a browser should load an iframe immediately or to defer loading of iframes until some conditions are met
name text Specifies the name of an <iframe>
referrerpolicy no-referrer
no-referrer-when-downgrade
origin
origin-when-cross-origin
same-origin
strict-origin-when-cross-origin
unsafe-url
Specifies which referrer information to send when fetching the iframe
sandbox allow-forms
allow-pointer-lock
allow-popups
allow-same-origin
allow-scripts
allow-top-navigation
Enables an extra set of restrictions for the content in an <iframe>
src URL Specifies the address of the document to embed in the <iframe>
srcdoc HTML_code Specifies the HTML content of the page to show in the <iframe>
width pixels Specifies the width of an <iframe>. Default width is 300 pixels


Thuộc tính toàn cầu

Thẻ <iframe>cũng hỗ trợ Thuộc tính chung trong HTML .


Thuộc tính sự kiện

Thẻ <iframe>cũng hỗ trợ Thuộc tính sự kiện trong HTML .


Các ví dụ khác

Thí dụ

Thêm và xóa đường viền iframe (với CSS):

<iframe src="/default.asp" width="100%" height="300" style="border:1px solid black;">
</iframe>

<iframe src="/default.asp" width="100%" height="300" style="border:none;">
</iframe>

Các trang liên quan

Hướng dẫn HTML: Khung nội tuyến HTML

Tham chiếu DOM HTML: Đối tượng IFrame


Cài đặt CSS mặc định

Hầu hết các trình duyệt sẽ hiển thị <iframe>phần tử với các giá trị mặc định sau:

iframe:focus {
  outline: none;
}

iframe[seamless] {
  display: block;
}