CSS 元素置中

更新於 發佈於 閱讀時間約 3 分鐘

以下展示三種置中方法 :

raw-image


<div class="method-container">
<div class="method-title">Flexbox</div>
<div class="box">
<div class="dot"></div>
</div>
</div>

<div class="method-container">
<div class="method-title">Absolute </div>
<div class="box box-absolute">
<div class="dot"></div>
</div>
</div>

<div class="method-container">
<div class="method-title">Margin 0 Auto</div>
<div class="box" style="position: relative; margin: 0 auto;">
<div class="dot" style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);"></div>
</div>
</div>


body {
display: flex;
justify-content: space-around;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}

.method-container {
display: flex;
flex-direction: column;
align-items: center;
}

.method-title {
margin-bottom: 10px;
font-weight: bold;
}

.box {
display: flex;
justify-content: center;
align-items: center;
width: 200px;
height: 200px;
border: 1px solid red;
margin-bottom: 20px;
}

.box-absolute {
position: relative;
}

.box-absolute div {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}

.dot {
width: 20px;
height: 20px;
background-color: red;
border-radius: 50%;
}
avatar-img
3會員
17內容數
留言0
查看全部
avatar-img
發表第一個留言支持創作者!
你可能也想看
Google News 追蹤
Thumbnail
/ 大家現在出門買東西還會帶錢包嗎 鴨鴨發現自己好像快一個禮拜沒帶錢包出門 還是可以天天買滿買好回家(? 因此為了記錄手機消費跟各種紅利優惠 鴨鴨都會特別注意銀行的App好不好用! 像是介面設計就是會很在意的地方 很多銀行通常會為了要滿足不同客群 會推出很多App讓使用者下載 每次
Thumbnail
/ 大家現在出門買東西還會帶錢包嗎 鴨鴨發現自己好像快一個禮拜沒帶錢包出門 還是可以天天買滿買好回家(? 因此為了記錄手機消費跟各種紅利優惠 鴨鴨都會特別注意銀行的App好不好用! 像是介面設計就是會很在意的地方 很多銀行通常會為了要滿足不同客群 會推出很多App讓使用者下載 每次