css - 在 HTML 中更改背景图像的不透明度

标签 css html image background

我已经创建了一个 HTML 页面。我的页面中有一个背景图片。我想更改背景的不透明度 即背景中使用的图像的不透明度。我正在使用此代码向我的页面添加背景图片:

<!DOCTYPE html>
<html>
<head>
    <style>
    body{background-image: url('IMG_18072014_115640.png')}
    </style>
</head>

    <h1>Hello world!</h1>
    <body>The background image will be shown behind this text.</body>

</html>

如何更改此代码以更改此背景图像的不透明度。

最佳答案

background-image 应用到 body:after :pseudo-element 并更改其 opacity,这样 body 的内容就不会受到影响。

body, html {
  position: absolute;
  width: 100%;
  height: 100%;
  margin: 0;
}
body:after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url(http://www.lorempixel.com/600/400);
  opacity: 0.3;
  z-index: -1;
}
<h1>Hello world!</h1>
<body>The background image will be shown behind this text.</body>

关于css - 在 HTML 中更改背景图像的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27861414/

相关文章:

css - Google Chrome 输入类型 =“date” 强制

css - 嵌入字体 : font-face or base64

html - IE8 的 CSS3 框阴影效果?

Android将图像添加到文本(在 TextView 中)?

html - 背景图像上的 float 图像在高分辨率下向屏幕左侧 float

iphone - 如何更改 TabBarItem 图像和标题问题

css 媒体查询,一个或多个用于移动优化

html - wordpress 响应已损坏

javascript - 如何更改CKEditor的编辑器大小?

html - 将 div 重新定位在前一个元素之上