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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:25:41+00:00 2026-05-30T18:25:41+00:00

I have this code: <div class=parent> <div class=child>something</div> </div> The parent should be 200px

  • 0

I have this code:

<div class="parent">
    <div class="child">something</div>
</div>

The parent should be 200px width and height, and the child 100px width and height. The parent is also set to position:absolute. Is it possible to horizontaly and verticaly center the child relative to the parent and how?

The result: If you set black background to the parent and white to the child, the whole thing should look like there is a white square with a large black border.

Static margins and paddings are out of the question since the size of the child will change dynamically.


That might not be the proper solution, but what I am actually trying to achieve is a black square which I can change the size of with jQuery and it will stay at the same absolute position, like it is being scaled from the center, not from the top left corner.

I thought I could set the position of a parent and let the child center horizontaly and verticaly auto while changing it’s size, so I would get the proper ‘scale’-like effect.

  • 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-30T18:25:42+00:00Added an answer on May 30, 2026 at 6:25 pm

    I used jQuery UI position API to position the child div at center and then on .animate based on border-width you should change the top and left accordingly.

    Try out the DEMO to understand the below code DEMO

    After Animation:

    enter image description here

    HTML:

    <div class="parent">
        <div class="child">something</div>
    </div>
    
    <button>Animate</button>
    

    JS:

    $(document).ready (function () {
        positionChild();
    });
    
    
    function positionChild() {
        $( ".child" ).position({
                    of: $( ".parent" ),
                    my: "center center",
                    at: "center center"
        });
    }
    
    $('button').click (function () {
        $('.child').animate({'border-width': 47, top: '-=46', left: '-=46'}, 1000);
    });
    

    CSS:

    .parent { 
        position: absolute;  
        width: 200px; 
        height: 200px; 
        background-color: black; 
        top: 100px; 
        left: 100px; 
    }
    
    .child { 
        width: 100px; 
        height: 100px; 
        background-color: #c2c2c2; 
        border: 1px solid blue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this html-code: <div class=im> <div style=height:220px>HELLO WORLD! <a class=close>X</a> <a class=open style=display:none;>V</a>
I have following code: <div class='parent'> <div class='left-child'></div> <div class=right-child></div> </div> What I want
I have this HTML code: <div class='com_box'> <div class='com_box_c'> <div class='com_box_info'> <a id='quote'>quote</a> </div>
I have a master page in C#, where I have this code: <div style=width:
So my list looks something like this: <div=list> <ul> <li class=page item>Parent 1 <ul>
I have this in my HTML file: <td> <div class=something>someText</div> <div class=something>otherText</div> </td> Is
I have this code: $(div[id^='intCell']).mouseover(function() { $(this).css({ border:,1px solid #ff097c}); }).mouseout(function() { $(this).css({border:,1px solid
I have this code that changes the opacity of the div on hover. $(#navigationcontainer).fadeTo(slow,0.6);
I have this code snippet: <div id=div1> </div> <div id=div2> <h3>This is the content</h3>
I have this code: var $msg = jQuery('<div></div>') .hide() .appendTo(document.body) ; if ($msg.is(:hidden)) {

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.