CodeGym /Cursos /Frontend SELF ES /Preprocesadores para CSS

Preprocesadores para CSS

Frontend SELF ES
Nivel 32 , Lección 5
Disponible

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:

Terminal
    
      npm install -g sass
    
  

Funciones principales de Sass

1. Variables

Las variables en Sass permiten almacenar valores que pueden ser reutilizados:

HTML
    
      <html>
        <head>
          <style>
            body {
              color: #333;
            }
          </style>
        </head>
        <body>
          <p>Párrafo 1</p>
          <p>Párrafo 2</p>
        </body>
      </html>
    
  
Sass
    
      $primary-color: #333

      body
        color: $primary-color
    
  

2. Reglas anidadas

Las reglas anidadas permiten escribir CSS de una manera más jerárquica:

HTML
    
      <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>
    
  
Sass
    
      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
    
      <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>
    
  
Sass
    
      @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
    
      <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>
    
  
Sass
    
      %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
    
      <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>
    
  
Sass
    
      @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:

Terminal
    
      sass style.scss style.css
    
  

11.2 LESS

Instalación de LESS

LESS se puede instalar a través de npm:

Terminal
    
      npm install -g less
    
  

Principales funciones de LESS

1. Variables:

HTML
    
      <html>
        <head>
          <style>
            body {
              color: #333;
            }
          </style>
        </head>
        <body>
          <p>Párrafo 1</p>
          <p>Párrafo 2</p>
        </body>
      </html>
    
  
LESS
    
      @primary-color: #333;

      body {
        color: @primary-color;
      }
    
  

2. Reglas anidadas:

HTML
    
      <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>
    
  
LESS
    
      nav {
        background: #eee;

        ul {
          margin: 0;
          padding: 0;
          list-style: none;

          li {
            color: tomato;
          }
        }
      }
    
  
HTML
    
      <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:

LESS
    
      .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
    
      <html>
        <head>
          <style>
            button:hover {
              background-color: #555;
            }
          </style>
        </head>
        <body>
          <button>Botón</button>
        </body>
      </html>
    
  
LESS
    
      .button {
        &:hover {
          background-color: #555;
        }
      }
    
  

5. Funciones:

LESS soporta funciones para trabajar con colores, números y cadenas.

HTML
    
      <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>
    
  
LESS
    
      @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:

Terminal
    
      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:

Terminal
    
      npm install -g stylus
    
  

Principales funciones de Stylus

1. Variables:

HTML
    
      <html>
        <head>
          <style>
            body {
              color: #333;
            }
          </style>
        </head>
        <body>
          <p>Párrafo 1</p>
          <p>Párrafo 2</p>
        </body>
      </html>
    
  
Stylus
    
      primary-color = #333

      body
        color primary-color
    
  

2. Reglas anidadas:

HTML
    
      <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>
    
  
Stylus
    
      nav
      background #eee

      ul
        margin 0
        padding 0
        list-style none

        li
          color tomato
    
  

3. Mixins:

HTML
    
      <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>
    
  
Stylus
    
      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
    
      <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>
    
  
Stylus
    
      .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
    
      <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>
    
  
Stylus
    
      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:

Terminal
    
      stylus style.styl
    
  
1
Cuestionario/control
Funciones CSS, nivel 32, lección 5
No disponible
Funciones CSS
Funciones CSS
Comentarios
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION