search
HomeWeb Front-endCSS TutorialImplementation of color gradient in css (three ways)

The content this article brings to you is about the implementation of color gradient in CSS (three methods). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

! ! Note that IE9 and earlier versions do not support gradients. Safari needs to be prefixed with -webkit-, Opera needs to be prefixed with -o-, and Firefox needs to be prefixed with -moz-!

1. Linear Gradients (linear gradient)-down/up/left/right/diagonal direction

1. Downward code

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<title>渐变学习</title>
<style>
div{
    width:200px;
    height:200px;
    }
.gradient{
 background:-webkit-linear-gradient(#8A2BE2,#DC143C);/*for safari5.1-6.0*/
 background:-o-linear-gradient(#8A2BE2,#DC143C);/*Opera 11.1-12.0*/
 background:-moz-linear-gradient(#8A2BE2,#DC143C);/*firefox 3.6-15*/
 background:linear-gradient(#8A2BE2,#DC143C);/*标准语法,必须放在最后*/
    }
</style>
</head>
<body>
<div class="gradient"></div>
</body>
</html>

Running effect:

Implementation of color gradient in css (three ways)

#2. Go up and change the order of the two colors of the downward gradient. Replace the above code and the running result is:

3. Left code

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<title>渐变学习</title>
<style>
div{
    width:200px;
    height:200px;
    }
.gradient{
 background:-webkit-linear-gradient(left,#8A2BE2,#DC143C);/*for safari5.1-6.0*/
 background:-o-linear-gradient(right,#8A2BE2,#DC143C);/*Opera 11.1-12.0*/
 background:-moz-linear-gradient(right,#8A2BE2,#DC143C);/*firefox 3.6-15*/
 background:linear-gradient(to right,#8A2BE2,#DC143C);/*标准语法*/
    }
</style>
</head>
<body>
<div class="gradient"></div>
</body>
</html>

The running result is:

Implementation of color gradient in css (three ways)

Note: Gradient from left to right, the standard writing method is to add the direction word to right in the brackets of the downward gradient; among them, add left for safari5.1-6.0; add left for Opera11.1-12 and firefox3.6-15 right; According to the browser order in the above code (standard syntax must be placed at the end!!), you can write the first item from the left and the next three items to the right.

4. From right to left, modify the direction word on the gradient code from left to right to get the rendering:

5. Right Angular direction

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8">
<title>渐变学习</title>
<style>
div{
    width:200px;
    height:200px;
    }
.gradient{
 background:-webkit-linear-gradient(left top,#8A2BE2,#DC143C);/*for safari5.1-6.0*/
 background:-o-linear-gradient(bottom right,#8A2BE2,#DC143C);/*Opera 11.1-12.0*/
 background:-moz-linear-gradient(bottom right,#8A2BE2,#DC143C);/*firefox 3.6-15*/
 background:linear-gradient(to bottom right,#8A2BE2,#DC143C);/*标准语法*/
    }
</style>
</head>
<body>
<div class="gradient"></div>
</body>
</html>

Run result:

Implementation of color gradient in css (three ways)

##Note: The direction words are written in the order of up, down, left and right according to the browser Write the starting position in one item, write the reaching position in the last three items, and add to as the fourth item is the standard item; it should be noted that the reaching item corresponding to the upper left left top is bottom right.

2. Angle gradient

1. All the diagonal gradients above, up, down, left and right can be written by angle gradient. Just add angles such as 0deg, 45deg, 90deg, 180deg, etc. to the first item of the bracket indicating the color.

2. Angle refers to the angle between the gradient direction and the horizontal line, calculated in the counterclockwise direction. For example: 0deg refers to the gradient from bottom to top, and 90deg refers to the gradient from left to right.

3. For Chrome, Safari, Firefox, etc., the conversion formula is 90-x=y, and X is the standard degree.

3. Multiple color nodes: Just add more colors in the brackets indicating the direction color. The writing method is still as above, write the direction first and then the color.

4. Gradient with transparency: use rgba (0,0,0,0.2) to represent color, where 0.2 represents transparency.

5. Repeating linear gradient: Use the repeating-linear-gradient() function, and each color in brackets specifies the gradient ratio.

6. Radial Gradient

1. Radial gradient: You can specify the center, shape (circle or ellipse), and size (closest-side; farthest-side) of the gradient. ;closest-coner;farthest-corner). The default center is center, the shape is ellipse (ellipse), and the gradient size is farthest-corner (to the farthest corner). Syntax: background:-radial-gradient(center,shape,size,start-color,...,last-clor).

2. Radial gradient of unevenly distributed color nodes: that is, specifying the proportion of each color.

3. Repeating radial gradient: use the repeating-radial-gradient() function. Set the scale for each color.

Related recommendations:

Gradient color of css

How to achieve background color gradient in CSS_html/css_WEB-ITnose

The above is the detailed content of Implementation of color gradient in css (three ways). For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Ultimate Guide to Linking CSS Files in HTMLThe Ultimate Guide to Linking CSS Files in HTMLMay 13, 2025 am 12:02 AM

Linking CSS files to HTML can be achieved by using elements in part of HTML. 1) Use tags to link local CSS files. 2) Multiple CSS files can be implemented by adding multiple tags. 3) External CSS files use absolute URL links, such as. 4) Ensure the correct use of file paths and CSS file loading order, and optimize performance can use CSS preprocessor to merge files.

CSS Flexbox vs Grid: a comprehensive reviewCSS Flexbox vs Grid: a comprehensive reviewMay 12, 2025 am 12:01 AM

Choosing Flexbox or Grid depends on the layout requirements: 1) Flexbox is suitable for one-dimensional layouts, such as navigation bar; 2) Grid is suitable for two-dimensional layouts, such as magazine layouts. The two can be used in the project to improve the layout effect.

How to Include CSS Files: Methods and Best PracticesHow to Include CSS Files: Methods and Best PracticesMay 11, 2025 am 12:02 AM

The best way to include CSS files is to use tags to introduce external CSS files in the HTML part. 1. Use tags to introduce external CSS files, such as. 2. For small adjustments, inline CSS can be used, but should be used with caution. 3. Large projects can use CSS preprocessors such as Sass or Less to import other CSS files through @import. 4. For performance, CSS files should be merged and CDN should be used, and compressed using tools such as CSSNano.

Flexbox vs Grid: should I learn them both?Flexbox vs Grid: should I learn them both?May 10, 2025 am 12:01 AM

Yes,youshouldlearnbothFlexboxandGrid.1)Flexboxisidealforone-dimensional,flexiblelayoutslikenavigationmenus.2)Gridexcelsintwo-dimensional,complexdesignssuchasmagazinelayouts.3)Combiningbothenhanceslayoutflexibilityandresponsiveness,allowingforstructur

Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)Orbital Mechanics (or How I Optimized a CSS Keyframes Animation)May 09, 2025 am 09:57 AM

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSS Animations: Is it hard to create them?CSS Animations: Is it hard to create them?May 09, 2025 am 12:03 AM

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframes CSS: The most used tricks@keyframes CSS: The most used tricksMay 08, 2025 am 12:13 AM

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSS Counters: A Comprehensive Guide to Automatic NumberingCSS Counters: A Comprehensive Guide to Automatic NumberingMay 07, 2025 pm 03:45 PM

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.