縮放影像以填充邊界框並保持縱橫比
縮放影像以適合邊界框,同時保持其縱橫比可以對CSS 提出挑戰。
考慮以下用例:
- 用例 1: 映像大於容器。
- 用例 2: 映像小於容器(縮小)。
- 用例 3: 映像小於容器(放大)。
標準 CSS 屬性 max-width 和 max-height 僅在圖像大於容器時才起作用(用例 1 和 2)。
幸運的是,CSS3 為用例 3 提供了解決方案。透過設定將圖像作為背景圖像並使用帶有包含值的背景大小屬性,圖像可以放大以填充容器而不扭曲其縱橫比。
HTML:
<div class="bounding-box"> </div>
CSS:
.bounding-box { background-image: url(...); background-repeat: no-repeat; background-size: contain; }
將影像在容器內居中:
.bounding-box { background-image: url(...); background-size: contain; position: absolute; background-position: center; background-repeat: no-repeat; height: 100%; width: 100%; }此解決方案與大多數現代瀏覽器相容。
以上是如何在 CSS 中縮放圖片以填充邊界框,同時保留寬高比?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

重構自己的代碼看起來是什麼樣的?約翰·瑞亞(John Rhea)挑選了他寫的一個舊的CSS動畫,並介紹了優化它的思維過程。

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatoryand and powerincreatingsmoothcsssanimations.keytricksinclude:1)definingsmoothtransitionsbetnestates,2)使用AnimatingMultatingMultationMultationProperPertiessimultane,3)使用使用4)使用BombingeNtibalibility,4)使用CombanningWiThjavoFofofofoftofofo

CSSCOUNTERSAREDOMANAGEAUTOMANAMBERINGINWEBDESIGNS.1)他們可以使用forterablesofcontents,ListItems,and customnumbering.2)AdvancedsincludenestednumberingSystems.3)挑戰挑戰InclassINCludeBrowsEccerCerceribaliblesibility andperformiballibility andperformissises.4)創造性

使用滾動陰影,尤其是對於移動設備,是克里斯以前涵蓋的一個微妙的UX。傑夫(Geoff)涵蓋了一種使用動畫限制屬性的新方法。這是另一種方式。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

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

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。