Hướng dẫn css filter green color

Đã đăng vào thg 10 10, 2018 1:59 SA 0 phút đọc

Bạn có thể áp dụng filter cho toàn bộ phần tử HTML khá dễ dàng với thuộc tính filter. Nhưng điều gì sẽ xảy ra nếu bạn muốn áp dụng bộ lọc chỉ cho background image của một phần tử? Có các thuộc tính CSS dành riêng cho background image, như chế độ hòa trộn nền [thuộc tính background-blend-mode] - nhưng việc pha trộn màu [blend] và các bộ lọc màu [filter] là không giống nhau.

Dưới dây mình sẽ hướng dẫn các bạn làm điều đó với việc sử dụng pseudo-element.

CSS

.element {
  position: relative;
}
.element::before {
  content: "";
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background-image: url[image-to-be-filtered.jpg];
  filter: grayscale[100%];
}
.element-inside {
  /* Sử dụng position relative sẽ làm cho nó xếp chồng lên trên thẻ ::before */
  position: relative;
}

HTML

element

Demo

Dưới đây là một tool khá hay để các bạn có thể thử nghiệm của tác giả Dan Wilson

All rights reserved

I want only red and blue color component in the image [or text]. Can I use CSS 'filter' feature for that ? If yes, please help. Thank you.

asked Aug 22, 2020 at 17:51

1

There is no such thing in CSS AFAIK, but you can use Canvas API to obtain image data via getImageData and filter out the required component manually with javascript code.

answered Aug 22, 2020 at 17:58

olkivanolkivan

891 silver badge3 bronze badges

You can try using the filter[] property of CSS [MDN]. It can apply effects on HTML elements including modifying colors. For your specific case, it would be required to define an own filter.

