Ver Mensaje Individual
  #3 (permalink)  
Antiguo 18/01/2008, 20:21
Avatar de Panino5001
Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años, 8 meses
Puntos: 834
Re: centrar div horizontal y verticalmente

Esto debería moverse al foro de css, a menos que quieras hacerlo con javascript:
Código PHP:
<div style="border:1px solid #000;position:absolute;width:410px; height:145px;top:50%;left:50%;margin-top:-72px;margin-left:-205px;">algo</div
Con top y left posicionamos el vértice superior izquierdo justo en el centro y luego colocamos márgenes negativos iguales a la mitad de las dimensiones para terminar de centrar.
Con javascript, sería así:
Código PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<
title>test</title>
<
script>
function 
getWindowData(){
    var 
widthViewport,heightViewport,xScroll,yScroll,widthTotal,heightTotal;
    if (
typeof window.innerWidth != 'undefined'){
        
widthViewportwindow.innerWidth;
        
heightViewportwindow.innerHeight;
    }else if(
typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0){
        
widthViewport=document.documentElement.clientWidth;
        
heightViewport=document.documentElement.clientHeight;
    }else{
        
widthViewportdocument.getElementsByTagName('body')[0].clientWidth;
        
heightViewport=document.getElementsByTagName('body')[0].clientHeight;
    }
    
xScroll=self.pageXOffset || (document.documentElement.scrollLeft+document.body.scrollLeft);
    
yScroll=self.pageYOffset || (document.documentElement.scrollTop+document.body.scrollTop);
    
widthTotal=Math.max(document.documentElement.scrollWidth,document.body.scrollWidth,widthViewport);
    
heightTotal=Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,heightViewport);
    return [
widthViewport,heightViewport,xScroll,yScroll,widthTotal,heightTotal];
}
function $(
id){
return 
document.getElementById(id);
}
window.onload=window.onresize=window.onscroll=function(){
var 
data=getWindowData();
$(
'pp').style.left=data[0]/2+data[2]-parseInt($('pp').style.width)/2+'px';
$(
'pp').style.top=data[1]/2+data[3]-parseInt($('pp').style.height)/2+'px';
}
</script>

</head>

<body><div id="pp" style="border:1px solid #000;position:absolute;width:410px; height:145px;">algo</div>  
</body>
</html> 

Última edición por Panino5001; 18/01/2008 a las 20:42