前端初學者,小白白一枚,都是靠自己學習的,若有問題,請多指教。
整理一下在很多面試題上面看到一道題:
將一個元素垂直水平居中:
1.若元素未知寬高或已知寬高:
將元素的父元素設置為:display:flex;justify-content:center;align-items:center;并設置父元素的寬高即可:
如:
<style> html{ width: 100%; height: 100%; display:flex; justify-content:center; align-items:center; } #box{ background-color:red; } </style> <body> <div id="box">未知寬高</div> </body>
2.若是一個已知寬高的元素也可以這樣:將父元素CSS設置為:position:relative;并設置其寬高;
將這個元素:設置寬高,position:absolute;left:0;top:0;right:0;bottom:0;margin:auto;
<style> html{ width:100%; height:100%; position:relative; } #box{ background-color:red; width:100px; height:100px; position:absolute; left:0; right:0; top:0; bottom:0; margin:auto; } </style> <body> <div id="box">已知寬高</div> </body>
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