是否可以將div垂直居中放置在%高度div中?
3 回答

慕的地10843
TA貢獻1785條經(jīng)驗 獲得超8個贊
如果您的內部div絕對高(比如說100px),則可以執(zhí)行以下操作:
.outerdiv{ position: relative; //or absolute, or fixed height: 80%;}.innerdiv{ position: absolute; width: 100px; height: 100px; top: 50%; // it's at 50% but not really centered margin-top: -50px; // so just move it up by the half of its height :D}
我不喜歡這種解決方案,我敢肯定還有很多其他可能性(也許使用表或display: table-cell;
)-但這是我想到的第一個...

慕哥6287543
TA貢獻1831條經(jīng)驗 獲得超10個贊
不需要px單位更改頂部,底部,右側,左側或使用百分比
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert title here</title></head><body><div style="position: absolute; top: 0; bottom: 0; right: 0; left: 0; text-align: center; white-space: nowrap; overflow: hidden;"> <div style="position: relative; display: inline-block; height: 100%; vertical-align: middle;"></div> <div style="background-color: #FEEF88; position: relative; display: inline-block; vertical-align: middle;">Hola todo el mundo :D</div></div></body></html>
添加回答
舉報
0/150
提交
取消