Vertically align div (no tables)

后端 未结 8 1309
孤街浪徒
孤街浪徒 2020-12-02 17:38

I can horizontally align a div and all the content looks nice. Looking to vertical align a div that does not contain any tables. I tried setting margin positions to some neg

8条回答
  •  庸人自扰
    2020-12-02 18:22

    There are a few ways to do this, all with compromises.

    • Use position:absolute, a fixed height, and overflow:auto.
    • Use display:table, display:table-cell, and vertical-align:middle
    • Use javascript

    I think option #2 is pretty good.

    edit: I don't think option 2 will work in IE. You may be stuck with javascript if you want to keep the height dynamic.

提交回复
热议问题