Look at this example: //codesandbox.io/s/happy-browser-wgvu8?file=/index.html



    
        .filter-this {
          filter: url[#myfilter];
        }

    



    

I am red

I am green

I am blue

I am yellow

The value of the feColorMatrix is a matrix which will be multiplied with every pixel. The input vectors are RGBa values appended with a 1 [1 1 0 1 1 for yellow]. By making the second row of the transformation matrix all zero, it removes the green component turning the green text to black and the yellow text to red.

answered Aug 22, 2020 at 21:08

CC.CC.

4524 silver badges11 bronze badges


Example

Change all images to black and white [100% gray]:

img {
  filter: grayscale[100%];
}

Try it Yourself »

Tip: More "Try it Yourself" examples below.

Definition and Usage

The filter property defines visual effects [like blur and saturation] to an element [often ].

Show demo ❯

Browser Support

The numbers in the table specify the first browser version that fully supports the property.

Numbers followed by -webkit- specify the first version that worked with a prefix.

Property
filter 53.0
18.0 -webkit-
13.0 35.0 9.1
6.0 -webkit-
40.0
15.0 -webkit-

CSS Syntax

filter: none | blur[] | brightness[] | contrast[] | drop-shadow[] | grayscale[] | hue-rotate[] | invert[] | opacity[] | saturate[] | sepia[] | url[];

Tip: To use multiple filters, separate each filter with a space [See "More Examples" below].

Filter Functions

Note: The filters that use percentage values [i.e. 75%], also accept the value as decimal [i.e. 0.75].

FilterDescriptionDemo
none Default value. Specifies no effects Demo ❯
blur[px] Applies a blur effect to the image. A larger value will create more blur.

If no value is specified, 0 is used.

Demo ❯
brightness[%] Adjusts the brightness of the image.

0% will make the image completely black.
100% [1] is default and represents the original image.
Values over 100% will provide brighter results.

Demo ❯
contrast[%] Adjusts the contrast of the image.

0% will make the image completely black.
100% [1] is default, and represents the original image.
Values over 100% will provide results with more contrast.

Demo ❯
drop-shadow[h-shadow v-shadow blur spread color] Applies a drop shadow effect to the image.

Possible values:
h-shadow - Required. Specifies a pixel value for the horizontal shadow. Negative values place the shadow to the left of the image.

v-shadow - Required. Specifies a pixel value for the vertical shadow. Negative values place the shadow above the image.

blur - Optional. This is the third value, and must be in pixels. Adds a blur effect to the shadow. A larger value will create more blur [the shadow becomes bigger and lighter]. Negative values are not allowed. If no value is specified, 0 is used [the shadow's edge is sharp].

spread - Optional. This is the fourth value, and must be in pixels. Positive values will cause the shadow to expand and grow bigger, and negative values will cause the shadow to shrink. If not specified, it will be 0 [the shadow will be the same size as the element].
Note: Chrome, Safari and Opera, and maybe other browsers, do not support this 4th length; it will not render if added.

color - Optional. Adds a color to the shadow. If not specified, the color depends on the browser [often black].

An example of creating a red shadow, which is 8px big both horizontally and vertically, with a blur effect of 10px:

filter: drop-shadow[8px 8px 10px red];

Tip: This filter is similar to the box-shadow property.

Demo ❯
grayscale[%] Converts the image to grayscale.

0% [0] is default and represents the original image.
100% will make the image completely gray [used for black and white images].

Note: Negative values are not allowed.

Demo ❯
hue-rotate[deg] Applies a hue rotation on the image. The value defines the number of degrees around the color circle the image samples will be adjusted. 0deg is default, and represents the original image.

Note: Maximum value is 360deg.

Demo ❯
invert[%] Inverts the samples in the image.

0% [0] is default and represents the original image.
100% will make the image completely inverted.

Note: Negative values are not allowed.

Demo ❯
opacity[%] Sets the opacity level for the image. The opacity-level describes the transparency-level, where:

0% is completely transparent.
100% [1] is default and represents the original image [no transparency].

Note: Negative values are not allowed.
Tip: This filter is similar to the opacity property.

Demo ❯
saturate[%] Saturates the image.

0% [0] will make the image completely un-saturated.
100% is default and represents the original image.
Values over 100% provides super-saturated results.

Note: Negative values are not allowed.

Demo ❯
sepia[%] Converts the image to sepia.

0% [0] is default and represents the original image.
100% will make the image completely sepia.

Note: Negative values are not allowed.

Demo ❯
url[] The url[] function takes the location of an XML file that specifies an SVG filter, and may include an anchor to a specific filter element. Example:

filter: url[svg-url#element-id]

initial Sets this property to its default value. Read about initial
inherit Inherits this property from its parent element. Read about inherit

More Examples

Blur Example

Apply a blur effect to the image:

img {
  filter: blur[5px];
}

Try it Yourself »

Blur Example 2

Apply a blurred background image:

img.background {
  filter: blur[35px];
}

Try it Yourself »

Brightness Example

Adjust the brightness of the image:

img {
  filter: brightness[200%];
}

Try it Yourself »

Contrast Example

Adjust the contrast of the image:

img {
  filter: contrast[200%];
}

Try it Yourself »

Drop Shadow Example

Apply a drop shadow effect to the image:

img {
  filter: drop-shadow[8px 8px 10px gray];
}

Try it Yourself »

Grayscale Example

Convert the image to grayscale:

img {
  filter: grayscale[50%];
}

Try it Yourself »

Hue Rotation Example

Apply a hue rotation on the image:

img {
  filter: hue-rotate[90deg];
}

Try it Yourself »

Invert Example

Invert the samples in the image:

img {
  filter: invert[100%];
}

Try it Yourself »

Opacity Example

Set the opacity level for the image:

img {
  filter: opacity[30%];
}

Try it Yourself »

Saturate Example

Saturate the image:

img {
  filter: saturate[800%];
}

Try it Yourself »

Sepia Example

Convert the image to sepia:

img {
  filter: sepia[100%];
}

Try it Yourself »

Using Multiple Filters

To use multiple filters, separate each filter with a space. Notice that the order is important [i.e. using grayscale[] after sepia[] will result in a completely gray image]:

img {
  filter: contrast[200%] brightness[150%];
}

Try it Yourself »

All Filters

A demonstration of all filter functions:

.blur {
  filter: blur[4px];
}

.brightness {
  filter: brightness[0.30];
}

.contrast {
  filter: contrast[180%];
}

.grayscale {
  filter: grayscale[100%];
}

.huerotate {
  filter: hue-rotate[180deg];
}

.invert {
  filter: invert[100%];
}

.opacity {
  filter: opacity[50%];
}

.saturate {
  filter: saturate[7];
}

.sepia {
  filter: sepia[100%];
}

.shadow {
  filter: drop-shadow[8px 8px 10px green];
}

Try it Yourself »

Related Pages

CSS Tutorial: CSS Images

HTML DOM reference: filter property



Chủ Đề