开发者

Center on viewport - a fixed height a width div VERTICALLY using either js or CSS method

开发者 https://www.devze.com 2023-01-21 08:11 出处:网络
I hav开发者_运维知识库e a div with several images inside, and I would like to center this vertically. I can give a specific width and height do the div. No prob. I also accept js if I must to. I\'m ju

I hav开发者_运维知识库e a div with several images inside, and I would like to center this vertically. I can give a specific width and height do the div. No prob. I also accept js if I must to. I'm just wondering, from the MAZE of those examples, can I have a simple one.

The div as width of 600 and height of 500.

Thanks in advance, MEM


Easy peasy. Say your div has id="myDiv". Then...

#myDiv {
    position: absolute;
    top: 50%;
    margin-top: -250px; // half the height of #myDiv
}

...will center the div inside of its offset parent.

Half-scale demo (so that you don't have to make your window really big to properly see the effect)


Handle this problem simply, you can use padding to the div.

0

精彩评论

暂无评论...
验证码 换一张
取 消