<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>emenia.es &#187; CSS</title>
	<atom:link href="http://www.emenia.es/category/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.emenia.es</link>
	<description>Creación y diseño web</description>
	<lastBuildDate>Mon, 31 May 2010 14:34:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Nuevas fuentes a través de Google para tu web</title>
		<link>http://www.emenia.es/nuevas-fuentes-google-para-tu-web/</link>
		<comments>http://www.emenia.es/nuevas-fuentes-google-para-tu-web/#comments</comments>
		<pubDate>Thu, 20 May 2010 16:34:05 +0000</pubDate>
		<dc:creator>Juan Diaz-Bustamante</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.emenia.es/?p=867</guid>
		<description><![CDATA[<a href="http://googlecode.blogspot.com/2010/05/introducing-google-font-api-google-font.html">Google ha anunciado</a> su nuevo API para fuentes, incluyendo un <a href="http://code.google.com/webfonts">directorio de fuentes</a> y una herramienta para visualizarlas. Esto nos permitirá incluir en nuestra web fuentes diferentes de las tradicionales (las que están incluídas en el directorio de fuentes de Google). La gran ventaja es <a href="http://code.google.com/apis/webfonts/docs/getting_started.html#Quick_Start">su facilidad de uso</a>. Símplemente hay que incluir un enlace al CSS de la fuente en Google y luego asignar la fuente al elemento que deseemos desde nuestra hoja de estilos.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.emenia.es%2Fnuevas-fuentes-google-para-tu-web%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.emenia.es%2Fnuevas-fuentes-google-para-tu-web%2F&amp;source=JuanDBB_Emenia&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p><a href="http://googlecode.blogspot.com/2010/05/introducing-google-font-api-google-font.html">Google ha anunciado</a> su nuevo API para fuentes, incluyendo un <a href="http://code.google.com/webfonts">directorio de fuentes</a> y una herramienta para visualizarlas. Esto nos permitirá incluir en nuestra web fuentes diferentes de las tradicionales (las que están incluídas en el directorio de fuentes de Google). La gran ventaja es <a href="http://code.google.com/apis/webfonts/docs/getting_started.html#Quick_Start">su facilidad de uso</a>. Símplemente hay que incluir un enlace al CSS de la fuente en Google y luego asignar la fuente al elemento que deseemos desde nuestra hoja de estilos.</p>
<p>Como vemos se trata símplemente de aplicar <strong>@font-face</strong>, pero con la ventaja de cargar la fuente desde Google, lo cual permite mejorar la velocidad de carga y reducir el consumo de ancho de banda (es como cargar jQuery desde Google).</p>
<p>Vamos con un ejemplo. Elejimos una fuente desde el <a href="http://code.google.com/webfonts">directorio de fuentes de Google</a>, por ejemplo <strong>Josefin Sans Std Light</strong>. Vemos que al poner el ratón sobre la misma aparece un botón que dice <em>Click to embed Josefin Sans Std Light</em>. Al hacer click iremos a una página que nos muestra los distintos tamaños y estilos disponibles para la fuente, una descripción y el set de caracteres. Además hay una pestaña que dice <strong>Get the Code</strong>. Si hacemos click sobre la misma vemos que nos dice que tenemos que pegar dentro del head de nuestra página:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;link href='http://fonts.googleapis.com/css?family=Josefin+Sans+Std+Light' rel='stylesheet' type='text/css' /&gt;</pre></td></tr></table></div>

<p>Ojo que la etiqueta en Google viene sin cerrar. Arriba yo la he cerrado para que valide bien la página.</p>
<p>Luego, en nuestra hoja de estilos, si queremos asignar esta fuente a los elementos h1 haríamos:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">h1 <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">'Josefin Sans Std Light'</span><span style="color: #00AA00;">,</span> arial<span style="color: #00AA00;">,</span> <span style="color: #993333;">serif</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Y eso es todo!  Según nos dicen en Google este sistema es compatible con Google Chrome: versión 4.249.4+,  Mozilla Firefox: versión: 3.5+, Apple Safari: versión 3.1+ y Microsoft Internet Explorer: versión 6+.<br />
No soportado por iPhone, iPad, iPod, o Android.</p>
<p><a href="http://www.emenia.es/demos-blog/google-fonts/index.html">Aquí podéis ver la demo</a>. Esperemos que el número de fuentes se vaya incrementando.</p>
<h3>Otras lecturas de interés</h3>
<p><a href="http://googlecode.blogspot.com/2010/05/introducing-google-font-api-google-font.html">Anuncio de Google sobre el API de fuentes (en Inglés)</a><br />
<a href="http://code.google.com/webfonts">Directorio de fuentes de Google</a></p>
<div id="crp_related"><h3>Artículos relacionados:</h3><ul><li><a href="http://www.emenia.es/utilizando-css3-hoy-5-font-face/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (5): @font-face</a></li><li><a href="http://www.emenia.es/taxonomias-en-wordpress/" rel="bookmark" class="crp_title">Taxonomías en Wordpress</a></li><li><a href="http://www.emenia.es/primeros-pasos-tras-una-nueva-instalacion-de-wordpress/" rel="bookmark" class="crp_title">Primeros pasos tras una nueva instalación de Wordpress</a></li><li><a href="http://www.emenia.es/curso-de-jquery-3-selectores-segunda-parte/" rel="bookmark" class="crp_title">Curso de jQuery (3): Selectores, Segunda Parte</a></li><li><a href="http://www.emenia.es/recopilacion-de-45-trucos-para-wordpress/" rel="bookmark" class="crp_title">Recopilación de 45 trucos para Wordpress</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emenia.es/nuevas-fuentes-google-para-tu-web/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Internet Explorer 6 y display:inline</title>
		<link>http://www.emenia.es/internet-explorer-6-y-displayinline/</link>
		<comments>http://www.emenia.es/internet-explorer-6-y-displayinline/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 12:10:17 +0000</pubDate>
		<dc:creator>Juan Diaz-Bustamante</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.emenia.es/?p=585</guid>
		<description><![CDATA[De todos es conocido el quebradero de cabeza que supone <strong>adaptar nuestras webs a Internet Explorer 6</strong>. Todos tenemos nuestros trucos para intentar aliviar lo máximo posible las diferencias entre este navegador y los demás.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.emenia.es%2Finternet-explorer-6-y-displayinline%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.emenia.es%2Finternet-explorer-6-y-displayinline%2F&amp;source=JuanDBB_Emenia&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>De todos es conocido el quebradero de cabeza que supone <strong>adaptar nuestras webs a Internet Explorer 6</strong>. Todos tenemos nuestros trucos para intentar aliviar lo máximo posible las diferencias entre este navegador y los demás. Sin duda mi favorito es asignar en la hoja de estilo alternativa para este navegador la instrucción de CSS <strong><em>display: inline</em></strong> a todos los elementos a los que hayamos asignado un <strong><em>float</em></strong> y un margen.</p>
<p>Supongamos que asignamos un float a un elemento con un margen, por ejemplo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.elemento</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
  <span style="color: #808080; font-style: italic;">/* Aplicamos 100px al margen izquierdo */</span>
  <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>El problema es que mientras con todos los navegadores el margen izquierdo que en este caso hemos establecido en 100px será el esperado, con Internet Explorer 6 el margen será el doble, es decir 200px.</p>
<p>Pero si añadimos display: inline el problema está solucionado:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #6666ff;">.elemento</span> <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">inline</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">float</span><span style="color: #00AA00;">:</span> <span style="color: #000000; font-weight: bold;">left</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">margin-left</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
  <span style="color: #808080; font-style: italic;">/* Aplicamos 100px al margen izquierdo */</span>
  <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Este es un viejo truco, conocido hace tiempo, pero que estoy utilizando ahora con bastante frecuencia en una web que estoy haciendo. Por ello pensé que sería interesante recordarlo mientras esperamos a que Internet Explorer 6 desaparezca para siempre de nuestras vidas&#8230;&#8230;.</p>
<div id="crp_related"><h3>Artículos relacionados:</h3><ul><li><a href="http://www.emenia.es/utilizando-css3-hoy-1-bordes-redondeados/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (1): Bordes Redondeados</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-2-colores-y-opacidad-utilizando-rbga/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (2): Colores y opacidad utilizando RBGA</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-5-font-face/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (5): @font-face</a></li><li><a href="http://www.emenia.es/nuevas-fuentes-google-para-tu-web/" rel="bookmark" class="crp_title">Nuevas fuentes a través de Google para tu web</a></li><li><a href="http://www.emenia.es/os-presento-codigoytrucos-com/" rel="bookmark" class="crp_title">Os presento CodigoyTrucos.com</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emenia.es/internet-explorer-6-y-displayinline/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utilizando CSS3 hoy (5): @font-face</title>
		<link>http://www.emenia.es/utilizando-css3-hoy-5-font-face/</link>
		<comments>http://www.emenia.es/utilizando-css3-hoy-5-font-face/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 18:46:54 +0000</pubDate>
		<dc:creator>Juan Diaz-Bustamante</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.emenia.es/?p=465</guid>
		<description><![CDATA[Vamos a ver en detalle una de las funcionalidades más interesantes de CSS3: la posibilidad de usar fuentes nuevas de forma segura en nuestras webs.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.emenia.es%2Futilizando-css3-hoy-5-font-face%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.emenia.es%2Futilizando-css3-hoy-5-font-face%2F&amp;source=JuanDBB_Emenia&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Una de las grandes limitaciones actuales en el diseño y desarrollo web es la <strong>limitada lista de fuentes que se pueden utilizar de forma segura</strong> Con <strong><em>&#8220;forma segura&#8221;</em></strong> entendemos que la fuente que hemos escogido esté instalada en todos (o la mayoría) de los ordenadores de los usuarios que van a entrar en nuestra web. Hay muy pocas fuentes que sean &#8220;seguras&#8221;. Quizás las fuentes seguras más conocidas sean Arial y Verdana. (<a href="http://www.ampsoft.net/webdesign-l/WindowsMacFonts.html" title="Ir a ampsoft para ver una lista de fuentes seguras">Ver lista de fuentes seguras</a>).</p>
<p>Se han desarrollado distintas soluciones para poder utilizar cualquier fuente &#8220;no segura&#8221;, ya sea utilizando Flash (<a href="http://wiki.novemberborn.net/sifr/" title="Ir a la web de sIFR"><strong>sIFR</strong></a>), o Javascript (<a href="http://wiki.github.com/sorccu/cufon/about" title="Ir a la web de Cufón"><strong>Cufón</strong></a>). Pero el futuro está en CSS3 y su declaración<strong> @font-face</strong>.</p>
<p>Para ver cómo funciona lo mejor es poner un ejemplo. </p>
<p>Vamos a comenzar por escoger una fuente gratuita. He seleccionado Cookies (<a href="http://www.dafont.com/cookies.font" title="Ver la fuente Cookies">http://www.dafont.com/cookies.font</a>).</p>
<p>Lo primero que haremos en nuestra hoja de estilos es crear una declaración que defina la fuente que vamos a utilizar:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@font-face { </span>
   <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> cookies<span style="color: #00AA00;">;</span> 
   src<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000; font-style: italic;">fuentes/cookies.ttf</span><span style="color: #00AA00;">&#41;</span> format<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'truetype'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
 <span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Si la fuente en vez de ser TrueType (.ttf) fuera OpenType (.otf), pondríamos &#8220;opentype&#8221; en vez de &#8220;truetype&#8221;.</p>
<p>Con esta declaración conseguimos dos cosas: en primer lugar damos un nombre a la fuente que vamos a utilizar (en este caso <em>cookies</em>), en segundo lugar asociamos ese nombre al enlace directo de la fuente que vamos a utilizar bajo ese nombre (en este caso la fuente cookies que se encuentra en formato truetype dentro del directorio<em> fuentes</em>).</p>
<p>Ahora que ya hemos definido la fuente podremos utilizarla como cualquier otra fuente en nuestra hoja de estilos:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">body <span style="color: #00AA00;">&#123;</span> 	
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> cookies<span style="color: #00AA00;">,</span> Verdana<span style="color: #00AA00;">,</span> Tahoma<span style="color: #00AA00;">,</span> Geneva<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span> 
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">62.5</span>%</span><span style="color: #00AA00;">;</span> 
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Veámoslo en un ejemplo (ojo: sólo lo veremos bien en Safari 3.1+, Firefox 3.5+, Chrome y Opera 10. ¿Internet Explorer? En seguida veremos cómo hacerlo): <a href="http://www.emenia.es/demos-blog/font-face/" title="Ver demo de utilización de font-face" rel="nofollow">Ver demo de utilización de @font-face</a>. Podéis descargar el código aquí: <a href="/demos-blog/font-face/font-face.zip" title="Descargar código en formato ZIP" rel="nofollow">Descargar Demo 1</a></p>
<p>Hasta aquí todo muy fácil. Sin embargo hay dos problemas:</p>
<p>1) <strong>El problema legal</strong>. Hay muchas fuentes (especialmente las comerciales) que no permiten que se las use utilizando @font-face. La razón probablemente sea que entonces serían muy fácilmente pirateables. Hay que tener por lo tanto mucho cuidado con las fuentes que se utilizan. Lo primero que hay que leer es su documento de licencia (incluso si son fuentes gratuitas) y ver si permiten su uso mediante font-face, y si lo permiten ver bajo qué condiciones (en ocasiones sólo hay que hay que hacer una mención a la fuente y a su autor en la hoja de estilos, por ejemplo).</p>
<p>Hay algunos <strong>sitios muy útiles</strong> que nos ofrecen fuentes gratuitas señalando incluso cuales se pueden utilizar con @font-face, como <a href="http://www.fontsquirrel.com/" title="Ir a Font Squirrel">Font Squirrel</a>, <a href="http://kernest.com/" title="Ir a Kernest">Kernest</a> o <a href="http://webfonts.info/wiki/index.php?title=Fonts_available_for_%40font-face_embedding" title="Ir a Webfonts">Webfonts</a>.</p>
<p>2) <strong>El problema de la compatibilidad</strong>. Hemos hablado hasta ahora que @font-face es sólo compatible con Safari, Chrome, Firefox y Opera. Pero, <strong>¿qué pasa con Internet Explorer? </strong> Pues resulta que <strong>@font-face lo soporta Internet Explorer desde su versión 4</strong>. Sorprendente ¿verdad? El único problema reside en que Internet Explorer, de momento, sólo soporta un formato propietario de Microsoft llamado <strong>Embedded Open Type (.eot)</strong>. Por ello necesitamos incluir dos ficheros si queremos que @font-face funcione en todos los navegadores: uno TrueType o OpenType para Safari, Chrome, Opera o Firefox y otro EOT para Microsoft Explorer.  </p>
<p>No hay muchas fuentes disponibles en formato .eot de Microsoft. Hay una herramienta muy fácil de usar llamada <a href="http://code.google.com/p/ttf2eot/" title="Ir a ttf2">ttf2</a> que convierte fuentes TrueType a EOT. Para convertir fuentes OpenType hay que convertirlas primero a TrueType (se puede hacer con <a href="http://fontforge.sourceforge.net/" title="Ir a FontForge">FontForge</a>).</p>
<p>De nuevo, hay sitios web como <a href="http://www.fontsquirrel.com/" title="Ir a Font Squirrel">Font Squirrel</a> que nos ofrecen ya fuentes gratuitas en formato EOT en lo que llaman <a href="http://www.fontsquirrel.com/fontface" title="Ver los @font-face kits">@font-face kit</a> (que incluye todos los formatos). Font Squirrel tiene incluso un <a href="http://www.fontsquirrel.com/fontface/generator" title="Ir al generador de @font-face">generador de @font-face</a>.</p>
<p>Vamos a ver un ejemplo utilizando una fuente en formato EOT. De Font Squirrel he bajado la fuente <a href="http://www.fontsquirrel.com/fonts/SF-Cartoonist-Hand" title="Ver fuente">Cartoonish Hand</a>, y de ella voy a usar dos ficheros que he grabado en la carpeta fuentes: SF_Cartoonist_Hand_SC.ttf y SF_Cartoonist_Hand_SC.eot</p>
<p>En la hoja de estilos haría esta vez:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@font-face {</span>
  <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">'Cartoonist Hand'</span><span style="color: #00AA00;">;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">/* Para Internet Explorer */</span>
	src<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'/fuentes/SF_Cartoonist_Hand_SC.eot'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">/*  Para el resto de navegadores */</span>
	src<span style="color: #00AA00;">:</span> local<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'Cartoonist Hand'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>
	     <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'/fuentes/SF_Cartoonist_Hand_SC.ttf'</span><span style="color: #00AA00;">&#41;</span> format<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'truetype'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Vemos que primero (<strong>y es importante el orden</strong>) hemos incluído el archivo con formato .eot para Internet Explorer y luego hemos incluido la declaración para el resto de los navegadores. Como novedad, hemos incluido <em>&#8220;src: local(&#8216;Cartoonist Hand&#8217;)&#8221;</em> que busca primero si la fuente está instalada en el ordenador del usuario que abre la web. Si no es así la carga desde el servidor de la web (la siguiente línea).</p>
<p>Luego haríamos:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">  body  <span style="color: #00AA00;">&#123;</span>	
    <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#333</span><span style="color: #00AA00;">;</span> 
    <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">'Cartoonist Hand'</span><span style="color: #00AA00;">,</span> Verdana<span style="color: #00AA00;">,</span> Tahoma<span style="color: #00AA00;">,</span> Geneva<span style="color: #00AA00;">,</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span> 
    <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;"><span style="color: #cc66cc;">62.5</span>%</span><span style="color: #00AA00;">;</span> 
    <span style="color: #000000; font-weight: bold;">line-height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1.7em</span><span style="color: #00AA00;">;</span> 
    <span style="color: #000000; font-weight: bold;">text-align</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">center</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Podemos ver la demo aquí: <a href="http://www.emenia.es/demos-blog/font-face-dos/" rel="nofollow" title="Ir a Demo 2">Ver demo de @font-face con archivos .eot para Microsoft Explorer</a>. Podéis bajar el código  aquí: <a href="/demos-blog/font-face-dos/font-face-dos.zip" title="Descargar código en formato ZIP" rel="nofollow">Descargar Demo 2</a></p>
<p>Por último es importante decir que si queremos utilizar distintas versiones de la misma fuente (su itálica o su negrita, por ejemplo) hay que cargar todos los archivos correspondientes, por ejemplo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@font-face {</span>
  <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">'Cartoonist Hand'</span><span style="color: #00AA00;">;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">/* Para Internet Explorer */</span>
	src<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'/fuentes/SF_Cartoonist_Hand_SC.eot'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">/*  Para el resto de navegadores */</span>
	src<span style="color: #00AA00;">:</span> local<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'Cartoonist Hand'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>
	     <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'/fuentes/SF_Cartoonist_Hand_SC.ttf'</span><span style="color: #00AA00;">&#41;</span> format<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'truetype'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
<span style="color: #a1a100;">@font-face {</span>
  <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">'Cartoonist Hand Bold'</span><span style="color: #00AA00;">;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">/* Para Internet Explorer */</span>
	src<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'/fuentes/SF_Cartoonist_Hand_SC_Bold.eot'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
&nbsp;
 <span style="color: #808080; font-style: italic;">/*  Para el resto de navegadores */</span>
	src<span style="color: #00AA00;">:</span> local<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'Cartoonist Hand'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">,</span>
	     <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'/fuentes/SF_Cartoonist_Hand_SC_Bold.ttf'</span><span style="color: #00AA00;">&#41;</span> format<span style="color: #00AA00;">&#40;</span><span style="color: #ff0000;">'truetype'</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Como podéis ver el uso de @font-face es muy sencillo. Una vez se solucionen los problemas legales sobre su uso e Internet Explorer soporte TrueType y OpenType (que seguramente lo hará con Internet Explorer 9) @font-face solucionará el problema del uso de la fuente que deseemos en nuestra web. De todas formas, hay ya muchos sitios web que utilizan ya @font-face, que como habéis visto se puede usar con facilidad con fuentes gratuitas.</p>
<h3>Más lecturas recomendadas</h3>
<p>Themeforest, <a href="http://blog.themeforest.net/tutorials/how-to-achieve-cross-browser-font-face-support/" title="Ir al artículo">How to achieve cross-browser @font-face support</a><br />
Snook.ca,  <a href="http://snook.ca/archives/html_and_css/becoming-a-font-embedding-master" title="Ir al artículo">Becoming a Font Embedding Master</a><br />
Paul Irish, <a href="http://paulirish.com/2009/bulletproof-font-face-implementation-syntax/" title="Ir al artículo">Bulletproof @font-face syntax</a><br />
User Agent Man, <a href="http://www.useragentman.com/blog/2009/09/20/font-face-in-depth/" title="Ir al artículo">@font-face in Depth</a><br />
AllCreatives.net, ofrece un <a href="http://allcreatives.net/2009/12/05/smoother-font-face-embedding-in-ie-7-8/" title="Ir a AllCreatives.net">sistema para mejorar cómo se ven las fuentes en IE7 e IE8 utilizando @font-face</a><br />
spyrestudios, nos ofrece 21 fuentes disponibles para @font-face: <a href="http://spyrestudios.com/21-awesome-font-face-embedable-typefaces/" title="Ir al artículo">21 Awesome @font-face Embeddable Typefaces</a><br />
Podemos ver más información sobre los navegadores que soportan @font-face en <a href="http://webfonts.info/wiki/index.php?title=%40font-face_browser_support" title="Ir a webfonts">webfonts</a>. </p>
<div id="crp_related"><h3>Artículos relacionados:</h3><ul><li><a href="http://www.emenia.es/nuevas-fuentes-google-para-tu-web/" rel="bookmark" class="crp_title">Nuevas fuentes a través de Google para tu web</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-3-sombras/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (3): Sombras</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-1-bordes-redondeados/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (1): Bordes Redondeados</a></li><li><a href="http://www.emenia.es/como-pre-cargar-imagenes-con-jquery/" rel="bookmark" class="crp_title">Cómo pre-cargar imágenes con jQuery</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-4-border-image/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (4): border-image</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emenia.es/utilizando-css3-hoy-5-font-face/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utilizando CSS3 hoy (4): border-image</title>
		<link>http://www.emenia.es/utilizando-css3-hoy-4-border-image/</link>
		<comments>http://www.emenia.es/utilizando-css3-hoy-4-border-image/#comments</comments>
		<pubDate>Sun, 29 Nov 2009 19:36:44 +0000</pubDate>
		<dc:creator>Juan Diaz-Bustamante</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.emenia.es/?p=409</guid>
		<description><![CDATA[Otra posibilidad que nos ofrece CSS3 es la de utilizar imágenes en los bordes de elementos de nuestra web en lugar del borde normal, gracias a border-image.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.emenia.es%2Futilizando-css3-hoy-4-border-image%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.emenia.es%2Futilizando-css3-hoy-4-border-image%2F&amp;source=JuanDBB_Emenia&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Vamos a ver hoy otra posibilidad que nos ofrece CSS3 y que soportan ya Safari 3.1+, FireFox 3.5+ y Chrome 1.0+: La de utilizar imágenes en los bordes de elementos de nuestra web en lugar del borde normal, utilizando la porción de la imagen que indiquemos y estirándola también lo que indiquemos en la instrucción de CSS3.</p>
<p>La sintaxis a utilizar es un poco compleja, pero con los ejemplos creo que se entenderá mucho mejor (eso espero&#8230;). </p>
<p>La propiedad es <strong><em>border-image</em></strong>, aunque para utilizarla hoy hay que anteponer los prefijos que hemos visto para otras instrucciones de CSS3: <em>-webkit</em> para Safari y Chrome y <em>-moz</em> para Firefox:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">-webkit-border-image<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>imagen<span style="color: #00AA00;">&#41;</span> %arriba %derecha %abajo %izquierda stretch stretch<span style="color: #00AA00;">;</span>
-moz-border-image<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>imagen<span style="color: #00AA00;">&#41;</span> %arriba %derecha %abajo %izquierda stretch stretch<span style="color: #00AA00;">;</span></pre></td></tr></table></div>

<p>En donde pone <em><strong>imagen</strong></em> habrá que poner la ruta hasta la imagen que vayamos a utilizar en el borde. Luego hay que indicar el <em><strong>tamaño que se va a utilizar de cada esquina de la imagen</strong></em> para cada esquina del nuevo borde de nuestro elemento (en el orden de las agujas del reloj, es decir arriba, derecha, abajo e izquierda.<strong> Se pueden expresar en porcentajes o sin ninguna unidad de medida para expresar píxeles exactos</strong>, es decir por ejemplo 10 10 10 10). La última parte, donde poner <em><strong>stretch</strong></em>, determina cómo se van a escalar y cortar los bordes de los lados y superior e inferior de nuestro elemento. <strong>Hay tres posibles valores:</strong><br />
- stretch (estirar)<br />
- repeat (repetir)<br />
- round (redondear)<br />
Si especificamos sólo un valor es como poner dos veces el mismo (por ejemplo sólo una vez stretch. Entonces será stretch tanto para los lados superior e inferior como para los lados izquierda y derecha).  El valor por defecto es &#8220;stretch&#8221;.</p>
<p>Vamos a ver algunos ejemplos para ver cómo funciona:</p>
<p>Supongamos que vamos a utilizar esta imagen para crear los bordes. Es un gif de 50px por 50px que lo he llamado borde1.gif:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/borde11.gif" alt="borde1" title="borde1" width="50" height="50" class="alignleft size-full wp-image-414" /><br />
<br class="clear" /></p>
<p>Ahora supongamos que tenemos un elemento DIV de 300px de ancho y 100px de alto para el que queremos poner de borde la imagen anterior. Si hacemos:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;border-image&lt;/title&gt;
  &lt;style type=&quot;text/css&quot;&gt;
   div {
     width: 300px;
     height: 200px;
     padding: 10px; /* pongo un padding para despegar el texto de los bordes*/
     border-width: 10px;
     -webkit-border-image: url(borde1.gif) 25% stretch stretch;
     -moz-border-image: url(borde1.gif) 25% stretch stretch;
    }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div&gt;
     &lt;p&gt;Este es un texto dentro del div sobre el que vamos a aplicar como borde una imagen&lt;/p&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>El resultado es este:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/borde1r.png" alt="borde1r" title="borde1r" width="351" height="249" class="alignleft size-full wp-image-425" /><br />
<br class="clear" /></p>
<p>¿Qué ha sucedido? Si vemos el código, en primer lugar hemos dicho que el grosor del borde debe ser de 10px (con <em>border-width</em>).  Esos 10 píxels de borde se van a rellenar con nuestra imagen <em>borde1.gif</em>. Para ello hemos aplicado <em>border-image</em> para los navegadores que usan webkit (Safari y Chrome) y a los de Mozilla (Firefox) con sus respectivos prefijos. Vamos a ver los parámetros que hemos elegido para <em>border-image</em> uno a uno:<br />
- url(borde1.gif) : indicamos el camino y nombre de la imagen que vamos a usar para crear el borde.<br />
- 25% : Hemos elegido el mismo valor para las cuatro esquinas (arriba, derecha, abajo e izquierda). Podríamos haber puesto cuatro valores diferentes o dos valores (arriba-abajo e izquierda-derecha). ¿Qué nos indica este 25%? Que se va a tomar para rellenar los 10 píxels de borde de la esquina superior izquierda del <em>DIV</em> el 25% de la imagen que usamos como borde cortando desde la esquina superior izquierda. Y lo mismo para el resto de esquinas:</p>
<p><img src="http://www.emenia.es/wp-content/uploads/2009/11/borde1e.png" alt="borde1e" title="borde1e" width="139" height="118" class="alignleft size-full wp-image-429" /><br />
<br class="clear" /></p>
<p>- stretch stretch: indica que tanto para los lados superior-inferior como para los lados izquierda-derecha lo que se hace es alargar la imagen del borde hasta que se llegue a la siguiente esquina.</p>
<p><strong>Vamos a ver otro ejemplo</strong>. Supongamos que la imagen que vamos a usar como borde es esta:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/borde2.gif" alt="borde2" title="borde2" width="56" height="57" class="alignleft size-full wp-image-434" /><br />
<br class="clear" /></p>
<p>Con este código:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;border-image&lt;/title&gt;
  &lt;style type=&quot;text/css&quot;&gt;
   div {
     width: 300px;
     height: 200px;
     padding: 10px;
     border-width: 10px;
     -webkit-border-image: url(borde2.gif) 28% stretch;
     -moz-border-image: url(borde2.gif) 28% stretch;
    }
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div&gt;
     &lt;p&gt;Este es un texto dentro del div sobre el que vamos a aplicar como borde una imagen&lt;/p&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Se obtiene esto (ahora se ve mejor el efecto de stretch alargando la imagen del borde hasta encontrar la otra esquina):<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/borde2r.png" alt="borde2r" title="borde2r" width="352" height="250" class="alignleft size-full wp-image-435" /><br />
<br class="clear" /></p>
<p>Si hubiéramos puesto <em>round</em> en vez de <em>stretch</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">     -webkit-border-image<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>borde2.gif<span style="color: #00AA00;">&#41;</span> <span style="color: #933;"><span style="color: #cc66cc;">28</span>%</span> round<span style="color: #00AA00;">;</span>
     -moz-border-image<span style="color: #00AA00;">:</span> <span style="color: #993333;">url</span><span style="color: #00AA00;">&#40;</span>borde2.gif<span style="color: #00AA00;">&#41;</span> <span style="color: #933;"><span style="color: #cc66cc;">28</span>%</span> round<span style="color: #00AA00;">;</span></pre></td></tr></table></div>

<p>tendríamos:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/border2r2.png" alt="border2r2" title="border2r2" width="351" height="249" class="alignleft size-full wp-image-436" /><br />
<br class="clear" /></p>
<p>Es un poco difícil cogerle el truco, pero haciendo pruebas con los porcentajes y los efectos se pueden lograr cosas interesantes, por ejemplo para crear botones. Os recomiendo los artículos recomendados que siguen a continuación que, aunque estén en inglés, se ven algunos ejemplos prácticos. Además, en el de irbabe.com se nos enseña cómo conseguir aplicar imágenes a bordes con jQuery para que así funcione también con Internet Explorer.</p>
<h3>Otras lecturas recomendadas</h3>
<p>irbabe.com: <a href="http://www.lrbabe.com/sdoms/borderImage/index.html">Meet a ninja living in browsers</a><br />
John Resig Bog: <a href="http://ejohn.org/blog/border-image-in-firefox/">border-image in Firefox</a><br />
css3.info: <a href="http://www.css3.info/preview/border-image/">Border-image: using images for your border</a><br />
elliotswan.com: <a href="http://www.elliotswan.com/2009/07/27/9-css3-properties-you-can-use-now/">9 CSS3 properties you can use now</a></p>
<div id="crp_related"><h3>Artículos relacionados:</h3><ul><li><a href="http://www.emenia.es/utilizando-css3-hoy-1-bordes-redondeados/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (1): Bordes Redondeados</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-3-sombras/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (3): Sombras</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-2-colores-y-opacidad-utilizando-rbga/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (2): Colores y opacidad utilizando RBGA</a></li><li><a href="http://www.emenia.es/curso-de-jquery-3-selectores-segunda-parte/" rel="bookmark" class="crp_title">Curso de jQuery (3): Selectores, Segunda Parte</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-5-font-face/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (5): @font-face</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emenia.es/utilizando-css3-hoy-4-border-image/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utilizando CSS3 hoy (3): Sombras</title>
		<link>http://www.emenia.es/utilizando-css3-hoy-3-sombras/</link>
		<comments>http://www.emenia.es/utilizando-css3-hoy-3-sombras/#comments</comments>
		<pubDate>Fri, 27 Nov 2009 17:35:06 +0000</pubDate>
		<dc:creator>Juan Diaz-Bustamante</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.emenia.es/?p=370</guid>
		<description><![CDATA[Nueva entrega sobre propiedades CSS3 que podemos aplicar hoy: Sombras en textos y elementos.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.emenia.es%2Futilizando-css3-hoy-3-sombras%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.emenia.es%2Futilizando-css3-hoy-3-sombras%2F&amp;source=JuanDBB_Emenia&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Seguimos con propiedades de CSS3 que podemos utilizar hoy en nuestras web, siempre teniendo en cuenta que son propiedades no soportadas por Internet Explorer. Hoy vamos a ver las sombras.</p>
<h3>Text-shadow</h3>
<p>En primer lugar vamos a ver <em><strong>text-shadow</strong></em>, que permite aplicar sombras a textos. Actualmente lo soportan Safari, Chrome, Opera y Firefox 3.5. Su sintaxis es:</p>
<p><img src="http://www.emenia.es/wp-content/uploads/2009/11/shadow32.png" alt="shadow3" title="shadow3" width="543" height="46" class="alignleft size-full wp-image-399" /><br />
<br class="clear" /></p>
<p>Veamos un ejemplo:</p>
<p>Supongamos que tenemos un texto al que le aplicamos estos estilos:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">p <span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">text-shadow</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #933;">3px</span> <span style="color: #933;">4px</span> <span style="color: #cc00cc;">#666</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">24px</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>El resultado es:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/shadow1.gif" alt="shadow1" title="shadow1" width="303" height="74" class="alignleft size-full wp-image-371" /><br />
<br class="clear" /></p>
<p>Tenemos una sombra de 3px a la derecha, 3px hacia abajo, 4px de desenfoque y con un color de sombra #666.</p>
<p>Hay que tener en cuenta que la utilización de text-shadow puede hacer que no se valide nuestra hoja de estilos respecto a las especificaciones de CSS 2.1, pero ¿es eso importante? Yo creo que no.</p>
<p>Podemos también utilizar rgba (que vimos en el anterior artículo) en vez del color en formato hexadecimal, es decir:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">p <span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">text-shadow</span><span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #933;">3px</span> <span style="color: #933;">4px</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> .5<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">24px</span><span style="color: #00AA00;">;</span> 
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Por lo que en vez del gris #666 utilizamos un negro con una opacidad del 50%</p>
<p>Conviene jugar con las distintas combinaciones de valores de la sombra para conseguir los efectos más adecuados a lo que queremos. Podemos incluso lograr efectos de relieve interno:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">p <span style="color: #00AA00;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">text-shadow</span><span style="color: #00AA00;">:</span> <span style="color: #933;">0px</span> <span style="color: #933;">1px</span> <span style="color: #cc66cc;">0</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">255</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">255</span><span style="color: #00AA00;">,</span> .5<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-size</span><span style="color: #00AA00;">:</span> <span style="color: #933;">24px</span><span style="color: #00AA00;">;</span> 
  <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#777</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">&quot;Arial Black&quot;</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
  <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><img src="http://www.emenia.es/wp-content/uploads/2009/11/shadow2.gif" alt="shadow2" title="shadow2" width="488" height="50" class="alignleft size-full wp-image-373" /><br />
<br class="clear" /></p>
<h3>Box-shadow</h3>
<p>Otro tipo de sombra que podemos aplicar es <strong><em>box-shadow</em></strong>, que en vez de aplicarse a un texto se aplica <strong>a cualquier elemento</strong>. </p>
<p>Su sintaxis es como la de text-shadow:</p>
<p><img src="http://www.emenia.es/wp-content/uploads/2009/11/shadow4.png" alt="shadow4" title="shadow4" width="543" height="46" class="alignleft size-full wp-image-382" /><br />
<br class="clear" /></p>
<p>Veamos un ejemplo, con un elemento <strong><em>p</em></strong> dentro de un elemento <em><strong>div</strong></em>, aplicando una sombra al elemento <em><strong>div</strong></em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">&lt;html xmlns<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> lang<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;es&quot;</span> xml<span style="color: #00AA00;">:</span>lang<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;es&quot;</span><span style="color: #00AA00;">&gt;</span>
&lt;head<span style="color: #00AA00;">&gt;</span>
&lt;style type<span style="color: #00AA00;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span><span style="color: #00AA00;">&gt;</span>
div <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">300px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">60px</span><span style="color: #00AA00;">;</span>
	box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #933;">3px</span> 4pc <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span>
	-webkit-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #933;">3px</span> <span style="color: #933;">4px</span> <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span>
	-moz-box-shadow<span style="color: #00AA00;">:</span> <span style="color: #933;">3px</span> <span style="color: #933;">3px</span> <span style="color: #933;">4px</span> <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#eaeaea</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#ccc</span><span style="color: #00AA00;">;</span>
	-moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
	-webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
p <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#666</span><span style="color: #00AA00;">;</span>
	<span style="color: #000000; font-weight: bold;">font-family</span><span style="color: #00AA00;">:</span> <span style="color: #ff0000;">&quot;Arial Black&quot;</span> <span style="color: #993333;">sans-serif</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
&lt;/style<span style="color: #00AA00;">&gt;</span>
&lt;/head<span style="color: #00AA00;">&gt;</span>
&lt;body<span style="color: #00AA00;">&gt;</span>
&lt;div<span style="color: #00AA00;">&gt;</span>
	&lt;p<span style="color: #00AA00;">&gt;</span>Este es un texto dentro de un DIV&lt;/p<span style="color: #00AA00;">&gt;</span>
&lt;/div<span style="color: #00AA00;">&gt;</span>
&lt;/body<span style="color: #00AA00;">&gt;</span>
&lt;/html<span style="color: #00AA00;">&gt;</span></pre></td></tr></table></div>

<p>Vemos que hemos aplicado al elemento <em><strong>div</strong></em> una anchura de 300px, una altura de 100px, una sombra de 3px a la derecha, 3px hacia abajo, un desenfoque de 4px y un color de sombra #ccc. Vemos que hemos aplicado la instrucción box-shadow que hoy en día aún no está reconocida por los navegadores, por lo que más adelante hemos aplicado las específicas de webkit (Safari y Chrome) y Mozilla (Firefox). Además le hemos puesto un color de fondo #eaeaea, un borde de 1px sólido de color #ccc y bordes redondeados con un radio de 10px.</p>
<p><img src="http://www.emenia.es/wp-content/uploads/2009/11/shadow51.png" alt="shadow5" title="shadow5" width="340" height="94" class="alignleft size-full wp-image-390" /><br />
<br class="clear" /></p>
<h3>Otras lecturas recomendadas</h3>
<p>vagabundia &#8211; <a href="http://vagabundia.blogspot.com/2009/08/sombras-la-propiedad-text-shadow.html" title="Ir al artículo">Sombras: la propiedad text-shadow</a><br />
mozilla hispano &#8211; <a href="http://www.mozilla-hispano.org/documentacion/Sombras_en_cajas_con_box-shadow" title="Ir al artículo">Sombras en cajas con box-shadow</a></p>
<div id="crp_related"><h3>Artículos relacionados:</h3><ul><li><a href="http://www.emenia.es/utilizando-css3-hoy-1-bordes-redondeados/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (1): Bordes Redondeados</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-4-border-image/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (4): border-image</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-2-colores-y-opacidad-utilizando-rbga/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (2): Colores y opacidad utilizando RBGA</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-5-font-face/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (5): @font-face</a></li><li><a href="http://www.emenia.es/curso-de-jquery-2-selectores-primera-parte/" rel="bookmark" class="crp_title">Curso de jQuery (2): Selectores, Primera Parte</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emenia.es/utilizando-css3-hoy-3-sombras/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utilizando CSS3 hoy (2): Colores y opacidad utilizando RBGA</title>
		<link>http://www.emenia.es/utilizando-css3-hoy-2-colores-y-opacidad-utilizando-rbga/</link>
		<comments>http://www.emenia.es/utilizando-css3-hoy-2-colores-y-opacidad-utilizando-rbga/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 07:09:25 +0000</pubDate>
		<dc:creator>Juan Diaz-Bustamante</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.emenia.es/?p=353</guid>
		<description><![CDATA[Seguimos viendo propiedades de CSS3 que podemos utilizar en nuestras webs. Hoy veremos cómo aplicar colores mediante RGBA.  ]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.emenia.es%2Futilizando-css3-hoy-2-colores-y-opacidad-utilizando-rbga%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.emenia.es%2Futilizando-css3-hoy-2-colores-y-opacidad-utilizando-rbga%2F&amp;source=JuanDBB_Emenia&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>RGB (red, green, blue, es decir rojo, verde, azul) es un modelo que permite obtener cualquier color a partir de la combinación de esos tres colores. Los que conocéis CSS estáis habituados a especificar un color por su valor hexadecimal, por ejemplo #000000 para el negro. Pero todo color tiene también su valor RGB, que en el caso del negro es R:0 G:0 B:0. En el extremo contrario estaría el blanco que sería R:255 G:255 B:255. </p>
<p>Hasta ahora podíamos definir un color no sólo mediante su valor hexadecimal, sino también según sus valores RGB. Por ejemplo. para poner un fondo negro a un div:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">div <span style="color: #00AA00;">&#123;</span> 
   <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">rgb</span><span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
   <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><img src="http://www.emenia.es/wp-content/uploads/2009/11/rgb.gif" alt="rgb" title="rgb" width="69" height="61" class="alignleft size-full wp-image-354" /><br />
<br class="clear" /></p>
<p>Pero gracias a CSS3 podemos incluir un valor más para cada color: <strong>su opacidad</strong>. Entonces hablaríamos de RGBA, siendo A=Alpha, que nos permitirá especificar la opacidad del color. El valor de A es un número entre 1 y 0, siendo 1 totalmente opaco y 0 totalmente transparente. Si seguimos el ejemplo anterior del fondo negro y queremos que tenga una opacidad del 50% haremos:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">div <span style="color: #00AA00;">&#123;</span> 
   <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #00AA00;">,</span> .5<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span> 
   <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><img src="http://www.emenia.es/wp-content/uploads/2009/11/rgba1.gif" alt="rgba" title="rgba" width="69" height="65" class="alignleft size-full wp-image-358" /><br />
<br class="clear" /></p>
<p>Como vemos hemos situado su opacidad al 50% por lo que si hubiera algo debajo de ese DIV <strong>se vería</strong>.</p>
<p>Existe ya una propiedad en CSS3 para conseguir la opacidad de un elemento. Se trata de <strong><em>opacity</em></strong>. Por ejemplo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">div <span style="color: #00AA00;">&#123;</span> 
   <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span> 
   opacity<span style="color: #00AA00;">:</span> .50<span style="color: #00AA00;">;</span>  
   <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Hemos definido el color del fondo y luego le aplicamos &#8220;opacity&#8221;. El resultado parece ser el mismo:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/opacity.gif" alt="opacity" title="opacity" width="69" height="65" class="alignleft size-full wp-image-359" /><br />
<br class="clear" /></p>
<p>A primera vista no hay diferencias, hasta que incluimos algo dentro del div. Vamos a verlo. Supongamos que, simplificando al máximo, tenemos:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;style type=&quot;text/css&quot;&gt;
div { 
   background: #000; 
   opacity: .50;  
   width: 50px;
   height: 50px;
}
p { 
   color: #000;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
&lt;p&gt;TEXTO&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Es decir un elemento &#8220;div&#8221; que tiene en su interior un elemento &#8220;p&#8221; con la palabra &#8220;TEXTO&#8221;. Vemos que hemos aplicado al elemento p el color negro (#000;) lo mismo que al fondo del elemento DIV. Pero además al elemento DIV le hemos aplicado una opacidad del 50%  Esto es lo que vemos:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/opacity2.gif" alt="opacity2" title="opacity2" width="71" height="66" class="alignleft size-full wp-image-360" /><br />
<br / class="clear" /></p>
<p>¿Donde está el texto? No se ve por una razón: con <strong>opacity</strong> no sólo se altera la opacidad del elemento sino además se altera también la opacidad de cualquier elemento que se encuentre dentro del primero, en este caso el elemento<strong> p</strong>.  Es decir, el elemento <strong>p</strong> tiene aquí también una opacidad del 50% y entonces tiene el mismo color que el elemento <strong>div</strong> y por eso no se ve.</p>
<p>Vamos a hacerlo ahora con RGBA:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;html&gt;
&lt;head&gt;
&lt;style type=&quot;text/css&quot;&gt;
div { 
   background: rgba(0,0,0,.5);  
   width: 50px;
   height: 50px;
}
p { 
   color: #000;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
&lt;p&gt;TEXTO&lt;/p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>Ahora lo que veremos es esto:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/rgba2.gif" alt="rgba2" title="rgba2" width="66" height="68" class="alignleft size-full wp-image-361" /><br />
<br class="clear" /></p>
<p>Como vemos con RBGA se afecta sólo a la opacidad del elemento sobre el que se aplica la propiedad, sin afectar a los elementos que se encuentren en su interior.</p>
<p>La propiedad RGBA en estos momentos la soporta Safari, Chrome, Firefox 3 y Opera 10. Internet Explorer (que raro) no la soporta. Entonces si se quiere utilizar un fondo para un elemento (seguimos con el ejemplo del negro) y utilizar la propiedad RGBA para los navegadores más avanzados deberíamos establecer también el fondo para Internet Explorer:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">div <span style="color: #00AA00;">&#123;</span> 
   <span style="color: #000000; font-weight: bold;">background</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#000</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* Para Internet Explorer /*
   background: rgba(0,0,0,.5);  /* Para los navegadores que soportan RGBA */</span>
   <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">50px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Si, en Internet Explorer se verá distinto, sin opacidad. Pero si esto no afecta a la usabilidad ni a la accesibilidad del sitio es totalmente válido su uso.</p>
<p>Por último señalar la facilidad que nos da RGBA para el uso de una gama de colores en una web determinada. Supongamos que queremos utilizar distintas tonalidades a partir del mismo verde. Utilizando distintos niveles de opacidad podemos tener variaciones diferentes para utilizar por ejemplo como color de los enlaces y del <em><strong>hover</strong></em> de los enlaces:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">a<span style="color: #3333ff;">:link</span><span style="color: #00AA00;">,</span> a<span style="color: #3333ff;">:visited </span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">178</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">120</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">1</span><span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span>
a<span style="color: #3333ff;">:hover </span><span style="color: #00AA00;">&#123;</span>
<span style="color: #000000; font-weight: bold;">color</span><span style="color: #00AA00;">:</span> rgba<span style="color: #00AA00;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">178</span><span style="color: #00AA00;">,</span><span style="color: #cc66cc;">120</span><span style="color: #00AA00;">,</span>.5<span style="color: #00AA00;">&#41;</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p><img src="http://www.emenia.es/wp-content/uploads/2009/11/rgba3.gif" alt="rgba3" title="rgba3" width="169" height="70" class="alignleft size-full wp-image-367" /><br />
<br class="clear" /></p>
<h3>Otras lecturas recomendadas</h3>
<p>Lea Verou, propone un método para poder utilizar fondos RGBA en Internet Explorer: <a href="http://leaverou.me/2009/02/bulletproof-cross-browser-rgba-backgrounds/" title="Ir al artículo">Bulletproof, cross-browser RGBA backgrounds, today</a><br />
24Ways.com: <a href="http://24ways.org/2009/working-with-rgba-colour" title="Ir al artículo">Working With RGBA Colour</a></p>
<div id="crp_related"><h3>Artículos relacionados:</h3><ul><li><a href="http://www.emenia.es/utilizando-css3-hoy-1-bordes-redondeados/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (1): Bordes Redondeados</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-3-sombras/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (3): Sombras</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-4-border-image/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (4): border-image</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-5-font-face/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (5): @font-face</a></li><li><a href="http://www.emenia.es/internet-explorer-6-y-displayinline/" rel="bookmark" class="crp_title">Internet Explorer 6 y display:inline</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emenia.es/utilizando-css3-hoy-2-colores-y-opacidad-utilizando-rbga/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utilizando CSS3 hoy (1): Bordes Redondeados</title>
		<link>http://www.emenia.es/utilizando-css3-hoy-1-bordes-redondeados/</link>
		<comments>http://www.emenia.es/utilizando-css3-hoy-1-bordes-redondeados/#comments</comments>
		<pubDate>Sun, 22 Nov 2009 08:27:58 +0000</pubDate>
		<dc:creator>Juan Diaz-Bustamante</dc:creator>
				<category><![CDATA[CSS]]></category>

		<guid isPermaLink="false">http://www.emenia.es/?p=327</guid>
		<description><![CDATA[Podemos utilizar ya estilos de CSS3 para los navegadores más avanzados y siempre que no afecte a la usabilidad y accesibilidad de nuestra web. Hoy veremos los bordes redondeados.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.emenia.es%2Futilizando-css3-hoy-1-bordes-redondeados%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.emenia.es%2Futilizando-css3-hoy-1-bordes-redondeados%2F&amp;source=JuanDBB_Emenia&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Acabo de leer el libro de <strong>Dan Cederholm</strong> <a href="http://handcraftedcss.com/" title="Ir a la web del libro">Handcrafted CSS</a>, que me ha parecido muy bueno. En general el libro gira sobre dos ideas:</p>
<p>- Siempre hay que preguntarse cuando se desarrolla una web <em><strong>¿qué pasaría si&#8230;?</strong></em> para estar preparado ante cualquier circunstancia que pueda alterar los distintos componentes de la web y hacerlos distintos a como nosotros los habíamos previsto (por ejemplo incrementando el tamaño del texto). Eso lo llama <em>&#8220;web a prueba de balas&#8221;</em> y &#8220;desarrollo artesano&#8221;, es decir, <strong>con atención al detalle</strong>.</p>
<p>- La idea de que no tiene por qué verse igual nuestra web en todos los navegadores. <strong>Dan Cederholm reivindica que se utilice ya CSS3</strong>, soportado por los navegadores más nuevos (en ocasiones sólo por Safari, Chrome y Firefox, e incluso sólo por Safari), siempre que no afecte a la usabilidad y accesibilidad del sitio cuando se vea con otro navegador que no soporte CSS3 (básicamente Internet Explorer). Es decir, que bajo IE6 se pueda usar y leer perfectamente nuestra web, aunque no sea tan &#8220;bonita&#8221; como cuando se usa un navegador moderno. Pero claro, para tomar una decisión como esta antes hay que conocer qué porcentaje de nuestros usuarios utilizan IE6, por ejemplo, y luego decidir si estamos dispuestos a que vean una web &#8220;más fea&#8221; siendo un porcentaje de usuarios alto los que lo usan&#8230;</p>
<p>Aprovechando esta última idea voy a empezar a hablar sobre las novedades más importantes que, aparte de los <a href="http://librosweb.es/css_avanzado/capitulo3/selectores_de_css_3.html" title="Leer más sobre los selectores CSS3">selectores</a>, aporta CSS3 y que podemos empezar a utilizar ya en nuestros diseños (teniendo en cuenta que no se verán en algunos navegadores). Para no hacer muy largo el artículo voy a ir desgranando cada novedad en artículos diferentes. Hoy empezaré con los bordes redondeados, probablemente la novedad más conocida.</p>
<h3>Bordes Redondeados</h3>
<p>Si hasta ahora teníamos que utilizar imágenes para obtener bordes redondos en nuestros diseños ahora estos se pueden conseguir gracias a CSS3. Para ello aplicamos al elemento al que le queremos redondear los bordes la propiedad <strong><em>border-radius</em></strong>.</p>
<p>Sin embargo esta propiedad aún no está recogida tal cual por los navegadores. Internet Explorer no lo recoge en ninguna de sus versiones (lo han prometido para Internet Explorer 9). Sí está recogida en cambio por Safari y Chrome (utilizando el prefijo<strong>-webkit-</strong>) y por Firefox (utilizando el prefijo <strong>-moz-</strong>). </p>
<p>Vamos a poner un ejemplo. Supongamos que tenemos un DIV  de 100px de ancho y 100px de alto con color de fondo rojo que queremos que tenga unas esquinas redondas con un radio de 10px:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">div<span style="color: #6666ff;">.cuadro</span> <span style="color: #00AA00;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
    <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span>
    -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
    -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>El resultado sería este:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/esquinas.gif" alt="esquinas" title="esquinas" width="114" height="108" class="alignleft size-full wp-image-332" /><br />
<br class="clear" /><br />
Podemos redondear incluso algunas esquinas y otras no, desplegando la propiedad en todas sus esquinas. Por ejemplo, si queremos que sea redonda sólo la esquina superior derecha:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">div<span style="color: #6666ff;">.cuadro</span> <span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span>
   -moz-border-radius-topright<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
   -webkit-border-top-right-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>El resultado sería:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/esquina.gif" alt="esquina" title="esquina" width="120" height="115" class="alignleft size-full wp-image-334" /><br />
<br class="clear" /></p>
<p>En el caso de alterar varias esquinas, pero no todas, podemos abreviar:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">div<span style="color: #6666ff;">.cuadro</span> <span style="color: #00AA00;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">height</span><span style="color: #00AA00;">:</span> <span style="color: #933;">100px</span><span style="color: #00AA00;">;</span>
   <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">red</span><span style="color: #00AA00;">;</span>
   -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #933;">10px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
   -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">10px</span> <span style="color: #933;">10px</span> <span style="color: #933;">0px</span> <span style="color: #933;">0px</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Vemos como hay una ligera diferencia entre la expresión de webkit (Safari y Chrome) y la de Mozilla (Firefox). Aquí tenemos un detalle de todas ellas:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/border.png" alt="border" title="border" width="600" height="570" class="alignnone size-full wp-image-337" /></p>
<p>Yo suelo utilizar border-radius mucho en los formularios, para redondear las casillas del mismo. ¿Que en Internet Explorer se ven cuadradas? No pasa nada, no afecta al formulario. Vamos a verlo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;form action=&quot;#&quot; method=&quot;post&quot;&gt;
  &lt;fieldset&gt;
    &lt;p&gt;&lt;label for=&quot;nombre&quot;&gt;Nombre:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;nombre&quot;  /&gt;&lt;/p&gt;
    &lt;p&gt;&lt;label for=&quot;email&quot;&gt;Email:&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;email&quot; value=&quot;&quot; /&gt;&lt;/p&gt;
    &lt;p&gt;&lt;label for=&quot;comentarios&quot;&gt;Comentarios:&lt;/label&gt;&lt;textarea name=&quot;comentarios&quot;&gt;&lt;/textarea&gt;&lt;/p&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Enviar&quot; class=&quot;boton&quot; /&gt;
  &lt;/fieldset&gt;
&lt;/form&gt;</pre></td></tr></table></div>

<p>Y aplicando estos estilos:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="css" style="font-family:monospace;">p     		<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">padding</span><span style="color: #00AA00;">:</span> <span style="color: #933;">5px</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
label 	<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">block</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span>
input<span style="color: #00AA00;">,</span>
textarea	<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">200px</span><span style="color: #00AA00;">;</span>
		   -webkit-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
		   -moz-border-radius<span style="color: #00AA00;">:</span> <span style="color: #933;">8px</span><span style="color: #00AA00;">;</span>
		  <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">1px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#BDC2C9</span><span style="color: #00AA00;">;</span>
		 <span style="color: #00AA00;">&#125;</span>
input<span style="color: #6666ff;">.boton</span>	<span style="color: #00AA00;">&#123;</span>  <span style="color: #000000; font-weight: bold;">width</span><span style="color: #00AA00;">:</span> <span style="color: #933;">70px</span><span style="color: #00AA00;">;</span><span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>

<p>Quedaría:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/formulario.png" alt="formulario" title="formulario" width="228" height="249" class="alignleft size-full wp-image-340" /><br />
<br class="clear" /></p>
<p>Vemos que hemos redondeado los bordes de los elementos. En internet Explorer 8 se vería así:<br />
<img src="http://www.emenia.es/wp-content/uploads/2009/11/formulario_ie.png" alt="formulario_ie" title="formulario_ie" width="221" height="228" class="alignleft size-full wp-image-349" /><br />
<br class="clear" /></p>
<p>Como se ve todo aparece sin problemas en Internet Explorer. Los bordes no aparecen redondeados, pero no supone ningún problema para el uso del formulario. Lo único que estamos haciendo es ofrecer diseños más avanzados a aquellos usuarios que utilizan navegadores más modernos, pero sin afectar a la usabilidad y accesibilidad de la web.</p>
<p>Seguiremos en breve con otra propiedad de CSS3, los colores utilizando RGBA.</p>
<h3>Lecturas recomendadas</h3>
<p>BuildInternet, <a href="http://buildinternet.com/2009/10/using-rounded-corners-with-css3/">Using Rounded Corners with CSS3</a><br />
Smashing Magazine, <a href="http://www.smashingmagazine.com/2009/07/13/css-3-cheat-sheet-pdf/" title="Ir al artículo">CSS 3 Cheat Sheet (PDF)</a><br />
Elliotswan, <a href="http://www.elliotswan.com/2009/07/27/9-css3-properties-you-can-use-now/" title="Ir al artículo">9 CSS3 properties you can use now</a><br />
CSS3.info, <a href="http://www.css3.info/preview/" title="Ir al artículo">CSS3 Previews</a></p>
<div id="crp_related"><h3>Artículos relacionados:</h3><ul><li><a href="http://www.emenia.es/utilizando-css3-hoy-3-sombras/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (3): Sombras</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-4-border-image/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (4): border-image</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-2-colores-y-opacidad-utilizando-rbga/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (2): Colores y opacidad utilizando RBGA</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-5-font-face/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (5): @font-face</a></li><li><a href="http://www.emenia.es/curso-de-jquery-2-selectores-primera-parte/" rel="bookmark" class="crp_title">Curso de jQuery (2): Selectores, Primera Parte</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emenia.es/utilizando-css3-hoy-1-bordes-redondeados/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Como utilizar un fondo aleatorio para nuestra web</title>
		<link>http://www.emenia.es/como-utilizar-un-fondo-aleatorio-para-nuestra-web/</link>
		<comments>http://www.emenia.es/como-utilizar-un-fondo-aleatorio-para-nuestra-web/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 07:59:42 +0000</pubDate>
		<dc:creator>Juan Diaz-Bustamante</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://emenia.es/?p=159</guid>
		<description><![CDATA[Hay veces que queremos que el fondo de nuestra web sea distinto cada vez que entramos en la misma. Esto lo podemos hacer de una manera muy sencilla con PHP.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.emenia.es%2Fcomo-utilizar-un-fondo-aleatorio-para-nuestra-web%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.emenia.es%2Fcomo-utilizar-un-fondo-aleatorio-para-nuestra-web%2F&amp;source=JuanDBB_Emenia&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Hay ocasiones en las que queremos que cada vez que entre un usuario a nuestra web <strong>el fondo de la misma sea elegido aleatoriamente</strong>. De esta forma podemos sorprender a nuestros usuarios con distintos diseños, distintos mensajes, etc..</p>
<p>Conseguirlo es muy sencillo combinando PHP y CSS.</p>
<p>Supongamos que tenemos una serie de imágenes para el fondo de nuestra web que se llaman: &#8220;fondo_body1.jpg&#8221; , &#8220;fondo_body2.jpg&#8221; , &#8220;fondo_body3.jpg&#8221; , &#8220;fondo_body4.jpg&#8221; , &#8220;fondo_body5.jpg&#8221;, &#8220;fondo_body6.jpg&#8221; , &#8220;fondo_body7.jpg&#8221; , &#8220;fondo_body8.jpg&#8221; , &#8220;fondo_body9.jpg&#8221; , &#8220;fondo_body10.jpg&#8221;,&#8221;fondo_body11.jpg&#8221; , &#8220;fondo_body12.jpg&#8221; , &#8220;fondo_body13.jpg&#8221; , &#8220;fondo_body14.jpg&#8221; y &#8220;fondo_body15.jpg&#8221;</p>
<p>¡Quince nada menos!  De esta manera va a ser dificil que a un usuario le salga dos veces seguidas la misma imagen&#8230;&#8230;</p>
<p>Vamos a ver línea por línea. Como vamos a introducir estilos dentro de <em>Head</em> escribiríamos:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;style type=&quot;text/css&quot; media=&quot;screen&quot;&gt;</pre></td></tr></table></div>

<p>A continuación abrimos php y creamos un número aleatorio utilizando PHP:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$valor</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Uso de rand(): rand(Valor minimo, Valor maximo);</span></pre></td></tr></table></div>

<p>De esta forma creamos un número aleatorio entre cero y catorce y lo almacenamos en la variable $valor. ¿Por qué empezamos por cero? Porque ahora vamos a almacenar los nombres de nuestras imágenes en una <em>array</em> o matriz, y estas comienzan a contar desde cero. Por lo tanto como tenemos quince imágenes contaríamos desde cero a catorce.</p>
<p>Almacenemos nuestras imágenes en el <em>array</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$imagenes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fondo_body1.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body2.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body3.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body4.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body5.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body6.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body7.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body8.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body9.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body10.jpg&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;fondo_body11.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body12.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body13.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body14.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body15.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Como veis hemos creado un array donde hemos almacenado todos los nombres de nuestras imágenes y ese array lo hemos almacenado a su vez en la variable $imagenes.</p>
<p>Ahora vamos a escribir la línea de CSS que va a crear el fondo para nuestra web, es decir, el fondo de<em> body</em> (cerrando a continuación <em>php</em> y <em>style</em>):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;body{ background: #000 url('images/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$imagenes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$valor</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;') no-repeat top right fixed;}&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span> 
<span style="color: #339933;">&lt;/</span>style<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Como estamos aún con PHP la manera que utilizamos en PHP para escribir algo es a través de <em>echo</em>. A continuación sigue la instrucción de CSS que da el fondo a body pero con una peculiaridad: el nombre de la imagen que va a estar ocupando el fondo vendrá dado por las dos variables que hemos creado con anterioridad. Por ejemplo, si el número aleatorio es 3 tendremos <em>$imagenes[3]</em>, es decir, escogerá el tercer valor del <em>array</em> comenzando por cero, que es <em>fondo_body4.jpg</em>. Hay que tener cuidado con las comillas. Añadimos la variable con un punto (que es la manera de añadir con PHP) después de interrumpir con dobles comillas la instrucción normal de CSS para introducir PHP. Si seguimos con nuestro ejemplo el resultado será:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;style type=&quot;text/css&quot; media=&quot;screen&quot;&gt;
body{ background: #000 url('images/fondo_body4.jpg') no-repeat top right fixed;}
&lt;/style&gt;</pre></td></tr></table></div>

<p>Vamos a ver ahora todas las líneas que escribiríamos dentro de <em>head</em>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;style type=&quot;text/css&quot; media=&quot;screen&quot;&gt;
<span style="color: #000000; font-weight: bold;">&lt;?php</span> 
  <span style="color: #000088;">$valor</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Uso de rand(): rand(Valor minimo, Valor maximo);</span>
  <span style="color: #000088;">$imagenes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fondo_body1.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body2.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body3.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body4.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body5.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body6.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body7.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body8.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body9.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body10.jpg&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;fondo_body11.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body12.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body13.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body14.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body15.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;body{ background: #000 url('images/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$imagenes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$valor</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;') no-repeat top right fixed;}&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span> 
&lt;/style&gt;</pre></td></tr></table></div>

<p><strong>Podemos refinar aún más nuestro código</strong>. ¿Qué pasa si de repente en vez de tener 15 imágenes queremos que sean 16? Que tenemos que cambiar el número en <em>rand</em>. ¿No podemos hacer que se cuente de manera automática el número de imágenes que hay en el <em>array</em>? Pues sí se puede. Lo haríamos así:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$imagenes</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>En nuestro caso nos dará un valor de 15, ya que hay 15 imágenes en el <em>array</em>. Para llevar esto a la función que crea el número aleatorio haríamos:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"> <span style="color: #000088;">$valor</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$imagenes</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Uso de rand(): rand(Valor minimo, Valor maximo);</span></pre></td></tr></table></div>

<p>Vemos que a la función que nos cuenta el número de imágenes del array le hemos restado uno ¿Por qué? Recordad que se comienza siempre contando desde cero. Si hay una sola imagen <em>$valor</em> tendría que ser cero (un número aleatorio entre cero y cero, saldría cero, que es la primera imagen).</p>
<p>Ahora quedaría todo así:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;style type=&quot;text/css&quot; media=&quot;screen&quot;&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span> 
	<span style="color: #000088;">$imagenes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;fondo_body1.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body2.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body3.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body4.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body5.jpg&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body6.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body7.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body8.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body9.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body10.jpg&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;fondo_body11.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body12.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body13.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body14.jpg&quot;</span> <span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;fondo_body15.jpg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
       <span style="color: #000088;">$valor</span> <span style="color: #339933;">=</span> <span style="color: #990000;">rand</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$imagenes</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Uso de rand(): rand(Valor minimo, Valor maximo);</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;body{ background: #000 url('images/&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$imagenes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$valor</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;') no-repeat top right fixed;}&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span> 
&lt;/style&gt;</pre></td></tr></table></div>

<p>Fijaros en la instrucción que he incluído en body. Por supuesto que podéis cambiar como queráis lo que he puesto yo (fondo negro -#000-, situado en la parte superior de la pantalla -top- en la parte derecha -right- y que no se desplace cuando nos desplazamos hacia abajo en la página -fixed-)</p>
<h3>Más lecturas recomendadas</h3>
<p>PHP.net &#8211; <a href="http://php.net/manual/en/language.types.array.php">Arrays in PHP</a><br />
Desarrolloweb.com &#8211; <a href="http://www.desarrolloweb.com/articulos/310.php">Creación de tablas por medio de variables tipo array. Utilidad y funciones útiles relacionadas.</a></p>
<div id="crp_related"><h3>Artículos relacionados:</h3><ul><li><a href="http://www.emenia.es/utilizando-css3-hoy-2-colores-y-opacidad-utilizando-rbga/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (2): Colores y opacidad utilizando RBGA</a></li><li><a href="http://www.emenia.es/como-pre-cargar-imagenes-con-jquery/" rel="bookmark" class="crp_title">Cómo pre-cargar imágenes con jQuery</a></li><li><a href="http://www.emenia.es/curso-de-jquery-introduccion-1/" rel="bookmark" class="crp_title">Curso de jQuery (1): Introducción</a></li><li><a href="http://www.emenia.es/curso-de-jquery-3-selectores-segunda-parte/" rel="bookmark" class="crp_title">Curso de jQuery (3): Selectores, Segunda Parte</a></li><li><a href="http://www.emenia.es/utilizando-css3-hoy-3-sombras/" rel="bookmark" class="crp_title">Utilizando CSS3 hoy (3): Sombras</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.emenia.es/como-utilizar-un-fondo-aleatorio-para-nuestra-web/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
