背景图像模糊 - css

Background img blur - css

尊敬的开发人员,您好,

我想模糊 hovering.but 上的背景图像,但它不起作用。有谁能够帮助我?我尝试了很多方法。 这是一个盒子,我想模糊背景并显示带有悬停效果的文本。 当前,当鼠标悬停在框上时,会发生以下情况: 任何一个: 背景变得模糊但文字没有出现 要么: 出现文字但背景不会模糊

HTML

<div class="kursContainer">
        <div class="BodyKurs">
                <h1>Body</h1>
                <picture>
                    <img src="../Kurse/img/body.webp" alt="zumba">
                </picture>
            <div class="overlay">
                <h2>Body</h2>
                <h3>Abnehmen - 6 Wochen</h3>
                <h4>Lorem ipsum dolor sit amet, consectetur adipiscing     elit</h4>
            </div>
        </div>

CSS

.kursContainer {
  display: grid;
  justify-content: space-evenly;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 1em;
  justify-items: center;
  width: 122vmin;
  margin: 2vmin auto;
  align-items: center;

  grid-template-areas:
    "body body fatburn"
    "body body zumba"
    "yoga sixpack sixpack"
    "step sixpack sixpack"
    "backwork backwork tourde"
    "backwork backwork booty";
 }
 .BodyKurs{
   position: relative;
   width: 80vmin;
   height: 80vmin;
   border-radius: 10px;
   background-color: var(--schwarz);
   transition: transform 0.2s;
 }

.BodyKurs img{
   width: 80vmin;
   border-radius: 10px;
}
 .BodyKurs h1 {
   position: absolute;
   text-align: start;
   padding-left: 3vmin;
   bottom: 1vmin;
   color: var(--hellGrau);
   font-size: 8vmin;
 }

 .BodyKurs:hover {
    box-shadow: 0 0px 4.6px rgba(0, 0, 0, 0.278),
     0 0px 15.4px rgba(0, 0, 0, 0.411), 0 0px 69px rgba(0, 0, 0, 0.69);
   transform: scale(1.05);
   z-index: 2;
   cursor: pointer;
 }

  .overlay {
  width: 80vmin;
  height: 80vmin;
  position: absolute;
  text-align: center;
  bottom: 0;
  border-radius: 10px;
  color: var(--hellGrau);
  transition: 0.3s all;
  /* background: rgba(0, 0, 0, 0.2); */
  visibility: hidden;
   cursor: pointer;
  z-index: 5;
}
 .overlay:hover {
  visibility: visible;
}

picture:hover {
  filter: blur(5px);
}

img 标签的 CSS 的伪 class :hover:before 上使用 filter: blur()。我在文本内容上使用了 p 标签,并在两个元素上也设置了 z-index。有效地将元素堆叠在一起。

背景将继续 :before 并确保将位置设置为绝对,将内容设置为 ''(无)。高度和宽度为 100%,因此它会填满它正在堆叠的 div。

blur()

filter:

