使用文字換行浮動圖像:綜合指南
在HTML 和CSS 中,將圖像放置在文字旁邊可能是一個挑戰,尤其是當您希望圖像浮動到右下角和環繞它的文字。本文探討如何使用 spacer 元素和 JavaScript 來實現此效果。
建立一個Spacer 元素
要將映像推送到頁面底部,請建立一個帶有float: right 的spacer 元素,高度等於內容和映像之間的差異高度:
<div class="spacer"></div> <img class="bottomRight" src="" alt="如何在 HTML 和 CSS 中使用文字換行將圖像浮動到右下角?" >
CSS樣式
使用以下CSS 樣式:
.spacer { float: right; width: 0px; } .bottomRight { float: right; clear: right; }
計算間隔高度
要精確定位影像,您需要計算使用JavaScript 計算間隔符號的高度。函數以spacer 元素為參數:
function sizeSpacer(spacer) { var container = spacer.parentNode; var img = spacer.nextElementSibling || spacer.nextSibling; var lastContentNode = container.children[container.children.length - 1]; var h = Math.max(0, container.clientHeight - img.clientHeight); var imgBottom = img.getBoundingClientRect().bottom; var lastContentBottom = lastContentNode.getBoundingClientRect().bottom; // Adjust spacer height to align with content bottom while (h > 0 && imgBottom > lastContentBottom) { spacer.style.height = --h + "px"; imgBottom = img.getBoundingClientRect().bottom; lastContentBottom = lastContentNode.getBoundingClientRect().bottom; } if (lastContentBottom > imgBottom) { spacer.style.height = ++h + "px"; } }
jQuery 外掛程式
為了方便起見,您可以使用這個支援左下方或右下方浮動影像的jQuery 外掛:
$(function() { $(".bottomRight").bottomRight(); });
以上是如何在 HTML 和 CSS 中使用文字換行將圖像浮動到右下角?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

React生態系統為我們提供了許多庫,所有庫都集中在拖放的相互作用上。我們有反應,反應,可愛dnd,

我可以說我經常使用背景折疊。 IT Wager IT幾乎從未在日常CSS工作中使用。但是在斯特凡·朱迪斯(Stefan Judis)的帖子中,我想起了它,

使用RequestAnimationFrame進行動畫化應該很容易,但是如果您還沒有徹底閱讀React的文檔,那麼您可能會遇到一些事情

聽著,我不是GraphQL專家,但我確實喜歡與之合作。作為前端開發人員,它向我曝光數據的方式非常酷。它就像一個菜單


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中