Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gray out a section of an image with CSS

Tags:

css

image

filter

I'm trying to grey out a section of an image (two stripes at left and right). Is there a way to do this using just CSS?

I know I can use filters (i.e. filter: grayscale(100%)), but I don't want the entire image to be grey.

like image 265
kunde Avatar asked Oct 25 '13 21:10

kunde


2 Answers

Pure CSS Solution with no extra markup

JSFIDDLE DEMO

HTML

<div class="image-container">
    <img class="image-grey" src="http://placekitten.com/200/150" />
</div>

CSS

.image-container {
    position:relative;
    display:inline-block;
}

.image-grey {
    display:block;
    -webkit-filter: grayscale(100%);
}

.image-container:after {
    position:absolute;
    content:"";
    top:0;
    left:50%;
        width:50%;
    height:100%;
    background-image:url(http://placekitten.com/200/150);
    background-position:top right;
}
like image 95
Paulie_D Avatar answered Nov 15 '22 07:11

Paulie_D


Put a div over it.

<div id="wrapper">
    <img src="path/to/file.jpg" />
    <div id="filter">
</div>
<style>
#wrapper {position: relative;}
#filter {
    position: absolute;
    top: 123px;
    left: 123px;
    width: 42px;
    height: 42px;
    background: rgba(255,0,0,0.5);
}
</style>

http://jsfiddle.net/BQ7J3/

like image 23
bjb568 Avatar answered Nov 15 '22 07:11

bjb568