Centering a div vertically & horizontally using jQuery

后端 未结 9 1328
轮回少年
轮回少年 2020-12-02 08:44

I am using this script to center my div horizontally and vertically.

When the page loads the div gets centered vertically, not horizontally until I resize the brows

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-02 09:19

    I have recently been trying to place a box in the middle of a browser window. I created the code below. The jQuery script is quite long because I took into account all paddings and margins of html and body tags, if someone have set them. If one does not care about the backgrounds, then only winwidth, winheight, toppx, leftpx, and "#container" have to be set. The rest may be deleted.

    
    
    
        Test of centre of container
        
        
    
    
        
        

    Example text





    EDIT: I found much better solution for this on CSS Tricks website and it uses only CSS :). The code's below:

    
    
    
        
            Test centring!
        
        
    
    
        

    Example text
    Example Text

提交回复
热议问题