背景图像模糊

创建于 2024-12-03 / 27
字体: [默认] [大] [更大]

了解如何使用 CSS 创建模糊的背景图片。


模糊背景图片

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

亲自试一试 »


如何创建模糊的背景图像

步骤 1) 添加 HTML:

实例

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

<div class="bg-text">
  <h1>I am John Doe</h1>
  <p>And I'm a Photographer</p>
</div>
步骤 2) 添加 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;
} 亲自试一试 »

0 人点赞过