Propiedad de ajuste de objeto CSS
Ejemplo
Recorte los lados de una imagen, conservando la relación de aspecto, y rellene el espacio:
img.a {
width: 200px;
height:
400px;
object-fit: cover;
}
Definición y uso
La object-fit
propiedad se usa para especificar cómo se debe cambiar el tamaño de un <img> o <video> para que se ajuste a su contenedor.
Esta propiedad le dice al contenido que llene el contenedor de varias maneras; como "preservar esa relación de aspecto" o "estirar y ocupar tanto espacio como sea posible".
Valor por defecto: | ver propiedades individuales |
---|---|
Heredado: | No |
Animable: | No. Leer sobre animable |
Versión: | CSS3 |
Sintaxis de JavaScript: | objeto .style.objectFit="cover" |
Compatibilidad con navegador
Los números de la tabla especifican la primera versión del navegador que es totalmente compatible con la propiedad.
Property | |||||
---|---|---|---|---|---|
object-fit | 31.0 | 16.0 | 36.0 | 7.1 | 19.0 |
Sintaxis CSS
object-fit: fill|contain|cover|scale-down|none|initial|inherit;
Valores de propiedad
Value | Description | Try it |
---|---|---|
fill | This is default. The replaced content is sized to fill the element's content box. If necessary, the object will be stretched or squished to fit | |
contain | The replaced content is scaled to maintain its aspect ratio while fitting within the element's content box | |
cover | The replaced content is sized to maintain its aspect ratio while filling the element's entire content box. The object will be clipped to fit | |
none | The replaced content is not resized | |
scale-down | The content is sized as if none or contain were specified (would result in a smaller concrete object size) | |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
páginas relacionadas
Tutorial de CSS: ajuste de objetos CSS
Referencia CSS: posición del objeto CSS
Referencia HTML DOM: la propiedad objectFit