-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
index.md
118 lines (84 loc) · 3.83 KB
/
index.md
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
---
title: calc
slug: Web/CSS/calc
---
{{CSSRef}}{{SeeCompatTable}}
## Sumario
La función CSS `calc()` puede ser usada en cualquier sitio donde {{cssxref("<length>")}}, {{cssxref("<frequency>")}}, {{cssxref("<angle>")}}, {{cssxref("<time>")}}, {{cssxref("<number>")}}, o {{cssxref("<integer>")}} sea requerido. `Con calc()` puedes realizar cálculos para determinar valores de propiedades CSS.
Es posible anidar llamadas a `calc()` dentro de otras llamadas `calc()`.
## Sintaxis
```
calc(expresión)
```
### Values
- expresión
- : Una expresión matemática cuyo resultado es usado como valor para la propiedad sobre la cual se aplica.
### Expresiones
La expresión puede ser una combinación de los siguientes operadores:
- **+**
- : Suma
- **-**
- : Resta
- **\***
- : Multiplicación. Al menos uno de los argumentos debe ser un {{cssxref("<number>")}}.
- **/**
- : División. El divisor debe ser un {{cssxref("<number>")}}.
Los operandos en la expresión pueden ser valores tanto positivos como negativos. Puedes usar diferentes unidades para cada valor si lo deseas. Es recomendable el uso de paréntesis para añadir legibilidad a la expresión o para forzar precedencia en las operaciones en caso necesario.
> [!NOTE]
> La división por cero dará lugar a un error generado por el analizador de HTML del navegador.
> [!NOTE]
> Las operaciones + y - siempre deben estar separadas de sus operandos mediante espacios en blanco. La expresión `calc(50% -8px)` será tomada como un operando de porcentaje seguido de otro operando de signo negativo (una expresión inválida, dado que no hay operador en medio), mientras que la expresión `calc(50% - 8px)` es un porcentaje seguido de una operación de resta.
> Los operadores `* y` `/` no requieren espacio en blanco, pero es recomendable añadirlo por consistencia.
## Ejemplos
### Añadir un margen a un objeto en pantalla
`calc()` hace más fácil añadir márgenes a un objeto en determinadas circunstancias. En este ejemplo, CSS crea un espacio horizontal de color amarillo que llena el ancho de la ventana con un hueco de 40 pixels en ambos lados:
```css
.banner {
position: absolute;
left: 40px;
width: 90%; /* salvaguarda para navegadores que no reconocen calc() */
width: calc(100% - 80px);
border: solid black 1px;
box-shadow: 1px 2px;
background-color: yellow;
padding: 6px;
text-align: center;
}
```
```html
<div class="banner">This is a banner!</div>
```
{{ EmbedLiveSample('Positioning_an_object_on_screen_with_a_margin', '100%', '60') }}
### Entradas de formulario que se ajustan automáticamente al ancho de su contenedor
Otro caso de uso para `calc()` es asegurar que los campos de un formulario llenan el espacio disponible sin pasarse de los límites de su contenedor mientras mantienen el margen apropiado.
Echémosle un vistazo al CSS:
```css
input {
padding: 2px;
display: block;
width: 98%; /* salvaguarda para navegadores que no reconocen calc() */
width: calc(100% - 1em);
}
#formbox {
width: 130px; /* salvaguarda para navegadores que no reconocen calc() */
width: calc(100% / 6);
border: 1px solid black;
padding: 4px;
}
```
El formulario usa 1/6 del ancho disponible. Para asegurar que los campos de entrada tienen el tamaño adecuado, usamos `calc()` de nuevo para establecer el ancho que deberían tener (el de su contenedor) menos 1em. Para probar esto, usaremos el siguiente HTML:
```html
<form>
<div id="formbox">
<label>Type something:</label>
<input type="text" />
</div>
</form>
```
{{ EmbedLiveSample('Automatically_sizing_form_fields_to_fit_their_container', '100%', '80') }}
## Especificaciones
{{Specifications}}
## Compatibilidad con navegadores
{{Compat}}
## Ver también
- [Firefox 4: CSS3 calc() ✩ Mozilla Hacks – the Web developer blog](https://hacks.mozilla.org/2010/06/css3-calc/)