milar's avatar
Level 1

how to remove the white image html-css like png?

Can we use CSS to delete the white photo background?

.tracking-wider {
    content: "";
    background-color: rgba(0, 0, 255, 0.5);
    width: 100%;
    height: 100%;
    z-index: 1;
    opacity: 1;
    transition: opacity .2s;
    display: inline-block;
}
.tracking-wider:before {
    content: "";
    background-color: rgba(0, 0, 255, 0.5);
    width: 100%;
    height: 100%;
    z-index: 1;
    opacity: 1;
    transition: opacity .2s;
}

demo

0 likes
16 replies
sr57's avatar

css styles your html page, meaning it works on html element. Since your image is in one elemennt you cannot have different styles for this elt. We can imagine having a stack of different elements with the property z-index but it will be awful to code/manage.

The only/best thing you have to do is to recreate your image to remove the background and manage it by directly by css or with a second image/element.

milar's avatar
Level 1

@sr57

How to recreate my image to remove the background?

Sinnbeck's avatar

@kar Open it in an image editor and remove the white background (eraser tool)

Sinnbeck's avatar

@kar How would css know the difference between the white around the image, and the white on the screen?

sr57's avatar

@Sinnbeck

nice example ... anyway. My previous answer should be 'no exact way'

Sinnbeck's avatar

@sr57 Yeah I completely agree with you. I would never do this with css :)

milar's avatar
Level 1

@Sinnbeck

I changed this code

<div class="tracking-wider">
    <img class="w-100 d-inline-block" src="{{ asset('storage/'.$service->image) }}">
</div>

.tracking-wider img {
    mix-blend-mode: multiply;
}

I see this demo.

demo

My problem is still not solved. Where is my real photo?

Sinnbeck's avatar

@kar how would I know? First if I already suggested doing it in a image program and explained why. Secondly I don't have access to your image so how could I test it? I gave a link to a page where it kinda works and expected you would test your image there

1 like
milar's avatar
Level 1

@Sinnbeck For example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .tracking-wider {
            background-color: blue;
            width: 200px;
            height: 200px;
            position: relative;
        }
        .tracking-wider img {
            mix-blend-mode: multiply;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
            width: 100%;
        }
    </style>
</head>
<body>
<div class="tracking-wider">
    <img src="https://dkstatics-public.digikala.com/digikala-products/c5c4a0bb88dc312a4c2f6de0b1567ef0da31a2d9_1630140406.jpg" alt="">
</div>
</body>
</html>
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@kar try setting the background color to #00ff00 in the link I shared an you will see the same. Aka not possible. Lightgray background only

1 like
Chaeril's avatar

you could 'crop' the image with object-fit and object-position. but since you will need a different CSS for each image, it's not a solution

Snapey's avatar

@Chaeril no you cannot because the browser can't see the object within the photo

Please or to participate in this conversation.