How to create a gradient background in CSS?
Jul 30, 2025 am 05:42 AMTo create a CSS gradient background, use the background or background-image attributes to be used with the gradient function. 1. Linear gradient: Use linear-gradient() to set the direction such as to bottom, to right or angle such as 45deg to achieve a linear transition of color. 2. Radial gradient: Use radial-gradient() to define the shape through circle or ellipse, and the color spreads outward from the center point. 3. Repeat gradient: Use repeating-linear-gradient() or repeating-radial-gradient() to create stripes or patterns, such as diagonal lines every 10px. 4. Browser compatibility: Provide solid color background for older browsers as fallback scheme, and modern browsers will automatically overwrite as gradients. 5. Advanced tips: You can add multiple colors, custom color stop points (such as 0% to 100%), transparency (rgba) to achieve masking effects, and combine background-size and background-position to finely control the layout. With these methods, a modern background effect without pictures can be achieved with just a few lines of code.
Creating a gradient background in CSS is straightforward using the background
or background-image
property with the linear-gradient()
, radial-gradient()
, or other gradient functions. Here's how to do it step by step.

1. Linear Gradient (Most Common)
A linear gradient transitions colors in a straight line — top to bottom, left to right, or at an angle.
Basic Syntax:

background: linear-gradient(direction, color1, color2, ...);
Example – Top to Bottom (default):
.gradient-bg { background: linear-gradient(to bottom, #ff7e5f, #feb47b); }
Example – Left to Right:

.gradient-bg { background: linear-gradient(to right, #ff7e5f, #feb47b); }
Example – Diagonal (Top-left to Bottom-right):
.gradient-bg { background: linear-gradient(45deg, #ff7e5f, #feb47b); }
You can also use angles (eg, 135deg
) for more control.
2. Radial Gradient
Colors transition from a central point outward in a circular or elliptical shape.
Syntax:
background: radial-gradient(shape size at position, color1, color2, ...);
Example – Circular Gradient:
.gradient-bg { background: radial-gradient(circle, #ff7e5f, #feb47b); }
Example – Elliptical Gradient Centered:
.gradient-bg { background: radial-gradient(ellipse at center, #a8edea, #dfe9f3); }
3. Repeating Gradients
Use repeating-linear-gradient()
or repeating-radial-gradient()
for patterns.
Example – Striped Pattern:
.striped-bg { background: repeating-linear-gradient( 45deg, #ff7e5f, #ff7e5f 10px, #feb47b 10px, #feb47b 20px ); }
This creates diagonal stripes every 10px.
4. Adding Fallbacks and Browser Support
Always consider older browsers and provide a solid color fallback.
.gradient-bg { background: #ff7e5f; /* Fallback */ background: linear-gradient(to right, #ff7e5f, #feb47b); }
Modern browsers will override the solid color with the gradient.
5. Advanced Tips
Multiple Colors: You can include more than two colors.
background: linear-gradient(to right, red, yellow, blue);
Color Stops: Define exactly where each color starts.
background: linear-gradient(to right, #ff7e5f 0%, #feb47b 50%, #a8edea 100%);
Transparent Gradients: Useful for overlays.
background: linear-gradient(to top, rgba(0,0,0,0.8), rgba(0,0,0,0));
Background Size & Position: Combine with
background-size
orbackground-position
for fine control..gradient-bg { background: linear-gradient(45deg, #ff7e5f, #feb47b); background-size: cover; height: 100vh; }
Basically, gradients in CSS are flexible and powerful. Start with
linear-gradient()
for simple effects, then experiment with directions, color stops, and radial patterns. With just a few lines, you can create rich, modern backgrounds without images.The above is the detailed content of How to create a gradient background in CSS?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

1. The first choice for the Laravel MySQL Vue/React combination in the PHP development question and answer community is the first choice for Laravel MySQL Vue/React combination, due to its maturity in the ecosystem and high development efficiency; 2. High performance requires dependence on cache (Redis), database optimization, CDN and asynchronous queues; 3. Security must be done with input filtering, CSRF protection, HTTPS, password encryption and permission control; 4. Money optional advertising, member subscription, rewards, commissions, knowledge payment and other models, the core is to match community tone and user needs.

The core role of Homebrew in the construction of Mac environment is to simplify software installation and management. 1. Homebrew automatically handles dependencies and encapsulates complex compilation and installation processes into simple commands; 2. Provides a unified software package ecosystem to ensure the standardization of software installation location and configuration; 3. Integrates service management functions, and can easily start and stop services through brewservices; 4. Convenient software upgrade and maintenance, and improves system security and functionality.

will-change is a CSS property that is used to inform browser elements in advance of possible changes to optimize performance. Its core function is to enable the browser to create layers in advance to improve rendering efficiency. Common values include transform, opacity, etc., and can also be separated by multiple attribute commas; it is suitable for non-standard attribute animations, complex component transitions and user interaction triggered animations; but it must be avoided abuse, otherwise it will lead to excessive memory usage or increased GPU load; the best practice is to apply before the change occurs and remove it after it is completed.

Different browsers have differences in CSS parsing, resulting in inconsistent display effects, mainly including the default style difference, box model calculation method, Flexbox and Grid layout support level, and inconsistent behavior of certain CSS attributes. 1. The default style processing is inconsistent. The solution is to use CSSReset or Normalize.css to unify the initial style; 2. The box model calculation method of the old version of IE is different. It is recommended to use box-sizing:border-box in a unified manner; 3. Flexbox and Grid perform differently in edge cases or in old versions. More tests and use Autoprefixer; 4. Some CSS attribute behaviors are inconsistent. CanIuse must be consulted and downgraded.

This article has selected a series of top-level finished product resource websites for Vue developers and learners. Through these platforms, you can browse, learn, and even reuse massive high-quality Vue complete projects online for free, thereby quickly improving your development skills and project practice capabilities.

In CSS, the attribute selector can set styles according to the attributes and values of the element, providing more flexible style control. ①Basic usage: Select elements with specific attributes, such as input[type] to match all inputs containing type attributes; ②Exact match: Use = to match specific attribute values, such as input[type="text"] to match only the text input box; ③ Partial match: Use = (include), ^= (start), and $= (end), to match part of the attribute values, such as a[href="example.com"] to match anchors containing specific links; ④Combination match: match multiple attributes at the same time, such as inputtype=&qu
