HTMLSourceElement: thuộc tính width

Thuộc tính width của giao diện HTMLSourceElement là một số không âm cho biết chiều rộng của tài nguyên hình ảnh tính bằng pixel CSS.

Thuộc tính này chỉ có hiệu lực khi phần tử cha của phần tử <source> hiện tại là phần tử <picture>.

Nó phản ánh thuộc tính width của phần tử <source>.

Giá trị

Một số không âm cho biết chiều rộng của tài nguyên hình ảnh tính bằng pixel CSS.

Ví dụ

html
<picture id="img">
  <source
    srcset="landscape.png"
    media="(width >= 1000px)"
    width="1000"
    height="400" />
  <source
    srcset="square.png"
    media="(width >= 800px)"
    width="800"
    height="800" />
  <source
    srcset="portrait.png"
    media="(width >= 600px)"
    width="600"
    height="800" />
  <img
    src="fallback.png"
    alt="Image used when the browser does not support the sources"
    width="500"
    height="400" />
</picture>
js
const img = document.getElementById("img");
const sources = img.querySelectorAll("source");
console.log(Array.from(sources).map((el) => el.width)); // Output: [1000, 800, 600]

Thông số kỹ thuật

Specification
HTML
# dom-dim-width

Tương thích trình duyệt

Xem thêm