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 7541013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:47:01+00:00 2026-05-30T07:47:01+00:00

How to add/subtract in css property? #box { position: absolute; top: 50% ; left:

  • 0

How to add/subtract in css property?

#box {
  position: absolute;
  top: 50% ;
  left: calc(50% - 286px);
  margin-left: -75px /* half of the width */
  background-color: red; /* or something more useful */
  z-index:100;
}

I am simply try to add/subtract the contents value in the actual top and left percentage

top: 50% - contents_height_in_number_with_px

I want to do this so that my main contents is automatically centered
Note:i already search on googled and already try calc() not found any solution

  • 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-30T07:47:03+00:00Added an answer on May 30, 2026 at 7:47 am

    That css (calc()) is not supported in all browsers. You need to know the height of the current viewport and the height of div#box. Here’s a way to determine both, i.e. to center an element. You can ditch top:50% ; left:calc(50% - 286px);margin-left:-75px in the css now.

    // get viewport dimensions
    function viewport(){
        var  innerw = window.innerWidth
            ,root   = innerw  ? window : (document.body || document.documentElement)
            ,body   = (document.body || document.documentElement)
            ,which  = innerw ? 'inner' : 'client';
        return {  width : root[ which+'Width' ] 
                 ,height : root[ which+'Height' ]
                 ,scrollTop: body.scrollTop 
                 ,scrollLeft: body.scrollLeft };
    }
    
    // center an element (uses viewport function)
    function center(el){
      var dims = viewport()
         ,l = Math.floor( (0.5*dims.width)-(0.5*el.offsetWidth) )
         ,h = Math.floor( (0.5*dims.height)-(0.5*el.offsetHeight) );
      el.style.left = l+'px';
      el.style.top  = h+'px';
      return true;
    }
    // usage:
    center(document.getElementById('box'));
    

    See also this jsfiddle

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

Sidebar

Related Questions

How do I add and subtract 16 bit floating point half precision numbers? Say
My graphics are looking blurry unless I add or subtract a half pixel to
Given the following flags, [Flags] public enum Operations { add = 1, subtract =
I add my page top link button like this: <a name=top></a> . . .
You can add and subtract 1 from my cell.textLabel.text . I am adding 1
How do I add/subtract two time objects. I have two time objects (arrival and
I would like an alogrithm that would use only shift, add or subtract operations
PHP's DateTime class has the two methods add() and sub() to add or subtract
Hi so I was wondering what the best way to add or subtract from
Today, while I was trying to write code to just add and subtract the

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.