|
| 1 | +--- |
| 2 | +title: Colores, fondos y tipografía |
| 3 | +description: "" |
| 4 | +pubDate: '10/03/2024' |
| 5 | +collection: css |
| 6 | +--- |
| 7 | + |
| 8 | +El color, el fondo y la tipografía son elementos clave del diseño web. |
| 9 | +CSS permite controlar su apariencia con precisión, logrando estilos legibles, coherentes y visualmente atractivos. |
| 10 | + |
| 11 | +## Colores en CSS |
| 12 | + |
| 13 | +Los colores se pueden definir de distintas maneras según la necesidad: |
| 14 | + |
| 15 | +- **Nombres predefinidos** |
| 16 | + ```css |
| 17 | + p { |
| 18 | + color: red; |
| 19 | + } |
| 20 | + ``` |
| 21 | + |
| 22 | +- **Valores hexadecimales** |
| 23 | + ```css |
| 24 | + p { |
| 25 | + color: #ff0000; |
| 26 | + } |
| 27 | + ``` |
| 28 | + |
| 29 | +- **RGB y RGBA** |
| 30 | + ```css |
| 31 | + p { |
| 32 | + color: rgb(255, 0, 0); /* rojo puro */ |
| 33 | + color: rgba(255, 0, 0, 0.5); /* con transparencia */ |
| 34 | + } |
| 35 | + ``` |
| 36 | + |
| 37 | +- **HSL y HSLA** |
| 38 | + ```css |
| 39 | + p { |
| 40 | + color: hsl(120, 50%, 50%); |
| 41 | + color: hsla(120, 50%, 50%, 0.7); |
| 42 | + } |
| 43 | + ``` |
| 44 | + |
| 45 | +- `hsl(hue, saturation, lightness)` permite manipular el tono, saturación y luminosidad. |
| 46 | +- `a` (alpha) controla la opacidad, de 0 a 1. |
| 47 | + |
| 48 | +- **Variables de color (custom properties)** |
| 49 | + ```css |
| 50 | + :root { |
| 51 | + --color-primario: #007acc; |
| 52 | + } |
| 53 | + |
| 54 | + h1 { |
| 55 | + color: var(--color-primario); |
| 56 | + } |
| 57 | + ``` |
| 58 | + |
| 59 | +Las variables permiten mantener consistencia de color en todo el sitio. |
| 60 | + |
| 61 | +## Fondos (background) |
| 62 | + |
| 63 | +Los fondos pueden ser colores planos, degradados o imágenes. |
| 64 | + |
| 65 | +- **Color de fondo** |
| 66 | + ```css |
| 67 | + div { |
| 68 | + background-color: lightgray; |
| 69 | + } |
| 70 | + ``` |
| 71 | + |
| 72 | +- **Imagen de fondo** |
| 73 | + ```css |
| 74 | + body { |
| 75 | + background-image: url("fondo.jpg"); |
| 76 | + background-repeat: no-repeat; |
| 77 | + background-size: cover; |
| 78 | + background-position: center; |
| 79 | + } |
| 80 | + ``` |
| 81 | + |
| 82 | +- **Degradado como fondo** |
| 83 | + ```css |
| 84 | + section { |
| 85 | + background: linear-gradient(to right, #4a90e2, #50e3c2); |
| 86 | + } |
| 87 | + ``` |
| 88 | + |
| 89 | +- **Propiedades útiles de background** |
| 90 | + - `background-color`: define el color base. |
| 91 | + - `background-image`: establece una imagen o degradado. |
| 92 | + - `background-repeat`: controla la repetición (`repeat`, `no-repeat`, `repeat-x`, `repeat-y`). |
| 93 | + - `background-position`: define la posición inicial (`center`, `top`, `bottom`, valores en px o %). |
| 94 | + - `background-size`: ajusta la imagen (`cover`, `contain` o medidas personalizadas). |
| 95 | + - `background-attachment`: controla el desplazamiento (`scroll`, `fixed`, `local`). |
| 96 | + |
| 97 | +- **Ejemplo completo** |
| 98 | + ```css |
| 99 | + header { |
| 100 | + background: url("cabecera.jpg") no-repeat center center / cover; |
| 101 | + color: white; |
| 102 | + height: 300px; |
| 103 | + text-align: center; |
| 104 | + } |
| 105 | + ``` |
| 106 | + |
| 107 | +## Tipografía y fuentes |
| 108 | + |
| 109 | +La tipografía define la personalidad del sitio. |
| 110 | +CSS permite controlar la familia de fuentes, el tamaño, el peso, la altura de línea, el espaciado y más. |
| 111 | + |
| 112 | +- **font-family** |
| 113 | + ```css |
| 114 | + body { |
| 115 | + font-family: 'Open Sans', Arial, sans-serif; |
| 116 | + } |
| 117 | + ``` |
| 118 | + |
| 119 | + Incluye siempre fuentes de respaldo (fallbacks) para garantizar compatibilidad. |
| 120 | + |
| 121 | +- **font-size** |
| 122 | + ```css |
| 123 | + h1 { |
| 124 | + font-size: 2rem; |
| 125 | + } |
| 126 | + ``` |
| 127 | + |
| 128 | + Usa unidades relativas (`em`, `rem`, `%`) para adaptabilidad. |
| 129 | + |
| 130 | +- **font-weight** |
| 131 | + ```css |
| 132 | + strong { |
| 133 | + font-weight: bold; |
| 134 | + } |
| 135 | + ``` |
| 136 | + |
| 137 | + Valores: `normal`, `bold`, `lighter`, o numéricos (`100` a `900`). |
| 138 | + |
| 139 | +- **font-style** |
| 140 | + ```css |
| 141 | + em { |
| 142 | + font-style: italic; |
| 143 | + } |
| 144 | + ``` |
| 145 | + |
| 146 | +- **line-height** |
| 147 | + ```css |
| 148 | + p { |
| 149 | + line-height: 1.6; |
| 150 | + } |
| 151 | + ``` |
| 152 | + |
| 153 | + Controla el espaciado vertical entre líneas de texto. |
| 154 | + |
| 155 | +- **letter-spacing y word-spacing** |
| 156 | + ```css |
| 157 | + h2 { |
| 158 | + letter-spacing: 2px; |
| 159 | + word-spacing: 5px; |
| 160 | + } |
| 161 | + ``` |
| 162 | + |
| 163 | + Ajustan el espacio entre letras o palabras. |
| 164 | + |
| 165 | +## Cargar fuentes personalizadas |
| 166 | + |
| 167 | +Puedes importar fuentes externas mediante Google Fonts o archivos locales. |
| 168 | + |
| 169 | +**Ejemplo con Google Fonts** |
| 170 | +```css |
| 171 | +@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap'); |
| 172 | + |
| 173 | +body { |
| 174 | + font-family: 'Roboto', sans-serif; |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +**Ejemplo con archivo local** |
| 179 | +```css |
| 180 | +@font-face { |
| 181 | + font-family: 'MiFuente'; |
| 182 | + src: url('fonts/MiFuente.woff2') format('woff2'); |
| 183 | +} |
| 184 | + |
| 185 | +h1 { |
| 186 | + font-family: 'MiFuente', sans-serif; |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +## Transformaciones y decoración de texto |
| 191 | + |
| 192 | +- **text-transform** |
| 193 | + ```css |
| 194 | + h1 { |
| 195 | + text-transform: uppercase; |
| 196 | + } |
| 197 | + ``` |
| 198 | + |
| 199 | + Convierte el texto en mayúsculas, minúsculas o capitaliza la primera letra de cada palabra. |
| 200 | + |
| 201 | +- **text-decoration** |
| 202 | + ```css |
| 203 | + a { |
| 204 | + text-decoration: none; |
| 205 | + } |
| 206 | + ``` |
| 207 | + |
| 208 | + Controla subrayados, tachados, etc. |
| 209 | + Valores: `underline`, `line-through`, `overline`, `none`. |
| 210 | + |
| 211 | +- **text-shadow** |
| 212 | + ```css |
| 213 | + h1 { |
| 214 | + text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3); |
| 215 | + } |
| 216 | + ``` |
| 217 | + |
| 218 | + Agrega una sombra al texto. |
| 219 | + |
| 220 | +- **color de fondo detrás del texto** |
| 221 | + ```css |
| 222 | + mark { |
| 223 | + background-color: yellow; |
| 224 | + } |
| 225 | + ``` |
| 226 | + |
| 227 | +## Buenas prácticas |
| 228 | + |
| 229 | +- Usa colores con suficiente contraste para la accesibilidad (ver WCAG). |
| 230 | +- Define una paleta coherente de colores primarios y secundarios. |
| 231 | +- Usa variables CSS (`--color-...`) para mantener consistencia visual. |
| 232 | +- Evita fuentes decorativas excesivas: máximo 2 o 3 familias tipográficas por sitio. |
| 233 | +- Usa unidades relativas para tamaños de texto y evita píxeles fijos. |
| 234 | +- Comprueba la legibilidad en diferentes dispositivos. |
0 commit comments