CSS笔记 - 几种水平垂直居中方式

水平垂直居中的方式对比

绝对定位的方式

/* 这种居中方式,只适用于元素的大小确定时 */
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto;

table-cell的方式

/* table-cell的方式具有一定局限性 */
display: table-cell;
vertical-align: middle;
text-align: center;

transform的方式

/* transform变形平移的方式 */
position: absolute;
left: 50%;
top: 50%;
transform: translateX(-50%) translateY(-50%);

flex的方式

/*弹性盒的居中非常方便且常见*/
display: flex;
align-items: center;
justify-content: center;
posted @ 2022-10-31 02:48  Solitary-Rhyme  阅读(29)  评论(0)    收藏  举报