在我們開發前端頁面的時候,為了讓頁面效果美觀,會讓圖片呈現居中效果。那麼css怎麼讓img圖片居中顯示呢?本篇文章帶給大家css如何讓img圖片居中? css的display屬性實作圖片居中(程式碼實例),讓大家可以了解並掌握css的display屬性設定img圖片居中的兩種方法。有一定的參考價值,有需要的朋友可以參考一下,希望對你們有幫助。
首先我們來了解一下display屬性實作圖片居中的兩種方法是什麼?
1、利用display的table-cell屬性值,再配合text-align: center;與vertical-align: middle;設定圖片居中
2、設定display: flex;,利用彈性佈局flex來設定img圖片的居中
下面我們透過簡單的程式碼範例,詳細了解這兩種方法是怎麼實現圖片居中的。
1、利用display:table-cell來實現img標籤圖片的水平和垂直居中
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>img图片居中</title> <style> .demo{ width: 400px; height: 300px; border: 1px dashed #000; display: table-cell; /*主要是这个属性*/ vertical-align: middle; text-align: center; } .demo img{ width: 200px; height: 150px; } </style> </head> <body> <div class="demo"> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn//upload/image/815/403/273/1539915497904295.jpg?x-oss-process=image/resize,p_40" class="lazy" / alt="css如何讓img圖片居中? css的display屬性實作圖片居中(程式碼實例)" > </div> </body> </html>
效果圖:
說明:
在demo盒子中設定display: table-cell;會讓demo盒子作為一個表格單元格顯示(類似
2、彈性佈局flex
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>img图片居中</title> <style> *{margin: 0;padding:0;} .demo{ width: 400px; height: 300px; margin: 50px auto; border: 1px dashed #000; display: flex; justify-content: center; align-items: center; } .demo img{ width: 200px; height: 150px; } </style> </head> <body> <div class="demo"> <img src="/static/imghwm/default1.png" data-src="https://img.php.cn//upload/image/815/403/273/1539915497904295.jpg?x-oss-process=image/resize,p_40" class="lazy" / alt="css如何讓img圖片居中? css的display屬性實作圖片居中(程式碼實例)" > </div> </body> </html>
效果圖:
說明:
設定display: flex;實現flex彈性佈局,在設定justify-content: center;讓圖片水平居中對齊,設定align-items: center;讓圖片垂直居中對齊。
總結:以上就是css的display屬性實現圖片居中兩種方法的全部介紹,希望能對大家的學習有所幫助。更多相關教學請訪問 CSS3影片教學,Html5影片教學,bootstrap影片教學!
以上是css如何讓img圖片居中? css的display屬性實作圖片居中(程式碼實例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Goofonts是由開發人員和設計師丈夫簽名的附帶項目,它們都是版式的忠實擁護者。我們一直在標記Google

學習如何構建GraphQL API可能具有挑戰性。但是您可以學習如何在10分鐘內使用GraphQL API!碰巧的是,我得到了完美的

這裡是Yuanchuan的一些合法CSS騙局。有此CSS屬性偏移路徑。曾幾何時,它被稱為Motion-Path,然後被更名。我


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

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

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

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版