如何在 HTML 中製作響應式影像?
自適應影像會自動調整內容以適應螢幕及選項卡大小。
要製作自適應影像,我們首先必須使用 <img> 標記將其新增到網頁中,然後透過使用樣式表,我們可以更改影像的引數,使 HTML 中的影像自適應。
語法
以下是 HTML 中自適應影像的語法。
<img src="image.jpg" alt="alternate text…" style= "width: 100%;height: auto">
示例
以下是 HTML 中自適應影像的示例程式。這裡,我們使用了內聯樣式表。
<!DOCTYPE html> <html> <head> </head> <body> <h3>Responsive Image</h3> <img src="https://tutorialspoint.tw/coffeescript/images/coffeescript-mini-logo.jpg" alt="test image" class="responsive" style="width: 100%;height: auto"> </body> </html>
執行以上程式後,影像即會顯示,你可以調整顯示的影像。
示例
在以下示例中,我們使用了內部樣式表。
<!DOCTYPE html> <html> <head> <style> .responsive { width: 100%; height: auto; } </style> </head> <body> <h3>Responsive Image</h3> <img src="https://tutorialspoint.tw/coffeescript/images/coffeescript-mini-logo.jpg" alt="test image" class="responsive" > </body> </html>
執行以上程式後,影像將顯示。如果我們希望將自適應影像限制在最大尺寸,我們可以用畫素值使用 max-width 屬性。
示例
以下為自適應影像限制最大尺寸的示例程式。
<!DOCTYPE html> <html> <head> <style> .responsive { max-width: 100%; height: auto } </style> </head> <body> <h3>Responsive Image</h3> <img src="https://tutorialspoint.tw/coffeescript/images/coffeescript-mini-logo.jpg" alt="test image" class="responsive" > </body> </html>
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP