」工欲善其事,必先利其器。「—孔子《論語.錄靈公》
首頁 > 程式設計 > 在 HTML 和 CSS 中使 Div 居中的不同方法

在 HTML 和 CSS 中使 Div 居中的不同方法

發佈於2024-08-02
瀏覽:374

Different Ways to Center a Div in HTML and CSS

在 LinkedIn 上關注我
在 Github.com 上關注我

點選閱讀

沒有Boaring Setion,我們可以重定向到編碼!

1. 使用 Flexbox

Flexbox 是一款功能強大的佈局工具,可輕鬆地將元素水平和垂直居中。

例子:



    
    
    Center Div with Flexbox
    


    
Centered with Flexbox

2. 使用網格

CSS Grid 是另一個強大的佈局系統,可輕鬆地將元素置中。

例子:



    
    
    Center Div with Grid
    


    
Centered with Grid

3. 使用絕對定位和變換

此方法涉及絕對定位 div 並使用變換將其居中。

例子:



    
    
    Center Div with Absolute Positioning
    


    
Centered with Absolute Positioning

4. 使用自動保證金

對指定寬度的元素設定margin: auto可以使其水平居中。

例子:



    
    
    Center Div with Margin Auto
    


    
Centered with Margin Auto

5. 使用表格顯示

此方法使用 display: table 和 display: table-cell 讓元素置中。

例子:



    
    
    Center Div with Table Display
    


    
Centered with Table Display

拜耶
快樂編碼!

版本聲明 本文轉載於:https://dev.to/muhammedshamal/different-ways-to-center-a-div-in-html-and-css-47df?1如有侵犯,請聯絡[email protected]刪除
最新教學 更多>

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3