{"id":547,"date":"2012-04-14T21:23:01","date_gmt":"2012-04-14T21:23:01","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=547"},"modified":"2012-04-14T21:23:01","modified_gmt":"2012-04-14T21:23:01","slug":"jquery-center-div","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-center-div\/","title":{"rendered":"jQuery Center Div Example"},"content":{"rendered":"<p>Love jQuery? Need a quick way to center div in window? In this article I am going to show you<strong>\u00a0<em>how to center div using jQuery<\/em><\/strong>.<!--more--><\/p>\n<h2>Example to Center Div using jQuery<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=547\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p>In order to use this, simply apply the .center() method to your element selector and that will center the div.<\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;style type=\"text\/css\"&gt;\n#my_div {\n\tbackground-color: #DFF2FF;\n\tborder: 1px solid #06F;\n\theight: 200px;\n\twidth: 500px;\n}\n.my_div_2 {\n\tbackground-color: #FFCC66;\n\tborder: 1px solid #F90;\n\theight: 100px;\n\twidth: 300px;\n}\n&lt;\/style&gt;\n\n&lt;div id=\"my_div\"&gt;This is my_div&lt;\/div&gt;\n&lt;div class=\"my_div_2\"&gt;This is my_div_2&lt;\/div&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\njQuery.fn.center = function () {\nthis.css(\"position\",\"absolute\");\nthis.css(\"top\", ( $(window).height() - this.height() ) \/ 2+$(window).scrollTop() + \"px\");\nthis.css(\"left\", ( $(window).width() - this.width() ) \/ 2+$(window).scrollLeft() + \"px\");\nreturn this;\n}\n\n\/\/Call the \"center\" function on your desired div in the following way.\n\/\/Simply replace #my_div with the ID or the class of the div that you want to center.\n$('#my_div').center();\n$('.my_div_2').center();\n&lt;\/script&gt;<\/pre>\n<p>When you run the above code, you will notice that the divs are centered in the window when the page loads.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=547\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5>That&#8217;s it!<\/h5>\n<p>Do you know of any other ways to use <em><strong>jQuery to center div<\/strong><\/em>? Please share your thoughts below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Love jQuery? Need a quick way to center div in window? In this article I am going to show you\u00a0how to center div using jQuery.<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[19,20,6,13],"tags":[40,41],"class_list":["post-547","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery","category-jquery-tips-and-tricks","category-snippets","category-tips-and-tricks","tag-jquery-2","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-8P","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/547","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=547"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}