如何創建:模糊的背景圖像

學習如何使用 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;
}

親自試一試