.image {
  height: 200px;
  width: 200px;
  display: flex;
  justify-content: center;
}
.image:before {
  content: '';
  position: absolute;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  background-image: url(https://image.shutterstock.com/image-photo/white-transparent-leaf-on-mirror-260nw-1029171697.jpg);
  z-index: -1;
}
.image:hover:before {
  -webkit-filter: blur(20px);
  filter: blur(20px);
}
.img {
  position: relative;
  width: 200px;
  height: 200px;
}
.image p {
  z-index: 9999999;
}
<div class="img">
  <div class="image">
    <p>Some dummy text here</p>
  </div>
</div>

只需尝试从正文中删除图片标签,并简单地使用 CSS 背景图片 属性。以 rgba 为单位设置悬停时覆盖的背景颜色,如下所示:

<div class="kursContainer">
        <div class="BodyKurs">
                <h1>Body</h1>
            <div class="overlay">
                <h2>Body</h2>
                <h3>Abnehmen - 6 Wochen</h3>
                <h4>Lorem ipsum dolor sit amet, consectetur adipiscing     elit</h4>
            </div>
        </div>
</div>

CSS

.kursContainer {
  display: grid;
  justify-content: space-evenly;
  grid-template-columns: repeat(3, 1fr);
  grid-gap: 1em;
  justify-items: center;
  width: 122vmin;
  margin: 2vmin auto;
  align-items: center;

  grid-template-areas:
    "body body fatburn"
    "body body zumba"
    "yoga sixpack sixpack"
    "step sixpack sixpack"
    "backwork backwork tourde"
    "backwork backwork booty";
 }
 .BodyKurs{
   position: relative;
   width: 80vmin;
   height: 80vmin;
   border-radius: 10px;
   background-color: var(--schwarz);
   transition: transform 0.2s;
   background-image: url("../Kurse/img/body.webp");
 }

.BodyKurs img{
   width: 80vmin;
   border-radius: 10px;
}
 .BodyKurs h1 {
   position: absolute;
   text-align: start;
   padding-left: 3vmin;
   bottom: 1vmin;
   color: var(--hellGrau);
   font-size: 8vmin;
 }

 .BodyKurs:hover {
    box-shadow: 0 0px 4.6px rgba(0, 0, 0, 0.278),
     0 0px 15.4px rgba(0, 0, 0, 0.411), 0 0px 69px rgba(0, 0, 0, 0.69);
   transform: scale(1.05);
   z-index: 2;
   cursor: pointer;
 }

  .overlay {
  width: 80vmin;
  height: 80vmin;
  position: absolute;
  text-align: center;
  bottom: 0;
  border-radius: 10px;
  color: var(--hellGrau);
  transition: 0.3s all;
  /* background: rgba(255, 255, 255, 0.3); */
   cursor: pointer;
  z-index: 5;
}
 .overlay:hover {
  background-color: rgba(255, 255, 255, 0.5);
}

picture:hover {
  filter: blur(5px);
}

你可以使用:not(:hover)风格

示例 1:

.wrap:hover img,
.wrap:hover img:not(:hover) {
    filter: blur(3px);
}

.wrap {
    width: 300px;
}

span {
    position: absolute;
    color: #ffffff;
    z-index: 1;
    text-align: center;
    width: 300px;
    display: none;
}

.wrap:hover span {
    display: block;
}
<div class='wrap'>
    <span>
        <h1>Lorem</h1>
        <p>Text</p>
    </span>
    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRK7bvAryKdY7JSlepTHatE1A4nAVU5lJXkdw&usqp=CAU"
        width="300px">
</div>


这个例子有点多CSS不过我觉得比较容易理解。再次使用 :not(:hover) 但这次是在 span 元素上。

例二:

.wrap:hover img,
.wrap:hover span:not(:hover) {
    filter: blur(3px);
}

.wrap {
    position: relative;
    width: 300px;
    overflow: hidden;
}

span {
    position: absolute;
    top: 0px;
    right: 0px;
    bottom: 0px;
    left: 0px;
    color: #ffffff;
    z-index: 1;
    text-align: center;
    display: none;
}

.wrap:hover span {
    display: block;
}
<div class='wrap'>
    <span>
        <h1>Lorem</h1>
        <p>Text</p>
    </span>
    <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcRK7bvAryKdY7JSlepTHatE1A4nAVU5lJXkdw&usqp=CAU"
        width="300px">
</div>

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body, html {
  height: 100%;
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
}

* {
  box-sizing: border-box;
}

.bg-image {
  /* The image used */
  background-image: url("photographer.jpg");
  
  /* Add the blur effect */
  filter: blur(8px);
  -webkit-filter: blur(8px);
  
  /* Full height */
  height: 100%; 
  
  /* Center and scale the image nicely */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

/* Position text in the middle of the page/image */
.bg-text {
  background-color: rgb(0,0,0); /* Fallback color */
  background-color: rgba(0,0,0, 0.4); /* Black w/opacity/see-through */
  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;
}
</style>
</head>
<body>

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

<div class="bg-text">
  <h2>Blurred Background</h2>
  <h1 style="font-size:50px">I am Sarvesh</h1>
  <p>Blurred image</p>
</div>

</body>
</html>