11.1 Sass (Syntactically Awesome Stylesheets)
Pré-processadores para CSS — são ferramentas que ampliam as capacidades do CSS e simplificam o processo de escrita dos estilos. Eles adicionam funções como variáveis, regras aninhadas, mixins e funções, tornando o código mais modular, reutilizável e fácil de manter. Hoje a gente vai dar uma olhada nos pré-processadores de CSS mais populares: Sass, LESS e Stylus.
Sass — é um dos pré-processadores de CSS mais populares, que adiciona um monte de recursos legais para estilização.
Instalação do Sass
Você pode instalar o Sass usando npm:
npm install -g sass
Principais funcionalidades do Sass
1. Variáveis
As variáveis no Sass permitem armazenar valores que podem ser reutilizados:
<html>
<head>
<style>
body {
color: #333;
}
</style>
</head>
<body>
<p>Parágrafo 1</p>
<p>Parágrafo 2</p>
</body>
</html>
$primary-color: #333
body
color: $primary-color
2. Regras aninhadas
Regras aninhadas permitem escrever CSS de maneira mais hierá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
Mixins permitem criar grupos de estilos que podem ser incluídos em outras regras:
<html>
<head>
<style>
.box {
width: 256px;
min-height: 200px;
border-radius: 10px;
border: 2px solid green;
}
</style>
</head>
<body>
<div class="box">Conteúdo</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. Herança
Herança permite que um seletor herde os estilos de outro:
<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">Alguma informação</div>
<div class="box success">Sucesso</div>
<div class="box warning">Atenção!</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. Funções
<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);
Compilação do Sass
A compilação do Sass em CSS é feita com o comando:
sass style.scss style.css
11.2 LESS
Instalação do LESS
LESS pode ser instalado via npm:
npm install -g less
Principais funcionalidades do LESS
1. Variáveis:
<html>
<head>
<style>
body {
color: #333;
}
</style>
</head>
<body>
<p>Parágrafo 1</p>
<p>Parágrafo 2</p>
</body>
</html>
@primary-color: #333;
body {
color: @primary-color;
}
2. Regras aninhadas:
<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">Conteúdo</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. Herança:
LESS usa o símbolo "&" para referenciar o pai.
<html>
<head>
<style>
button:hover {
background-color: #555;
}
</style>
</head>
<body>
<button>Botão</button>
</body>
</html>
.button {
&:hover {
background-color: #555;
}
}
5. Funções:
LESS suporta funções para trabalhar com cores, números e strings.
<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);
}
Compilação do LESS
A compilação do LESS em CSS é feita com o comando:
lessc style.less style.css
11.3 Stylus
Stylus — é um pré-processador de CSS flexível e poderoso, que oferece uma sintaxe concisa e várias funções úteis.
Instalação do Stylus
Stylus pode ser instalado via npm:
npm install -g stylus
Principais funcionalidades do Stylus
1. Variáveis:
<html>
<head>
<style>
body {
color: #333;
}
</style>
</head>
<body>
<p>Parágrafo 1</p>
<p>Parágrafo 2</p>
</body>
</html>
primary-color = #333
body
color primary-color
2. Regras aninhadas:
<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">Conteúdo</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. Herança:
Stylus usa @extend
para herança.
<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">Alguma informação</div>
<div class="box success">Sucesso</div>
<div class="box warning">Atenção!</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. Funções:
Stylus permite criar funções para reutilização.
<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)
Compilação do Stylus
A compilação do Stylus em CSS é feita com o comando:
stylus style.styl
GO TO FULL VERSION