Last active
          December 16, 2015 13:59 
        
      - 
      
 - 
        
Save dustintheweb/5445872 to your computer and use it in GitHub Desktop.  
    jQuery - Dynamically center elements
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // >> jQuery - Dynamically center elements >>>>>>>>>>>>>>>> | |
| var el_1 = $('.element-1'), | |
| el_2 = $('.element-2'), | |
| el_3 = $('.element-3'), | |
| el_4 = $('.element-4'); | |
| // -- X&Y --------------- | |
| function XYmid(){ | |
| el_1.css({'marginLeft' : el_1.outerWidth()/-2, 'marginTop' : el_1.outerHeight()/-2}); | |
| el_2.css({'marginLeft' : el_2.outerWidth()/-2, 'marginTop' : el_2.outerHeight()/-2}); | |
| } | |
| // -- X --------------- | |
| function Xmid(){ | |
| el_3.css({'marginLeft' : el_3.outerWidth()/-2}); | |
| } | |
| // -- Y --------------- | |
| function Ymid(){ | |
| el_4.css({'marginTop' : el_4.outerHeight()/-2}); | |
| } | |
| XYmid(); | |
| Xmid(); | |
| Ymid(); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment