Saltar al contenido

Propiedad background-clip de CSS

La propiedad CSS background-clip define el área a la que se pintan el background-color y el background-image. Si el elemento no tiene background-color o background-image, esta propiedad no tiene efecto visual.

La propiedad background-clip es una de las propiedades CSS3.

Para recortar un fondo al texto, también está disponible una versión con prefijo de proveedor (-webkit-background-clip).

Valor inicialborder-box
Se aplica aTodos los elementos. También se aplica a ::first-letter y ::first-line.
HeredableNo.
AnimableNo.
VersiónCSS3
Sintaxis DOMobject.style.backgroundClip = "content-box";

Sintaxis

Sintaxis de la propiedad CSS background-clip

css
background-clip: border-box | padding-box | content-box | text | initial | inherit;

Ejemplo de la propiedad background-clip:

Ejemplo de la propiedad CSS background-clip con el valor content-box

html
<!DOCTYPE html>
<html>
  <head>
    <title>The title of the document</title>
    <style>
      #example {
        border: 3px solid #666;
        padding: 15px;
        background: #ccc;
        background-clip: content-box;
      }
    </style>
  </head>
  <body>
    <h2>Background-clip property example</h2>
    <p>Here the "content-box" value is used.</p>
    <div id="example">
      <p>The background extends to the edge of the content box.</p>
    </div>
  </body>
</html>

En el siguiente ejemplo, observe la diferencia entre los valores "padding-box" y "border-box".

Ejemplo de la propiedad background-clip con los valores "padding-box" y "border-box":

Ejemplo de la propiedad CSS background-clip con el valor border-box

html
<!DOCTYPE html>
<html>
  <head>
    <title>The title of the document</title>
    <style>
      #example1 {
        border: 5px dashed #666;
        padding: 15px;
        background: #1c87c9;
        background-clip: border-box;
      }
      #example2 {
        border: 5px dashed #666;
        padding: 15px;
        background: #1c87c9;
        background-clip: padding-box;
      }
    </style>
  </head>
  <body>
    <h2>Background-clip property example</h2>
    <p>Here the background-clip is set to "border-box" (this is the default value).</p>
    <div id="example1">
      <p>The background extends behind the border.</p>
    </div>
    <p>Here the background-clip is set to "padding-box".</p>
    <div id="example2">
      <p>The background extends to the inside edge of the border.</p>
    </div>
  </body>
</html>

En este ejemplo, el fondo se pinta dentro del texto en primer plano.

Propiedad CSS background-clip

Ejemplo de la propiedad background-clip con el valor "text":

Ejemplo de la propiedad CSS background-clip con el valor text

html
<!DOCTYPE html>
<html>
  <head>
    <title>The title of the document</title>
    <style>
      #example {
        border: 3px dashed #1ebf42;
        padding: 15px;
        background: #1c87c9;
        background-clip: text;
        -webkit-background-clip: text;
        color: transparent;
      }
    </style>
  </head>
  <body>
    <h2>Background-clip property example</h2>
    <p>Here the background-clip is set to "text"</p>
    <div id="example">
      <p>The background is painted within the foreground text.</p>
    </div>
  </body>
</html>

Valores

ValorDescripciónProbar
border-boxEl fondo aparece detrás del borde. Este es el valor predeterminado.Probar »
padding-boxEl fondo aparece dentro del borde.Probar »
content-boxEl fondo se extiende hasta el borde del cuadro de contenido.Probar »
textEl fondo se pinta dentro del texto en primer plano.Probar »
initialEstablece la propiedad en su valor predeterminado.Probar »
inheritHereda la propiedad de su elemento principal.

Práctica

¿Cuál de los siguientes valores se puede usar con la propiedad 'background-clip' en CSS?

¿Te resulta útil?

Vista previa dual-run — compárala con las rutas Symfony en producción.