Mahendra
Mahendra

Reputation: 153

How to set div exactly in the center of screen whose size is changing dynamically

I want to place the div exactly in the center whose size is changing dynamically.

<div id="containall">
  <div style="display:none" align="center" id="mainDiv">
    <div class="mainDiv" id="paste"></div>
    <div class="midDiv" id="popup"><a href="#" onClick="hideDiv();" style="font-size:20px;float:right;margin:5px 5px 0 0px;padding:5px 0px 5px 9px;background:#000;-webkit-border-radius: 16px;-moz-border-radius:16px;border-radius:16px;padding-right:10px;text-decoration:none">X</a>
        <img id="bearimage2" style="visibility:hidden;">
        <canvas id="tools_sketch" width="200" height="200"></canvas>
    </div>
 </div>
</div>

I am changing the size of canvas dynamically according to image it holds. the css for POPUP div is like

#popup {
    width:600px;
    height:400px;
    background:url(/BugClipper/bccss/bgtrans_bc.png);
    position:absolute;
    left:400px;
    -webkit-border-radius: 9px;
    -moz-border-radius: 9px;
    border-radius: 9px;
    margin:-600px 0 0 0;
    border:2px solid #333;
}

NOw when the popup size is changed to hold canvas, it goes to the right side of screen and i have to scroll to view full image on canvas.

Please guide me how to set css so that it always display on middle of screen

Upvotes: 2

Views: 3169

Answers (3)

Barnee
Barnee

Reputation: 3399

Try this:

#popup {
 top: 50%;
 left: 50%;
 width: 600px;
 height: 400px;
 margin: -200px 0 0 -300px;
 position: fixed;
}

Fiddle

Upvotes: 1

Rushikesh jogle
Rushikesh jogle

Reputation: 591

try this css

#popup {
width:50%;
height:400px;
background:url(/BugClipper/bccss/bgtrans_bc.png);

left:400px;
-webkit-border-radius: 9px;
-moz-border-radius: 9px;
border-radius: 9px;
margin:0 auto;
padding:0;
border:2px solid red;
 }

Upvotes: 1

Santosh
Santosh

Reputation: 12353

You can apply this CSS to the popup div:

#popup {
    width: 50%;
    margin: 0 auto;
}

Upvotes: 1

Related Questions