在響應式容器內垂直對齊圖像
挑戰
您有一個HTML 結構,其中一個容器保持方形縱橫比作為瀏覽器視窗已調整大小。在此容器內,您想要新增映像,但需要確保它保持垂直對齊。挑戰的出現是因為影像的高度是可變的,容器的高度無法固定。
解
使用CSS 內嵌元素
- 建立一個內聯塊偽元素作為容器元素的第一個(或容器元素的第一個(或容器元素最後一個)子元素,並將其高度設為100%以佔據整個容器的高度。
- 為偽元素和影像設定vertical-align: middle,使元素垂直居中。
- 透過設定 font-size 刪除元素之間的任何空白: 0;在容器元素上刪除字元(空格)所佔用的空間。
HTML:
<div class="container"> <div> <p>CSS:</p> <pre class="brush:php;toolbar:false">.container { height: 300px; text-align: center; /* align the inline(-block) elements horizontally */ font: 0/0 a; /* remove the gap between inline(-block) elements */ } .container:before { /* create a full-height inline block pseudo=element */ content: ' '; display: inline-block; vertical-align: middle; /* vertical alignment of the inline element */ height: 100%; } #element { display: inline-block; vertical-align: middle; /* vertical alignment of the inline element */ font: 16px/1 Arial sans-serif; /* <h3 id="製作容器響應式">製作容器響應式</h3><p>要建立高度相對於寬度調整大小的響應式容器,您可以使用百分比值對於頂部/底部填充屬性:</p><pre class="brush:php;toolbar:false">.responsive-container { width: 60%; padding-top: 60%; /* 1:1 Height is the same as the width */ padding-top: 100%; /* width:height = 60:100 or 3:5 */ padding-top: 45%; /* = 60% * 3/4 , width:height = 4:3 */ padding-top: 33.75%; /* = 60% * 9/16, width:height = 16:9 */ }
包裝圖像內容
為了避免容器頂部或底部出現過多空間,請將圖像包裝在包裝元素中並將其定位絕對在容器內以填充其整個空間:
.responsive-container { width: 60%; position: relative; } .responsive-container .wrapper { position: absolute; top: 0; right: 0; bottom: 0; left: 0; }
包括影像和CSS對齊
HTML:
<div class="responsive-container"> <div class="dummy"></div> <div class="img-container"> <img src="/static/imghwm/default1.png" data-src="http://placehold.it/150x150" class="lazy" alt=""> </div> </div>
用於圖片對齊的CSS:
.img-container { text-align: center; /* Align center inline elements */ font: 0/0 a; /* Hide the characters like spaces */ } .img-container:before { content: ' '; display: inline-block; vertical-align: middle; height: 100%; } .img-container img { vertical-align: middle; display: inline-block; }
瀏覽器相容性的替代方案
為了更喜歡好地跨瀏覽器相容性,您可以使用div元素作為影像容器的第一個子元素而不是偽元素:
HTML:
<div class="img-container"> <div class="centerer"></div> <img src="/static/imghwm/default1.png" data-src="http://placehold.it/150x150" class="lazy" alt=""> </div>
CSS:
.img-container .centerer { display: inline-block; vertical-align: middle; height: 100%; }
使用max-* 屬性進行影像控制
保留影像當容器內寬度較小時,可以使用max-height 和max-width屬性圖:
.img-container img { max-height: 100%; /* Set maximum height to 100% of its parent */ max-width: 100%; /* Set maximum width to 100% of its parent */ }
以上是如何在響應式容器內垂直對齊影像?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

@keyframesandCSSTransitionsdifferincomplexity:@keyframesallowsfordetailedanimationsequences,whileCSSTransitionshandlesimplestatechanges.UseCSSTransitionsforhovereffectslikebuttoncolorchanges,and@keyframesforintricateanimationslikerotatingspinners.

我知道,我知道:有大量的內容管理系統選項可用,而我進行了幾個測試,但實際上沒有一個是一個,y&#039;知道嗎?怪異的定價模型,艱難的自定義,有些甚至最終成為整個&

鏈接CSS文件到HTML可以通過在HTML的部分使用元素實現。 1)使用標籤鏈接本地CSS文件。 2)多個CSS文件可通過添加多個標籤實現。 3)外部CSS文件使用絕對URL鏈接,如。 4)確保正確使用文件路徑和CSS文件加載順序,優化性能可使用CSS預處理器合併文件。

選擇Flexbox還是Grid取決於佈局需求:1)Flexbox適用於一維佈局,如導航欄;2)Grid適合二維佈局,如雜誌式佈局。兩者在項目中可結合使用,提升佈局效果。

包含CSS文件的最佳方法是使用標籤在HTML的部分引入外部CSS文件。 1.使用標籤引入外部CSS文件,如。 2.對於小型調整,可以使用內聯CSS,但應謹慎使用。 3.大型項目可使用CSS預處理器如Sass或Less,通過@import導入其他CSS文件。 4.為了性能,應合併CSS文件並使用CDN,同時使用工具如CSSNano進行壓縮。

是的,youshouldlearnbothflexboxandgrid.1)flexboxisidealforone-demensional,flexiblelayoutslikenavigationmenus.2)gridexcelstcelsintwo-dimensional,confffferDesignssignssuchasmagagazineLayouts.3)blosebothenHancesSunHanceSlineHancesLayOutflexibilitibilitibilitibilitibilityAnderibilitibilityAndresponScormentilial anderingStruction

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

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用