本文示範如何設定圖片的 hspace 與 vspace 屬性。
這兩個屬性的單位是像素:hspace 指定圖片左右側與周圍文字之間的間距,vspace 則是上下側的間距。
<!DOCTYPE html> <html> <head> <script> function setSpace() { document.getElementById("compman").hspace="50"; document.getElementById("compman").vspace="50"; } </script> </head> <body> <img id="compman" src="compman.gif" alt="Computerman" width="107" height="98"> <p>Some text. Some text. Some text. Some text.</p> <input type="button" onclick="setSpace()" value="Set hspace and vspace"> </body> </html>
實務建議:hspace 與 vspace 同屬 HTML 的舊式呈現屬性,已不建議使用。改用 CSS margin 不但能達到相同效果,還可以分開控制上下左右四邊,例如 img.style.margin = "50px 20px",排版上更為精準。
希望本文所述對大家的 JavaScript 程式設計有所幫助。