最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to center a relative div horizontally using jquery on window resize? - Stack Overflow

programmeradmin0浏览0评论

i am initially centering div horizontally using jquery but when the window is resized it looks bad so what i want to do is keep it centered using jquery after the window is resized

is there a way to help?

EDIT

guys i have successfully made other elements centered but i am having another issue now :(

please check this /

and resize the window, you will see that the content doesn't get positioned correctly, i am trying to fix this for hours but can't find the solution please help with that

EDIT solved! it was plex and my code is very specific so posting it here won't help :) and although i used jquery to center it but if we use the css thing then FutureKode's answer is best suited for me :)

i am initially centering div horizontally using jquery but when the window is resized it looks bad so what i want to do is keep it centered using jquery after the window is resized

is there a way to help?

EDIT

guys i have successfully made other elements centered but i am having another issue now :(

please check this http://hrmanagementbradford./gallery/

and resize the window, you will see that the content doesn't get positioned correctly, i am trying to fix this for hours but can't find the solution please help with that

EDIT solved! it was plex and my code is very specific so posting it here won't help :) and although i used jquery to center it but if we use the css thing then FutureKode's answer is best suited for me :)

Share Improve this question edited Apr 15, 2011 at 21:39 shaheer asked Apr 15, 2011 at 15:15 shaheershaheer 3762 gold badges6 silver badges16 bronze badges 3
  • 3 Why don't you center the DIV with CSS (width:XXXpx; margin:auto;)? – Šime Vidas Commented Apr 15, 2011 at 15:16
  • 1 Could you post your code? I'm not sure why you're doing this in jQuery rather than CSS... – Town Commented Apr 15, 2011 at 15:16
  • you can see the code at the link i posted – shaheer Commented Apr 15, 2011 at 19:09
Add a ment  | 

4 Answers 4

Reset to default 3

Why are you using jquery to center horizontally when css can do it one line and it will stay in the center when the browser is resized:

div {
margin:0 auto;
width:800px
}

You can make it dead-centered like this:

$('#elementID').css({
  position:'absolute',
  top:'50%',
  left:'50%',
  width:'600px',                 // adjust width
  height:'300px',                // adjust height
  zIndex:1000,
  marginTop:'-150px'             // half of height
  marginLeft:'-300px'            // half of width
});

Note that element will appear at the center but with scrolling it won't move. If you want to make it appear at center, you need to set position to fixed instead. However, this won't work in IE6. So decision is yours :)


You can also create quick simple jQuery plugin:

(function($){
    $.fn.centerIt = function(settings){

        var opts = $.extend({}, $.fn.centerIt.defaults, settings);

        return this.each(function(settings){
          var options = $.extend({}, opts, $(this).data());
          var $this = $(this);

          $this.css({
            position:options.position,
            top:'50%',
            left:'50%',
            width:options.width,                 // adjust width
            height:options.height,               // adjust height
            zIndex:1000,
            marginTop:parseInt((options.height / 2), 10) + 'px'  // half of height
            marginLeft:parseInt((options.width / 2), 10) + 'px'  // half of height
          });

        });
    }

    // plugin defaults - added as a property on our plugin function
    $.fn.centerIt.defaults = {
      width: '600px',
      height: '600px',
      position:'absolute'
    }

})(jQuery);

And later use it like:

$('#elementId').centerIt({width:'400px', height:'200px'});

To center it when window is resized, you would use resize event just in case it does not center like this:

$(window).resize(function(){
  $('#elementId').centerIt({width:'400px', height:'200px'});
});

You can use

margin: 0 auto;

to centre a block element horizontally in CSS.

Like so:

div 
{
   width: 400px;
   margin: 0 auto;
}

do this:

$(window).resize(function(){

   // reposition div again here

})
发布评论

评论列表(0)

  1. 暂无评论