search
  • Sign In
  • Sign Up
Password reset successful

Follow the proiects vou are interested in andi aet the latestnews about them taster

Table of Contents
Basic syntax and direction
Adding color stops and multiple colors
Using transparency and fallbacks
Applying to elements and pseudo-elements
Home Web Front-end CSS Tutorial How to create a linear gradient background in CSS? (Code Examples)

How to create a linear gradient background in CSS? (Code Examples)

Jan 26, 2026 am 04:32 AM

使用CSS的linear-gradient()函数可创建线性渐变背景,支持方向控制(如to right、45deg)、多色停靠点、透明度(rgba)及浏览器兼容回退色。

How to create a linear gradient background in CSS? (Code Examples)

To create a linear gradient background in CSS, use the background or background-image property with the linear-gradient() function. It lets you define two or more colors that blend smoothly along a straight line.

Basic syntax and direction

The simplest linear gradient goes from top to bottom (default). You can control direction using keywords like to right, to bottom right, or degree values like 45deg.

  • background: linear-gradient(to right, red, blue); — left to right
  • background: linear-gradient(135deg, #ff9a9e, #fad0c4); — diagonal (top-left to bottom-right)
  • background: linear-gradient(to bottom, #3498db, #2c3e50); — top to bottom (explicit)

Adding color stops and multiple colors

You’re not limited to two colors. Specify exact positions using percentages or lengths to fine-tune transitions.

  • background: linear-gradient(to right, #ff0000, #ffff00 50%, #00ff00); — yellow stops exactly halfway
  • background: linear-gradient(90deg, #6a11cb 0%, #2575fc 50%, #00c9ff 100%); — three-color horizontal sweep

Using transparency and fallbacks

Include rgba() for transparent gradients. Always declare a solid background color before the gradient as a fallback for older browsers.

  • background: #333; — fallback color
  • background: linear-gradient(to bottom, rgba(255,255,255,0), rgba(0,0,0,0.8)); — fade-to-dark overlay

Applying to elements and pseudo-elements

Gradients work on any element with a background — buttons, cards, headers, or even ::before/::after.

  • button { background: linear-gradient(45deg, #ff6b6b, #4ecdc4); }
  • .card::before { content: ''; position: absolute; background: linear-gradient(to top, rgba(0,0,0,0.6), transparent); }

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

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Popular tool

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use the calc() function in CSS? (For Dynamic Sizing) How to use the calc() function in CSS? (For Dynamic Sizing) Jan 14, 2026 am 01:09 AM

The calc() function of CSS supports real-time calculation of attribute values ​​for responsive layout; the syntax is calc (expression), which requires a space-separated operator. Relative and absolute units (such as 100%-2rem) can be mixed, but must be mathematically legal; it supports custom attribute embedding and full coverage of modern browsers, and older versions of IE require a fallback in advance.

How to use CSS subgrid for inherited layouts? (Advanced Grid) How to use CSS subgrid for inherited layouts? (Advanced Grid) Jan 14, 2026 am 05:14 AM

Subgridreusesonlytheparent’sexplicittracksizing,notlayoutvalueslikegrid-template-columns;itrequiresthesubgridcontainertobeadirectgriditemwithdisplay:gridandexplicitparenttracks.

How to style form validation messages with CSS? How to style form validation messages with CSS? Jan 08, 2026 am 01:38 AM

Use CSS pseudo-classes and JavaScript to customize form validation styles to improve user experience. 1. Use :valid/:invalid to set the input box border color and other visual feedback; 2. Use JavaScript to prevent the default pop-up window and display a custom error message; 3. Create a hidden error prompt element, and add an active class display when verification fails; 4. Customize the .error-message style to match the website style, and support adding icons or animations to enhance the prompt effect.

How to create a frosted glass card in CSS? (Modern Design) How to create a frosted glass card in CSS? (Modern Design) Jan 14, 2026 am 06:19 AM

backdrop-filterappliesvisualeffectstotheareabehindanelement,requiringtransparency(e.g.,rgba(255,255,255,0.1))andastackingcontext(e.g.,position:relative)towork;solidbackgroundsblocktheeffect.

How to use CSS object-fit for responsive media? (Image Scaling) How to use CSS object-fit for responsive media? (Image Scaling) Jan 14, 2026 am 06:22 AM

object-fitcontrolshowmediafitsinsideitscontainer—notresizingthecontaineritself;itrequiresexplicitwidth/height(e.g.,width:100%,aspect-ratio)towork,onlyactivateswhenintrinsicandrenderedsizesdiffer,andneedsmodernbrowserfallbackslikebackground-imageforIE

How to manage CSS z-index for stacking elements? (A Beginner's Guide) How to manage CSS z-index for stacking elements? (A Beginner's Guide) Jan 14, 2026 am 05:00 AM

z-indexonlyworksonnon-staticpositionedelementsandoperateswithinstackingcontexts,notglobally;usemodest,spacedvalues(e.g.,0,10,20)anddebugviaDevTools’stackingvisualizationandoutlinechecks.

How to use CSS variables (custom properties)? (Full Tutorial) How to use CSS variables (custom properties)? (Full Tutorial) Jan 13, 2026 am 04:53 AM

CSS variables (custom properties) allow the definition of reusable dynamic values, support cascading, JavaScript update and rollback mechanisms, and are suitable for theming and responsive design.

How to use CSS calc() for dynamic sizing calculations? (Mathematical Styling) How to use CSS calc() for dynamic sizing calculations? (Mathematical Styling) Jan 13, 2026 am 03:22 AM

calc() supports mixed unit operations but must have consistent dimensions. For example, 100vw−40px is feasible but 10px is 20% invalid; operators must be separated by spaces, multiplication and division require numbers, and nesting is prohibited; CSS variables must have units and set default values; clamp() can achieve fluid typesetting but needs to avoid pitfalls of old Safari versions and layout contexts.

Related articles