search
HomeWeb Front-endCSS TutorialHow to create a linear gradient background using CSS?

How to create a linear gradient background using CSS?

In CSS, a linear gradient background is a design technique used to create a smooth transition between two or more colors within a single element. It is defined using the CSS background-image property and the lineargradient() function.

Linear gradient properties in CSS

  • to - It specifies the direction of the gradient

  • color-stops − It specifies the colors used in the gradient and their positions.

  • repeating-linear-gradient - Creates a repeating gradient where the gradient pattern repeats horizontally or vertically.

  • background-size − It specifies the size of the gradient background.

  • background-clip − It specifies the area of ​​the element that the gradient background should cover.

  • background-origin - It specifies the origin of the gradient background.

  • background-attachment - It specifies whether the gradient background should be fixed or scroll with the rest of the page.

  • background-position - It specifies the position of the gradient background within the element.

Use CSS to create a linear gradient background

Linear gradient is a popular background effect in web design because it can add depth and texture to any element. It’s easy to do using CSS, no graphics or complex design software required. In this article, we’ll explore how to create a linear gradient background using CSS.

step

With the following steps, we can easily create a linear gradient background in HTML and CSS.

Step 1: Define the gradient

In this step we define the gradient. To create a linear gradient, we use the CSS background property and the linear-gradient() function.

Step 2: Apply the gradient to the element

After defining the gradient, we apply it to the HTML element.

Step 3: Customize gradient

Gradient can be easily customized according to design needs. We can change the direction of the gradient by changing the value of the to keyword.

The Chinese translation of

Example 1

is:

Example 1

In this example, the gradient effect is applied to the body element in the CSS style block at the head of the HTML document.

<html>
   <head>
      <title>Example to create linear gradient background using CSS</title>
      <style>
         body {
            background: linear-gradient(to right, #ff0000, #ffff00);
         }
      </style>
   </head>
   <body>
      <h2 id="Creating-linear-gradient-background-using-CSS">Creating linear gradient background using CSS </h2>
      <p>This is a sample HTML document with a linear gradient background.</p>
   </body>
</html>

In the above example, a gradient from red (#ff0000) to yellow (#ffff00) is created using CSS’s linear-gradient function, from left to right. The gradient is applied to the body element in the CSS style block in the head section of the HTML document.

Example 2

In this example, the gradient effect will be applied to the .container element and will serve as a container for the text content.

<html>
   <head>
      <title>Example to create linear gradient background using CSS</title>
      <style>
         body{
            text-align:center;
         }
         .container {
            height: 200px;
            background: linear-gradient(to bottom, #ff0000, #ffff00);
         }
      </style>
   </head>
   <body>
      <div class="container">
         <h2 id="Creating-linear-gradient-background-using-CSS">Creating linear gradient background using CSS </h2>
         <p>This is a sample HTML document with a linear gradient background.</p>
      </div>
   </body>
</html>

In the above example, a CSS class named .container is created and applied to the div element in the HTML body. CSS Linear Gradient function is used to create a gradient starting from red (#ff0000) to yellow (#ffff00) from top to bottom (to the bottom).

The Chinese translation of

Example 3

is:

Example 3

In this example, the gradient effect will be applied to the .header element

<html>
   <head>
      <title>Example to create linear gradient background using CSS</title>
      <style>
         .header {
            height: 50px;
            background: linear-gradient(45deg, #ff0000, #ffff00);
         }
      </style>
   </head>
   <body>
      <div class="header"></div>
      <h2 id="Creating-linear-gradient-background-using-CSS">Creating linear gradient background using CSS </h2>
      <p>This is a sample HTML document with a linear gradient background.</p>
   </body>
</html>

In the above example, a CSS class named .header is created and applied to a div element in the HTML body. Use CSS's linear-gradient function to create a gradient from red (#ff0000) to yellow (#ffff00), starting at an angle of 45 degrees (45deg). This gradient is applied to the .header element with a height of 100 pixels, which serves as the header portion of the page.

in conclusion

Creating a linear gradient background using CSS is a simple and effective way to add depth and texture to your web design. Just a few lines of code.

The above is the detailed content of How to create a linear gradient background using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

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 Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software