Capa de color semitransparente sobre la imagen de fondo?


Tengo un DIV y me gustaría poner un patrón como fondo. Este patrón es gris. Así que para que sea un poco más agradable, me gustaría poner una "capa" de color transparente claro. A continuación se muestra lo que intenté, pero que no funcionó. ¿Hay alguna manera de poner la capa de color sobre la imagen de fondo?

Aquí está mi CSS:

background: url('../img/bg/diagonalnoise.png');
background-color: rgba(248, 247, 216, 0.7);
Author: Duncan Jones, 2012-02-07

11 answers

Aquí está:

.background {
    background:url('../img/bg/diagonalnoise.png');
    position: relative;
}

.layer {
    background-color: rgba(248, 247, 216, 0.7);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

HTML para esto:

<div class="background">
    <div class="layer">
    </div>
</div>

Por supuesto, debe definir un ancho y un alto para la clase .background, si no hay otros elementos dentro de ella

 159
Author: Johannes Klauß,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-07 20:04:08

Sé que este es un hilo muy antiguo, pero aparece en la parte superior de Google, así que aquí hay otra opción.

Este es CSS puro, y no requiere ningún HTML adicional.

box-shadow: inset 0 0 0 1000px rgba(0,0,0,.2);

Hay un sorprendente número de usos para la función box-shadow.

 216
Author: BevansDesign,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-06 14:48:49

De CSS-Tricks... hay una forma de un paso para hacer esto sin z-indexación y la adición de pseudo elementos requires requiere gradiente lineal que creo que significa que necesita soporte CSS3

.tinted-image {
  background-image: 
    /* top, transparent red */
    linear-gradient(
      rgba(255, 0, 0, 0.45), 
      rgba(255, 0, 0, 0.45)
    ),
    /* your image */
    url(image.jpg);
}
 81
Author: Tom,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-26 14:19:28

También puede utilizar un degradado lineal y una imagen: http://codepen.io/anon/pen/RPweox

.background{
  background: linear-gradient(rgba(0,0,0,.5), rgba(0,0,0,.5)),
    url('http://www.imageurl.com');
}

Esto se debe a que la función gradiente lineal crea una Imagen que se agrega a la pila de fondo. https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient

 36
Author: TorranceScott,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-04-23 20:17:55

Necesita entonces un elemento de envoltura con la imagen bg y en él el elemento de contenido con el color bg:

<div id="Wrapper">
  <div id="Content">
    <!-- content here -->
  </div>
</div>

Y el css:

#Wrapper{
    background:url(../img/bg/diagonalnoise.png); 
    width:300px; 
    height:300px;
}

#Content{
    background-color:rgba(248,247,216,0.7); 
    width:100%; 
    height:100%;
}
 23
Author: Sven Bieder,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-06 14:17:08

Prueba esto. Funciona para mí.

.background {
    background-image: url(images/images.jpg);
    display: block;
    position: relative;
}

.background::after {
    content: "";
    background: rgba(45, 88, 35, 0.7);
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1;
}

.background > * {
    z-index: 10;
}
 16
Author: yaufaniadam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-11 02:46:00

He utilizado esto como una forma de aplicar tintes de color, así como degradados a las imágenes para hacer que el texto de superposición dinámica sea más fácil de estilo para la legibilidad cuando no se puede controlar los perfiles de color de la imagen. No tienes que preocuparte por z-index.

HTML

<div class="background-image"></div>

SASS

.background-image {
  background: url('../img/bg/diagonalnoise.png') repeat;
  &:before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(248, 247, 216, 0.7);
  }
}

CSS

.background-image {
  background: url('../img/bg/diagonalnoise.png') repeat;
}

.background-image:before {
    content: '';
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
    background: rgba(248, 247, 216, 0.7);
  }

Espero que ayude

 12
Author: d4ncer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-04 02:06:13

Ver mi respuesta en https://stackoverflow.com/a/18471979/193494 para una visión general de las posibles soluciones:

  1. usando múltiples fondos con un gradiente lineal,
  2. múltiples fondos con un PNG generado, o
  3. styling an :después de pseudoelement para actuar como una capa de fondo secundaria.
 4
Author: Kevin C.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 11:55:03

¿Por qué tan complicado? Su solución fue casi correcta, excepto que es una manera más fácil de hacer el patrón transparente y el color de fondo sólido. PNG puede contener transparencias. Por lo tanto, use photoshop para hacer el patrón transparente configurando la capa al 70% y volviendo a guardar la imagen. Entonces solo necesita un selector. Funciona cross browser.

CSS:

.background {
   background: url('../img/bg/diagonalnoise.png');/* transparent png image*/
   background-color: rgb(248, 247, 216);
}

HTML:

<div class="background">
   ...
</div> 
 3
Author: Hexodus,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-23 14:13:12

Puede usar un píxel semitransparente, que puede generar por ejemplo aquí , incluso en base64 Aquí hay un ejemplo con blanco 50%:

background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8Xw8AAoMBgDTD2qgAAAAASUVORK5CYII=),
url(../img/leftpanel/intro1.png);
background-size: cover, cover;
  • Sin cargar

  • Sin html adicional

  • Supongo que la carga debería ser más rápida que la sombra de caja o el gradiente lineal

 2
Author: Fanky,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-05 15:27:16

Aquí hay un truco más simple con solo css.

<div class="background"> </div>
    <style>
    .background {
      position:relative;
      height:50px;
      background-color: rgba(248, 247, 216, 0.7);
      background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAJElEQVQYV2NctWrVfwYkEBYWxojMZ6SDAmT7QGx0K1EcRBsFAADeG/3M/HteAAAAAElFTkSuQmCC); 
    }

    .background:after {
        content:" ";
        background-color:inherit;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%; 
    }

    </style>
 0
Author: Zortext,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-10-06 14:36:06