0

I have a div that has not a preset width in css, I'm trying to center in on screen with JQuery, it works in Chrome, Firefox and IE8+, but does not work in IE7. Why?

css:

.productBoxWrapper{
    height:210px;
    clear:both;
    background-color:blue;
}

JQuery:

$('#mydiv').css("position", "absolute");
this.css("left", ($(window).width() - $("#mydiv").width()) / 2 + "px");

JSFiddle: http://jsfiddle.net/tygcz/3/

9
  • 1
    IE7 is old, don't support it. Commented Feb 25, 2013 at 13:22
  • 1
    Do you have an containing divs with padding/margin? you may need to use .outerWidth() instead to take account of padding and borders. Commented Feb 25, 2013 at 13:23
  • Agree with @s.lenders. Welcome to 2013. :-/ Commented Feb 25, 2013 at 13:24
  • 1
    do you really need to support IE7? This is just one of hundred of problems and wasted time you will have when supporting IE7. Commented Feb 25, 2013 at 13:24
  • 1
    Google dropped support for IE7 last year so when developing a new website don't bother making it work. IE7 is old, it updates automaticly to 8 on all windows. less then 2% of the users still use IE7. Not interesting to waste alot of time on Commented Feb 25, 2013 at 13:30

1 Answer 1

1

Try to center it like this:

Css:

#myDiv{
     position: absolute; top: 50%; left: 50%;
}

Script:

$('#myDiv').css('marginLeft', - ( parseInt( $('#myDiv').width() ) / 2) + 'px' );
$('#myDiv').css('marginTop', - ( parseInt( $('#myDiv').height() ) / 2) + 'px' );

I'm not sure weather parseInt is really needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.