響應式預覽與尺寸
依 mobile / tablet / desktop 三檔視埠算出 iframe 尺寸、縮放比與是否出現橫向捲動,輸出一份帶 media query 的預覽文件,並把它放進空 sandbox 的 iframe 裡直接渲染出來(文件裡的指令碼不會執行),這份 HTML 也能下載。
瀏覽器本機執行所有計算都在你的瀏覽器裡完成,資料不會離開本機。
HTML 原始碼
結果
處理結果可直接複製。
貼上 HTML 後即可看到三檔視埠尺寸
字元數: 0 · 行數: 0 · 視埠數: 0 · 刪除屬性: 0
這個工具能做什麼
- 在改樣式前先確認三檔視口會不會出現橫向滾動。
- 拿到各視口的建議尺寸,餵給截圖指令碼或設計稿。
- 生成一份可下載的預覽文件,脫離工具頁面也能看。
- 頁面上直接看到三檔視口的渲染結果,並可下載這份 HTML。
範例
輸入
<div width="800">content</div>
輸出
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive preview</title>
<style>body{margin:0;background:#f5f6f8;font:13px/1.6 system-ui,sans-serif}.frames{display:flex;flex-wrap:wrap;gap:16px;padding:16px}.frame{background:#fff;border:1px solid #e4e6eb;border-radius:8px;overflow:hidden}.frame figcaption{padding:6px 10px;color:#5b6270}@media (max-width:900px){.frames{flex-direction:column}}</style>
</head>
<body>
<div class="frames">
<figure class="frame"><figcaption>mobile 375×667</figcaption><iframe title="mobile" width="375" height="667"></iframe></figure>
<figure class="frame"><figcaption>tablet 768×1024</figcaption><iframe title="tablet" width="768" height="1024"></iframe></figure>
<figure class="frame"><figcaption>desktop 1440×900</figcaption><iframe title="desktop" width="1440" height="900"></iframe></figure>
<iframe title="mobile" width="375" height="667" src="about:blank" data-viewport="mobile"></iframe>
<iframe title="tablet" width="768" height="1024" src="about:blank" data-viewport="tablet"></iframe>
<iframe title="desktop" width="1440" height="900" src="about:blank" data-viewport="desktop"></iframe>
</div>
</body>
</html>
尺寸是計算結果,渲染靠頁面;`<div width="800">` 在 375px 視口下會被標記 `scrollX: true`。
常見問題
它會真的渲染三個 iframe 嗎?
會。邏輯側只負責算尺寸並輸出文件,頁面把這份文件放進空 sandbox 的 iframe 裡渲染,所以 mobile / tablet / desktop 三檔是真實畫出來的;sandbox 沒有開 allow-scripts,文件裡的指令碼不會執行。
scrollX 是怎麼判斷的?
取 HTML 裡第一個帶 `width` 屬性的元素值,與該視口寬度比較,超出即為 true;這只是提示,不是佈局引擎。
retina 影響什麼?
隻影響輸出裡的 scale(mobile/tablet 為 2,desktop 為 1),不改變 CSS 畫素尺寸。
gap 有什麼限制?
必須是不小於 0 的有限數字,否則拋 `invalid-gap`。
關鍵字:响应式预览视口尺寸html-responsive-previewviewportiframedeveloper