Foros del Web » Programando para Internet » Javascript »

[SOLUCIONADO] Meter css segun resolucion y que valide en W3C

Estas en el tema de Meter css segun resolucion y que valide en W3C en el foro de Javascript en Foros del Web. Buenos días Quiero que una web se centre verticalmente si la resolución de pantalla es mayor de 1024, por lo que en el head he ...
  #1 (permalink)  
Antiguo 10/02/2013, 02:51
 
Fecha de Ingreso: marzo-2009
Mensajes: 395
Antigüedad: 15 años, 1 mes
Puntos: 11
Meter css segun resolucion y que valide en W3C

Buenos días

Quiero que una web se centre verticalmente si la resolución de pantalla es mayor de 1024, por lo que en el head he incluido el siguiente código:
Código HTML:
<head>

<script type="text/javascript">

if(screen.width > 1024){
document.writeln("<link href='../../css/es/1152.css' rel='stylesheet' type='text/css' media='screen' />");

}
</script>
</head> 
Pero al pasar el validador W3C me dice que hay un error:

Código:
document type does not allow element "link" here
…f='../../css/es/1152.css' rel='stylesheet' type='text/css' media='screen' />");
✉
The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).
Que viene a decir que el código lo está metiendo en el body y no en el head.

Como haríais vosotros para que cogiera el css en función de la resolución sin usar CSS3 (porque el explorer 8 no lo soporta )
  #2 (permalink)  
Antiguo 10/02/2013, 04:02
Avatar de ipraetoriux  
Fecha de Ingreso: abril-2010
Ubicación: ipraetoriux.com
Mensajes: 1.125
Antigüedad: 14 años
Puntos: 155
Respuesta: Meter css segun resolucion y que valide en W3C

...pero para que te complicas la vida si podes usar condicionales de HTML

Código HTML:
Ver original
  1. <!--[if lt IE 9]>
  2. <link rel="stylesheet" type="text/css"  href="hojadeestilo.css"/>
  3. <![endif]-->

..luego tenes otra alternativa, para no estar metiendo muchas hojas de estilo...podes usar esto

Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <!--[if IE 8]>         <html class="ie8"> <![endif]-->
  3. <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
  4.         <meta charset="utf-8">
  5. </head>
  6. </body>
  7. </html>

.. acá también usas condicionales de HTML, peroooo, no es necesario agregar ninguna hoja de estilo extra. Lo único que tenes que hacer es crear una clase general "ie8" en la etiqueta HTML, y dentro de tu CSS, crear estilos que desciendan de esta clase.

Código CSS:
Ver original
  1. .ie8 #contenedor{
  2.    width:1024px;
  3.    margin: 0 auto;
  4. }

...entonces cuando se este usando IE8, el navegador va a renderizar la web con la clase para IE8 y todas sus clases desendientes, de esta forma podes crear estilos para distintos navegadores sin sobrecarga tu web con hojas de estilo, y tener todo ordenado dentro de una sola...
  #3 (permalink)  
Antiguo 10/02/2013, 10:47
Avatar de emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: Meter css segun resolucion y que valide en W3C

Cita:
Iniciado por Gothgauss Ver Mensaje
Buenos días

Quiero que una web se centre verticalmente si la resolución de pantalla es mayor de 1024, por lo que en el head he incluido el siguiente código:
Código HTML:
<head>

<script type="text/javascript">

if(screen.width > 1024){
document.writeln("<link href='../../css/es/1152.css' rel='stylesheet' type='text/css' media='screen' />");

}
</script>
</head> 
Pero al pasar el validador W3C me dice que hay un error:

Código:
document type does not allow element "link" here
…f='../../css/es/1152.css' rel='stylesheet' type='text/css' media='screen' />");
✉
The element named above was found in a context where it is not allowed. This could mean that you have incorrectly nested elements -- such as a "style" element in the "body" section instead of inside "head" -- or two elements that overlap (which is not allowed).

One common cause for this error is the use of XHTML syntax in HTML documents. Due to HTML's rules of implicitly closed elements, this error can create cascading effects. For instance, using XHTML's "self-closing" tags for "meta" and "link" in the "head" section of a HTML document may cause the parser to infer the end of the "head" section and the beginning of the "body" section (where "link" and "meta" are not allowed; hence the reported error).
Que viene a decir que el código lo está metiendo en el body y no en el head.

Como haríais vosotros para que cogiera el css en función de la resolución sin usar CSS3 (porque el explorer 8 no lo soporta )
Con uno de estos doctype y usando bloques CDATA no vas a tener problemas

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <title>titulo</title>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6. <script type="text/javascript">
  7. //<![CDATA[
  8. if(screen.width > 1024){
  9. document.writeln("<link href='../../css/es/1152.css' rel='stylesheet' type='text/css' media='screen' />");
  10. }
  11. //]]>
  12. </head>
  13. </body>
  14. </html>



Código HTML:
Ver original
  1. <!DOCTYPE html>
  2. <html lang="es-ar">
  3. <meta charset="utf-8" />
  4. <title>Html5</title>
  5. <script type="text/javascript">
  6. //<![CDATA[
  7. if(screen.width > 1024){
  8. document.writeln("<link href='../../css/es/1152.css' rel='stylesheet' type='text/css' media='screen' />");
  9. }
  10. //]]>
  11. </head>
  12. </body>
  13. </html>

Si no tambien media queries, aunque para ie8 vas a necesitar algo de esto
https://www.google.com/search?btnG=1...eries+polyfill

Los comentarios condicionales de IE van a desaparecer en la versión 10 de IE, así mejor que ir dejándolos de usar

SAludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.

Etiquetas: css, html, meter, w3c
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:54.