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

  • SEARCH
  • Home
  • 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 8739753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:59:56+00:00 2026-06-13T10:59:56+00:00

I am trying to set margin of the id:main dynamically by obtaining viewport width.

  • 0

I am trying to set margin of the id:main dynamically by obtaining viewport width. I have numerous blocks of same width in the page. So, i want to fit as many blocks as possible and centre them by dynamically calculating the remaining width. the block is of size 240px, 20px of padding

I have tried two codes, i’m stuck at this for a while.

var width=$(window).width()
var stickies=Math.floor(width/240);  //* calculate how many can fit in a row
var mrg=(width-(stickies*240)-20);//*calculating remaining width 
var mrg=(width-(stickies*240)-20)/2;
var el1=$('#main');
var el2=$('#main');
el1.css('margin-left',mrg+'px');
el2.css('margin-right',mrg+'px'); 



var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')     [0],x=w.innerWidth||e.clientWidth||g.clientWidth;
var stickies=Math.floor(var x/240);
var mrg=(var x-(var stickies*240)-20)/2;
var el1=$('#main');
var el2=$('#main');
el1.css('margin-left',mrg+'px');
el2.css('margin-right',mrg+'px');
  • 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-06-13T10:59:57+00:00Added an answer on June 13, 2026 at 10:59 am

    Short Version

    you should need something along those lines

    var width=$(window).width();
    var stickies=Math.floor(width/240);  //* calculate how many can fit in a row
    var mrg=(width-(stickies*240)-20)/2; //*calculating remaining width 
    $('#main').css({'margin-left': mrg+'px', 'margin-right': mrg+'px'});
    

    Long Version

    your code needs a lot of fixing up.

    var el1=$(#main);
    

    needs to change to

    var el1=$('#main');
    

    because at the moment u are not selecting anything. you need to place your selector in quotes.


    also i dont understand why u are using 2 variables for the same element. you could do something like

    $('#main').css({'margin-left': mrg+'px', 'margin-right': mrg+'px'});
    

    or something along those lines.


    Finally,

    var mrg=(width-(stickies*240)-20);//*calculating remaining width 
    var mrg=(width-(stickies*240)-20)/2;
    

    The 2nd line will overwrite the 1st line. Is this a typo or are you trying to do something here?

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

Sidebar

Related Questions

I have a web page, and I am trying to set up a login
im trying to set the height of two divs on my page, because my
I want to have a web page that contains 3 parts: A header at
I'm trying to set the default focus on an input box when the page
I'm trying to set the width of table column to a minimum value. The
I am trying to set the Margin of my Ellipse through code. How can
I've looked: How to set margin with jquery? This is what I am trying:
I trying to set up my nav so I have my list items and
I'm trying to set up a few add/removeClass functions, of the same class, depending
Trying to set up a menu so that I have border-left on each menu

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.