Eliminate the hazy white layer covering the exterior of an image that has been blurred using CSS

Hey there, I've encountered an issue with an image that transitions from blurred to unblurred when the dom is loaded.

<div class="image-wrapper-container2"><div class="image-wrapper orig" style="background-image: url('https://www.w3schools.com/css/img_fjords.jpg'); background-size: cover; background-position: bottom; background-color: #a5a0a5; filter: blur(15px);"></div></div>

Unfortunately, there is a white haze around the outside of the image.

Check out this fiddle to see the issue in action

I attempted to remove the haze by adjusting the dimensions of the second image with

width: 110%;margin-left: -5%;height: 110%;margin-top: -5%;
, but this didn't resolve the problem.

After the image is unblurred, it extends outside the div by 5%.

Is there a solution to eliminate the image haze without causing the image to exceed the boundaries of the image-wrapper-container2 div?

Thanks for any help!

Answer №1

Here's the solution I came up with:

Following @Kaiido's suggestion, I utilized the svg filters method to achieve the desired effect.

For compatibility with Internet Explorer, I had to create a base64 image with a small resolution of 10px x 10px. This base64 image was then loaded for Internet Explorer, resulting in a blurred effect when stretched to fit the container.

To transition to a non-blurred image, I employed the SMIL transition for browsers that support it, a filter: blur transition for browsers that do not support SMIL, and no transition for Internet Explorer where the image is simply swapped.

Here is the HTML:

<svg width="0px" height="0px"
 xmlns="http://www.w3.org/2000/svg"
 xmlns:xlink="http://www.w3.org/1999/xlink">

 <filter id="better-blur" x="0" y="0" width="1" height="1">
  <feGaussianBlur stdDeviation="25" result="blurred">
  <animate id="anim" attributeType="XML" attributeName="stdDeviation" from="25" to="0" begin="indefinite" fill="freeze"
        dur="0.35s"></animate>
  </feGaussianBlur>

  <feMorphology in="blurred" operator="dilate" radius="25" result="expanded"></feMorphology>

  <feMerge>
    <feMergeNode in="expanded"></feMergeNode>
    <feMergeNode in="blurred"></feMergeNode>
  </feMerge>
</filter>
</svg>
<div class="image-wrapper orig" style="background-image: url(\'' . esc_url($thePostThumbUrl) . '\'); background-size: cover; background-position: bottom; filter: url(#better-blur);visibility: hidden;"></div>

For JavaScript:

<script type="text/javascript">
    if (!jQuery("#anim")[0].beginElement) {
        jQuery(".image-wrapper").css("filter", "blur(25px)");
    }
</script>
<script type="text/javascript">
    jQuery(window).load(function() {
        setTimeout(function() {
            if (jQuery("#anim")[0].beginElement) {
                jQuery("#anim")[0].beginElement();
            }
            else {
                jQuery(".image-wrapper").css("filter", "blur(0px)");
            }
        }, 1000);
    });
</script>

And the CSS:

.image-wrapper {
transition: 0.25s filter linear;
-webkit-transition: 0.25s filter linear;
-moz-transition: 0.25s filter linear;
-o-transition: 0.25s filter linear;
position: absolute;
top: 0;
left: 0;
overflow: hidden;
display: block;
}

Special thanks to Kaiido for the guidance!

Answer №2

If you're struggling to get rid of the blurry edge around elements with the filter:blur rule, here's a clever workaround to try out:

  1. Overlap two divs that contain the same image. The first div should have an img element with the image, while the second div should be empty with the same image set as a background.
  2. In the top div, center the image inside a larger wrapper to create a border effect thick enough to cover the blurry edge. Apply the filter:blur to the entire wrapper to blend the blurry edge into the fake border. Adjust the image dimensions using overflow:hidden as needed.
  3. Use JavaScript to hide the top div on page load.

setTimeout(function() {
      $('.fake-image').hide();
    }, 2000);
.fake-image {
      overflow: hidden;
      width: 560px;
      height: 360px;
      margin: 0 auto;
      position: absolute;
      top: 10px;
      left: 50px;
      z-index: 10;
    }

    .container-backup{
        width: 680px;
        height: 480px;
        background-color: rgba(255,255,255,0);
        filter: blur(15px);
        margin-left: -60px;
        margin-top: -60px;
    }

    .image-top img{
      padding:  40px;
      display: block;
    }

    .real-bck-image {
      position: absolute;
      top: 10px;
      left: 50px;
      overflow: hidden;
      width: 560px;
      height: 360px;
      background-image:  url('https://www.w3schools.com/css/img_fjords.jpg');
      background-repeat: no-repeat;
      background-position: center;
      margin: 0 auto; 
     }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="fake-image">
  <div class="container-backup">
    <div class="image-top">
      <img src="https://www.w3schools.com/css/img_fjords.jpg" alt="">
    </div>
   </div>
</div>

<div class="real-bck-image"></div>

Give this a try and see if it solves your issue!

