As already mentioned in the comments, just calculate the offset
:
var $mc = $('#main-content'); // Seleciona o elemento div#main-content
var $wd = $('#windows'); // Seleciona o elemento div#windows
// Calcula o offset da esquerda
// posição esquerda + metade da largura do content - metade da largura do windows
var left = $mc.offset().left + ($mc.width() / 2) - ($wd.width() / 2);
// Calcula o offset do topo
// posição topo + metade da altura do content - metade da altura do windows
var top = $mc.offset().top + ($mc.height() / 2) - ($wd.height() / 2);
// Atribui posições
$('#windows').css({'left': left, 'top': top});
In order for the element to be positioned correctly, it must be in the "root" of the document, or child of the body
and its position
must be absolute
, similar to this:
#windows { /* windows deve ser filho do body */
position: absolute; /* <-- Importante */
background: red;
width: 708px;
height: 538px;
}
Demo:
//<div id="windows"></div>
jQuery(document).ready(function($){
$(document.body).append( $('<div />').attr('id', 'windows') );
var $mc = $('#main-content');
var $wd = $('#windows');
var left = $mc.offset().left + ($mc.width() / 2) - ($wd.width() / 2);
var top = $mc.offset().top + ($mc.height() / 2) - ($wd.height() / 2);
$('#windows').css({'left': left, 'top': top});
});
#page-wrap {
position: relative !important;
margin-top: 0px;
margin-left: 170px;
width: 768px;
height: 593px;
border-radius: 3px;
border: 1px solid #8e8e97;
background-color: rgb(247, 247, 247);
}
#main-content {
position: absolute;
width: 100%;
height: 100%;
background: yellow;
}
#windows {
position: absolute;
background: red;
width: 708px;
height: 538px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="page-wrap">
<div id="main-content">
<!--
Alinhar aqui no centro a div#windows
-->
</div>
</div>
And why the div
#windows
this is from the div#main-content
?– user3603
it is created dynamically
– Jesus
And what’s the problem? It can be created dynamically within
#main-content
, or you need her to stay out?– user3603
outside, but in the center of the div
#main-content
– Jesus
So your title is wrong, you said, "div parente", for the fact that
#windows
being out of it, no longer related– user3603
I’m sorry but this kind of confusing, for me, you want me to div
#windows
stay out of#main-content
but div#windows
be in the same alignment as#main-content
? That’s it?– user3603
That’s right @Gerep!!!
– Jesus
Then fix your title again and refine your question, leaving these details
importantes
clear in your doubt, only then will you get help =)– user3603
so? http://jsfiddle.net/4fu5533f/
– user3603
although it is possible to do this by calculating the top and left of the div #window using the offset of #page-wrap and #main-content as a parameter, I see no reason not to put #window inside #main-content and use a position:Absolute with margin auto.
– Tobias Mesquita
@Tobymosque, I totally agree but he said he needs the div
#windows
out of– user3603
@Gerep in his example is not at the center of
#main-content
the div#windows
– Jesus
@Jesus, you want it to line up in the center and on the div
#main-content
?– user3603
This... this @Gerep =)
– Jesus
@Jesus, in this case I think you’ll need to use Javascript to align correctly since CSS does not work with dynamic values.
– user3603
OK @Gerep, let’s see if someone from javascript is moved and helps me =/
– Jesus