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
Where should 'Subscribe to Podcast' link to?Where should 'Subscribe to Podcast' link to?Apr 16, 2025 pm 12:04 PM

For a while, iTunes was the big dog in podcasting, so if you linked "Subscribe to Podcast" to like:

Browser Engine DiversityBrowser Engine DiversityApr 16, 2025 pm 12:02 PM

We lost Opera when they went Chrome in 2013. Same deal with Edge when it also went Chrome earlier this year. Mike Taylor called these changes a "Decreasingly

UX Considerations for Web SharingUX Considerations for Web SharingApr 16, 2025 am 11:59 AM

From trashy clickbait sites to the most august of publications, share buttons have long been ubiquitous across the web. And yet it is arguable that these

Weekly Platform News: Apple Deploys Web Components, Progressive HTML Rendering, Self-Hosting Critical ResourcesWeekly Platform News: Apple Deploys Web Components, Progressive HTML Rendering, Self-Hosting Critical ResourcesApr 16, 2025 am 11:55 AM

In this week's roundup, Apple gets into web components, how Instagram is insta-loading scripts, and some food for thought for self-hosting critical resources.

Git Pathspecs and How to Use ThemGit Pathspecs and How to Use ThemApr 16, 2025 am 11:53 AM

When I was looking through the documentation of git commands, I noticed that many of them had an option for . I initially thought that this was just a

A Color Picker for Product ImagesA Color Picker for Product ImagesApr 16, 2025 am 11:49 AM

Sounds kind of like a hard problem doesn't it? We often don't have product shots in thousands of colors, such that we can flip out the with . Nor do we

A Dark Mode Toggle with React and ThemeProviderA Dark Mode Toggle with React and ThemeProviderApr 16, 2025 am 11:46 AM

I like when websites have a dark mode option. Dark mode makes web pages easier for me to read and helps my eyes feel more relaxed. Many websites, including

Some Hands-On with the HTML Dialog ElementSome Hands-On with the HTML Dialog ElementApr 16, 2025 am 11:33 AM

This is me looking at the HTML element for the first time. I've been aware of it for a while, but haven't taken it for a spin yet. It has some pretty cool and

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment