11.1 Sass (Syntactically Awesome Stylesheets)
Los preprocesadores de CSS son herramientas que permiten ampliar las posibilidades de CSS y simplificar el proceso de escritura de estilos. Añaden funcionalidades como variables, reglas anidadas, mixins y funciones, lo que hace que el código sea más modular, reutilizable y fácil de mantener. Hoy veremos los preprocesadores de CSS más populares: Sass, LESS y Stylus.
Sass es uno de los preprocesadores de CSS más populares, que añade un montón de funcionalidades útiles para la estilización.
Instalación de Sass
Sass se puede instalar a través de npm:
npm install -g sass
Funciones principales de Sass
1. Variables
Las variables en Sass permiten almacenar valores que pueden ser reutilizados:
<html>
<head>
<style>
body {
color: #333;
}
</style>
</head>
<body>
<p>Párrafo 1</p>
<p>Párrafo 2</p>
</body>
</html>
$primary-color: #333
body
color: $primary-color
2. Reglas anidadas
Las reglas anidadas permiten escribir CSS de una manera más jerárquica:
<html>
<head>
<style>
nav {
background: #eee;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
color: tomato;
}
</style>
</head>
<body>
<nav>
<ul>
<li>Elemento 1</li>
<li>Elemento 2</li>
<li>Elemento 3</li>
</ul>
</nav>
</body>
</html>
nav
background: #eee
ul
margin: 0
padding: 0
list-style: none
li
color: tomato
3. Mixins
Los mixins permiten crear grupos de estilos que se pueden incluir en otras reglas:
<html>
<head>
<style>
.box {
width: 256px;
min-height: 200px;
border-radius: 10px;
border: 2px solid green;
}
</style>
</head>
<body>
<div class="box">Contenido</div>
</body>
</html>
@mixin border-radius($radius)
-webkit-border-radius: $radius
-moz-border-radius: $radius
border-radius: $radius
.box
@include border-radius(10px)
border: 2px solid green
width: 256px
min-height: 200px
4. Herencia
La herencia permite que un selector herede estilos de otro:
<html>
<head>
<style>
.box {
min-height: 120px;
}
.box:not(:last-child) {
margin-bottom: 5px;
}
.info, .success, .warning {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success {
background-color: #cfc;
}
.warning {
background-color: #fc9
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box info">Algo de info</div>
<div class="box success">Éxito</div>
<div class="box warning">¡Advertencia!</div>
</div>
</body>
</html>
%message
border: 1px solid #ccc
padding: 10px
color: #333
.box
min-height: 120px
&:not(:last-child)
margin-bottom: 5px
.info
@extend %message
.success
@extend %message
background-color: #cfc
.warning
@extend %message
background-color: #fc9
5. Funciones
<html>
<head>
<style>
body {
font-size: 1.125rem;
}
</style>
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad cupiditate esse libero veritatis voluptatem?
Aliquam atque laboriosam porro recusandae totam.
</p>
</body>
</html>
@function px-to-rem($px, $base-font-size: 16px)
@return ($px / $base-font-size) * 1rem;
body
font-size: px-to-rem(18px);
Compilación de Sass
La compilación de Sass a CSS se realiza con el comando:
sass style.scss style.css
11.2 LESS
Instalación de LESS
LESS se puede instalar a través de npm:
npm install -g less
Principales funciones de LESS
1. Variables:
<html>
<head>
<style>
body {
color: #333;
}
</style>
</head>
<body>
<p>Párrafo 1</p>
<p>Párrafo 2</p>
</body>
</html>
@primary-color: #333;
body {
color: @primary-color;
}
2. Reglas anidadas:
<html>
<head>
<style>
nav {
background: #eee;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
color: tomato;
}
</style>
</head>
<body>
<nav>
<ul>
<li>Elemento 1</li>
<li>Elemento 2</li>
<li>Elemento 3</li>
</ul>
</nav>
</body>
</html>
nav {
background: #eee;
ul {
margin: 0;
padding: 0;
list-style: none;
li {
color: tomato;
}
}
}
<html>
<head>
<style>
.box {
width: 256px;
min-height: 200px;
border-radius: 10px;
border: 2px solid green;
}
</style>
</head>
<body>
<div class="box">Contenido</div>
</body>
</html>
3. Mixins:
.border-radius(@radius) {
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
border-radius: @radius;
}
.box {
.border-radius(10px);
border: 2px solid green;
width: 256px;
min-height: 200px;
}
4. Herencia:
LESS utiliza el símbolo "&" para referirse al padre.
<html>
<head>
<style>
button:hover {
background-color: #555;
}
</style>
</head>
<body>
<button>Botón</button>
</body>
</html>
.button {
&:hover {
background-color: #555;
}
}
5. Funciones:
LESS soporta funciones para trabajar con colores, números y cadenas.
<html>
<head>
<style>
body {
font-size: 1.125rem;
}
</style>
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad cupiditate esse libero veritatis voluptatem?
Aliquam atque laboriosam porro recusandae totam.
</p>
</body>
</html>
@base: 16px;
.font-size(@size) {
font-size: (@size / @base) * 1rem;
}
body {
.font-size(18px);
}
Compilación de LESS
La compilación de LESS a CSS se realiza con el comando:
lessc style.less style.css
11.3 Stylus
Stylus es un preprocesador de CSS flexible y potente, que ofrece una sintaxis concisa y un montón de funciones útiles.
Instalación de Stylus
Stylus se puede instalar a través de npm:
npm install -g stylus
Principales funciones de Stylus
1. Variables:
<html>
<head>
<style>
body {
color: #333;
}
</style>
</head>
<body>
<p>Párrafo 1</p>
<p>Párrafo 2</p>
</body>
</html>
primary-color = #333
body
color primary-color
2. Reglas anidadas:
<html>
<head>
<style>
nav {
background: #eee;
}
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
color: tomato;
}
</style>
</head>
<body>
<nav>
<ul>
<li>Elemento 1</li>
<li>Elemento 2</li>
<li>Elemento 3</li>
</ul>
</nav>
</body>
</html>
nav
background #eee
ul
margin 0
padding 0
list-style none
li
color tomato
3. Mixins:
<html>
<head>
<style>
.box {
width: 256px;
min-height: 200px;
border-radius: 10px;
border: 2px solid green;
}
</style>
</head>
<body>
<div class="box">Contenido</div>
</body>
</html>
border-radius(radius)
-webkit-border-radius radius
-moz-border-radius radius
border-radius radius
.box
border-radius(10px)
border 2px solid green
width 256px
min-height 200px
4. Herencia:
Stylus utiliza @extend
para herencia.
<html>
<head>
<style>
.box {
min-height: 120px;
}
.box:not(:last-child) {
margin-bottom: 5px;
}
.info, .success, .warning {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success {
background-color: #cfc;
}
.warning {
background-color: #fc9
}
</style>
</head>
<body>
<div class="wrapper">
<div class="box info">Algo de info</div>
<div class="box success">Éxito</div>
<div class="box warning">¡Advertencia!</div>
</div>
</body>
</html>
.message
border 1px solid #ccc
padding 10px
color #333
.info
@extend .message
.success
@extend .message
background-color #cfc
.warning
@extend .message
background-color #fc9
5. Funciones:
Stylus te permite crear funciones para reutilizar.
<html>
<head>
<style>
body {
font-size: 1.125rem;
}
</style>
</head>
<body>
<p>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad cupiditate esse libero veritatis voluptatem?
Aliquam atque laboriosam porro recusandae totam.
</p>
</body>
</html>
px-to-rem(px, base-font-size = 16px)
px / base-font-size * 1rem
body
font-size px-to-rem(18px)
Compilación de Stylus
La compilación de Stylus a CSS se realiza con el comando:
stylus style.styl
GO TO FULL VERSION