Comunidad de diseño web y desarrollo en internet online

Centrar divs internos al ancho de un div contenedor...

Citar            
MensajeEscrito el 16 Ago 2011 04:00 pm
Puede que en el titulo se lea facil, yo tambien se centrar algunos divs dentro de otros con el margin:auto, pero se me ha complicado la cosa...


Este es mi codigo actual...

Código HTML :

<div style="margin:auto; min-width:200px;"> <!-- AREA DE CALIFICACIONES -->
    
    
   <div style="width:125px; height:50px; float:left; border:solid 1px #666; clear:left;"><br /><center>Asignatura</center></div>
       <div style="width:121px; height:50px; float:left; margin:0px;">
           <div style="width:120px; height:25px; float:left; border:solid 1px #666; border-left:none;"><center>Primer Parcial</center></div>

           <div style="width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;"><center>Faltas</center></div>
           <div style="width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;"><center>Calif.</center></div>
   </div>
       <div style="width:121px; height:50px; float:left; margin:0px;">
           <div style="width:120px; height:25px; float:left; border:solid 1px #666; border-left:none;"><center>Segundo Parcial</center></div>
           <div style="width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;"><center>Faltas</center></div>
           <div style="width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;"><center>Calif.</center></div>

   </div>
       
   <div style="width:120px; height:50px; float:left; border:solid 1px #666; margin-left:3px; "><br /><center>Total Faltas</center></div>
    <div style="width:120px; height:50px; float:left; border:solid 1px #666; border-left:none;"><br /><center>Promedio General</center></div>
   
    <div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 1</div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>2</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>

               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>
               </div>
            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>2</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center></div><div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 2</div><div style='width:121px; height:24px; float:left; margin:0px;'>

               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>
               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center>
               </div>

            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>0</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>9</center></div><div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 3</div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>
               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>

               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>
               </div>
            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>0</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center></div><div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 4</div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>

               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>
               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center>
               </div>
            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>0</center></div>

   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>9</center></div><div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 5</div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center>
               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>

               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>7</center>
               </div>
            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>0</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>7.5</center></div><div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 6</div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center>

               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center>
               </div>
            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>0</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center></div>    <div style="clear:both; height:10px;"></div>

   <div style='width:125px; height:24px; float:left; border:solid 1px #666; clear:left;'><center>Promedio&nbsp;Mensual</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; '><center>9.3</center></div><div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; '><center>8.5</center></div>    
    <div style="clear:both; height:2px;"></div>
    <div style='width:125px; height:24px; float:left; border:solid 1px #666; clear:left;'><center>Conducta</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; '><center>Muy Buena</center></div><div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; '><center>Muy Buena</center></div>    
    
    </div> <!-- FIN AREA DE CALIFICACIONES -->


Y me da por resultado esto:


Y yo quiero que me quede asi:


El asunto, como tal vez se imaginen, es que las calificaciones se recuperan desde una base de datos y duplico los divs con un ciclo en php, asi que mientras mas parciales haya mas ancho va a ser el contenido interno del div que contiene las calificaciones y la idea es que se vaya centrando el contenido automaticamente...

Espero puedan ayudarme, de antemano muchas gracias.

Por holler

65 de clabLevel



Genero:Masculino  

firefox
Citar            
MensajeEscrito el 16 Ago 2011 04:14 pm
Y por qué no usas display: table; display: table-row y display: table-cell?. O directamente <table>, porque lo que estás haciendo ES una tabla.

Por DriverOp

Claber

2510 de clabLevel



 

opera
Citar            
MensajeEscrito el 16 Ago 2011 04:28 pm
Al principio lo intente hacer todo con un <table> pero no podia hacer que los ciclos pusieran las celdas donde debian hacerlo, asi que lo omiti y me fui directo por los <divs>, sin embargo no me acorde de los display que mencionaste, porque no sabia que existian, por consiguiente, no se usarlos, jaja. Si, asi de ridiculo como suena, podrias explicarme como funcionan, buscare por mientras en w3school, gracias por el dato.

Por holler

65 de clabLevel



Genero:Masculino  

firefox
Citar            
MensajeEscrito el 16 Ago 2011 05:41 pm
No sé qué complicaciones encontraste con <table>. A mi me resulta más fácil mostrar datos tabulados a partir de una base de datos con <table> que con <div>.
display: table; no hace más que hacer que un <div> (o cualquier elemento tipo block en realidad) se comporte como si fuera un <table>, y el resto se corresponde con <tr> y <td>.
Sospecho que la dificultad que tienes se debe a que "abusas" de los floats. Es mi parecer.
!Ah!, y reemplaza <center> por text-align: center; por favor ;)

