Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6032131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:17:19+00:00 2026-05-23T05:17:19+00:00

Need to center an object whose width changes depending on the browser window. Usually

  • 0

Need to center an object whose width changes depending on the browser window. Usually I’d use css with top and left positions set to 50% with a negative margin equal to half the width and height, but obviously that wouldn’t accomodate this need.

I thought my jQuery function would work… but no bueno. am i missing something in CSS?

Thanks!

http://jsfiddle.net/danielredwood/EbkLg/2/

Here’s the CSS

#t {
        width:25%;
        height:25%; 
        background:red;
    }

JavaScript

$(window).resize(function(){
     resizenow();
});
function resizenow() {
    var browserwidth = $(window).width();
    var browserheight = $(window).height();
    $('#t').css('left', (browserwidth - $(this).width())/2).css('top', (browserheight - $(this).height())/2);
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T05:17:20+00:00Added an answer on May 23, 2026 at 5:17 am

    In your JSFiddle that you linked to you use $(this).width():

    $('#t').css('left', ((browserwidth - $(this).width())/2))
    .css('top', ((browserheight - $(this).height())/2));
    

    But when the function resizenow() is called, this == window. If you replace $(this) with $("#t") it works fine, as this JSFiddle shows: http://jsfiddle.net/jackfranklin/F2tR3/1/

    Within the css() function of jQuery, the value of this is not set to the element whose style(s) you are changing, hence why your original code didn’t work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to center a CSS menu that has an unknown width. I found
I have a blog on wordpress and I need to center an object. What
With the following function I center an object in the window if the user
I need to access the X,Y values of a vertex object after Ive rotated
I have a simple silverlight application and i need to add the web browser
Has anyone got a bit of code I can use to change the center
I need to select all div s which contain object as their direct child.
I need to post a notification using postNotificationName:object:userInfo: method, and I'm passing a custom
I need to plot the transformation of a 3d object against time. I have
I am trying to calculate half of the width of an object using Javascript.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.