如何创建:模糊的背景图像

学习如何使用 CSS 创建模糊背景图像。

模糊背景图像

注意:此例不适用于 Edge 12、IE 11 或更早版本。

亲自试一试

如何创建模糊的背景图像

第一步 - 添加 HTML:

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

<div class="bg-text">
  <h1>I am Bill Gates</h1>
  <p>And I'm a Photographer</p>
</div>

第二步 - 添加 CSS:

body, html {
  height: 100%;
}

* {
  box-sizing: border-box;
}

.bg-image {
  /* 所用的图像 */
  background-image: url("photographer.jpg");

  /* 添加模糊效果 */
  filter: blur(8px);
  -webkit-filter: blur(8px);

  /* 全高 */
  height: 100%;

  /* 将图像居中并适当地缩放 */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

/* 将文本放置在页面/图像的中间 */
.bg-text {
  background-color: rgb(0,0,0); /* 回退颜色 */
  background-color: rgba(0,0,0, 0.4); /* 黑色半透明/可透视 */
  color: white;
  font-weight: bold;
  border: 3px solid #f1f1f1;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  width: 80%;
  padding: 20px;
  text-align: center;
}

亲自试一试