Por DriverOp

Claber

2510 de clabLevel



 

opera
Citar            
MensajeEscrito el 17 Ago 2011 07:39 pm

holler escribió:

Puede que en el titulo se lea facil, yo tambien se centrar algunos divs dentro de otros con el margin:auto, pero se me ha complicado la cosa...


Este es mi codigo actual...

Código HTML :

<div style="margin:auto; min-width:200px;"> <!-- AREA DE CALIFICACIONES -->
    
    
   <div style="width:125px; height:50px; float:left; border:solid 1px #666; clear:left;"><br /><center>Asignatura</center></div>
       <div style="width:121px; height:50px; float:left; margin:0px;">
           <div style="width:120px; height:25px; float:left; border:solid 1px #666; border-left:none;"><center>Primer Parcial</center></div>

           <div style="width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;"><center>Faltas</center></div>
           <div style="width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;"><center>Calif.</center></div>
   </div>
       <div style="width:121px; height:50px; float:left; margin:0px;">
           <div style="width:120px; height:25px; float:left; border:solid 1px #666; border-left:none;"><center>Segundo Parcial</center></div>
           <div style="width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;"><center>Faltas</center></div>
           <div style="width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;"><center>Calif.</center></div>

   </div>
       
   <div style="width:120px; height:50px; float:left; border:solid 1px #666; margin-left:3px; "><br /><center>Total Faltas</center></div>
    <div style="width:120px; height:50px; float:left; border:solid 1px #666; border-left:none;"><br /><center>Promedio General</center></div>
   
    <div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 1</div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>2</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>

               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>
               </div>
            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>2</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center></div><div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 2</div><div style='width:121px; height:24px; float:left; margin:0px;'>

               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>
               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center>
               </div>

            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>0</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>9</center></div><div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 3</div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>
               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>

               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>
               </div>
            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>0</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center></div><div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 4</div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>

               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>10</center>
               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center>
               </div>
            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>0</center></div>

   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>9</center></div><div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 5</div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center>
               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>

               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>7</center>
               </div>
            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>0</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>7.5</center></div><div style='width:125px; height:24px; float:left; border:solid 1px #666; border-top:none; clear:left;'>&nbsp;&nbsp;Materia 6</div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center>

               </div>
            </div><div style='width:121px; height:24px; float:left; margin:0px;'>
               <div style='width:60px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>0</center></div>
               <div style='width:59px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center>
               </div>
            </div><div style='width:120px; height:24px; float:left; border:solid 1px #666;border-top:none; margin-left:3px; '><center>0</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; border-top:none;'><center>8</center></div>    <div style="clear:both; height:10px;"></div>

   <div style='width:125px; height:24px; float:left; border:solid 1px #666; clear:left;'><center>Promedio&nbsp;Mensual</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; '><center>9.3</center></div><div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; '><center>8.5</center></div>    
    <div style="clear:both; height:2px;"></div>
    <div style='width:125px; height:24px; float:left; border:solid 1px #666; clear:left;'><center>Conducta</center></div>
   <div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; '><center>Muy Buena</center></div><div style='width:120px; height:24px; float:left; border:solid 1px #666; border-left:none; '><center>Muy Buena</center></div>    
    
    </div> <!-- FIN AREA DE CALIFICACIONES -->


Y me da por resultado esto:


Y yo quiero que me quede asi:


El asunto, como tal vez se imaginen, es que las calificaciones se recuperan desde una base de datos y duplico los divs con un ciclo en php, asi que mientras mas parciales haya mas ancho va a ser el contenido interno del div que contiene las calificaciones y la idea es que se vaya centrando el contenido automaticamente...

Espero puedan ayudarme, de antemano muchas gracias.


Es un chorro de codigo innecesario, porque no mejor creas los estilos en un css externo y lo jalas desde alla en lugar de estarle dando la instruccion a cada div?

por ejemplo en lugar de repetir a cada rato:
<div style='width:121px; height:24px; float:left; margin:0px;'>

porque no le pones algo asi:
<div class="box1">

y en el css:
.box {width:121px;height:24px;float:left;margin:0px;}


saludos

Por samSSL

19 de clabLevel



 

Seguridad Web

firefox
Citar            
MensajeEscrito el 18 Ago 2011 02:07 pm
porque en realidad solo lo escribi unas pocas veces, un ciclo en php hace que se imprima todas las veces que necesito

Por holler

65 de clabLevel



Genero:Masculino  

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.