Best regards

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The web application encountered an error: "Uncaught TypeError: Cannot read property 'map' of undefined" when trying to retrieve

Struggling with a simple API fetch, and despite checking everything multiple times, I can't seem to figure out what's going wrong. It feels like I'm missing something crucial. import { useState } from "react"; import ProductCard fr ...

Deliver an assured result to a variable within the angular.extend() function

Can someone provide guidance on how to set myVar to a value returned from an asynchronous service method in the following example? angular.extend(this, { myVar: (function() { getVal(); })() }); function getVal() { var d = $q.defer(); ...

Setting Up AdminLTE Using Bower

Recently, I decided to incorporate the Admin LTE Template into my Laravel project. I diligently followed the guidelines outlined here As soon as I entered the command: bower install admin-lte The installation process seemed to start, but then the ...

Sort the data in Angular JS by one key, but ensure that results with another key set to 0 remain at the end

Here is an array of objects containing information about various car brands: $scope.cars = [ {"brand":"McLaren","price":70,"stock":0}, {"brand":"Renault","price":10,"stock":0}, {"brand":"Ferrari","price":100,"stock":3}, {"brand":"Lamborghini","pri ...

JavaScript - Modify the proposed content prior to inserting it into the input field

In my current project, I have implemented a feature using jQuery UI - v1.11.4, where an HTML textbox utilizes a JavaScript autocomplete functionality. The suggested string for the textbox is created by concatenating several columns (patient_no, patient_nam ...

extracting the value of a chosen radio button in AngularJS using HTML

When attempting to retrieve the selected value from a radio button, I am encountering an issue where choosing "teacher" still shows as "student." Here is the HTML file: <!DOCTYPE html> <html ng-app="phase2"> ... (HTML code continues) Rige ...

Executing the .delete() queryset in Django within a ListView: Best Practices

After much work and effort, I have implemented the following functionality: 1.) I have successfully developed a JavaScript function that extracts the IDs of the items from the database, using checkbox selection in DataTables: function () { // count c ...

Issue "Invalid UTF-8" encountered while compiling sass using @import

Here is the structure of my project: project assets scripts styles app.scss bower_components node_modules public css app.css js bower.json gulpfile.js package.json I am using gulp-sass to compile app.scss into ap ...

Checking for Click Events in React Components

Recently, I created a React component named HelpButton with the following structure: import helpLogo from '../Resources/helplogo.svg'; function HelpButton(props) { const [isOpen, setisOpen] = React.useState(false) function toggle() { ...

Efficiently aligning divs within a container using Bootstrap 5 without altering the overall layout

I've been trying to figure out a solution to this issue, but so far nothing seems to work. I'm working on creating some general components for a system using Bootstrap 5. I've defined a button like this: <div class="col buttonDiv&quo ...

Is it possible to add a border to both the tbody and td

I currently have a table that is organized with tbody elements to group rows together. In order to create a grid-like structure, I applied borders to each individual td element within the tbody. However, I also desire to show that the tbodies themselves ar ...

Creating Dynamic Lists with React Native

<Search ref="search_box" onSearch={this.onSearch} backgroundColor="white" cancelButtonTextStyle={{ color: "red" }} placeholder="Search Food..." ...

Tips for submitting a jQuery star rating:

I recently installed and added a plugin to one of my pages that transforms radio buttons into stars. While the form control works perfectly, I am facing an issue with submitting the form. The radio buttons are grouped together as stars but they do not subm ...

Choose button based on its value

What is the best way to target a button with a specific value using jQuery? <input type="button" value="My task"> ...

Leveraging Angular to incorporate HTML templates from one component into another component

I am currently working with two components. The first one is a table component, identified by the selector 'table-component', which has standard filtering capabilities. The second component is designed for displaying the table on a page. This me ...

VueJS refreshes components while preserving previous data

As a newcomer to VueJs, I am currently working with a Practice component that includes an ExerciseMC component. The parent component retrieves a question object (with a text property) from the backend through a request and passes it as a prop to the Exerci ...

Modifying an object's value before pushing it into an array in JavaScript

I've been struggling to find the right keyword to search for my issue. I've spent hours trying to figure out what's wrong, but it seems like a simple case of pushing an object into an array. The problem arises when I try to log the values of ...

Authentication in HTML5 beyond the internet connection

Seeking feedback on the most effective way to control access to an HTML5 application that is primarily used offline. This application utilizes IndexedDB, local, and session storage to securely store data for offline use, all served via HTTPS. The main go ...

Vue.js's @click feature can be enhanced with ternary operations

I've been attempting to achieve the following in Vue.js, but I keep encountering an error that says [plugin:vite:vue] Unexpected token (1:27): @click="selectedFiles.push(file.id); selectedFiles.length < 1 ? isCollapse=false: isCollapse=true&q ...

Why are cloned jQuery elements triggering events on the parent <div> and not the child <div>?

Currently, I am working on a tool that includes dynamic input fields for user input. To create different sections, I have cloned some divs from the code successfully. However, I am facing an issue where the events attached to the parent div are triggered e ...