How to Make a Linear Gradient Background Cover the Entire Page

Oct 15, 2026
5 Min Read
How to Make a Linear Gradient Background Cover the Entire Page

How to Make a Linear Gradient Background Cover the Entire Page

Sometimes, you want the gradient to cover the entire browser window rather than just one section.

A common approach is to apply the gradient to the body element and set a minimum height for the page.

html {
    min-height: 100%;
}

body {
    min-height: 100vh;
    margin: 0;
    background: linear-gradient(
        135deg,
        #667eea,
        #764ba2
    );
    background-repeat: no-repeat;
}

The 100vh value represents 100% of the viewport height. Setting margin: 0 removes the browser's default body margin.

For a gradient that should cover the visible browser window, including when the page is shorter than the viewport, you can also use a fixed background:

body {
    min-height: 100vh;
    margin: 0;
    background: linear-gradient(135deg, #1d2671, #c33764);
    background-attachment: fixed;
}

Be aware that fixed backgrounds can behave differently on mobile devices. Test the effect on real mobile browsers before using it throughout a production website.

Linear Gradient vs. Radial Gradient in CSS

CSS provides several gradient functions. Linear and radial gradients are two of the most commonly used options.

Feature Linear Gradient Radial Gradient
Color transition Along a straight line Outward from a central point or ellipse
CSS function linear-gradient() radial-gradient()
Best use cases Banners, sections, buttons Glows, highlights, decorative effects
Direction control Angles and directional keywords Shape, size, and position
Example linear-gradient(to right, blue, purple) radial-gradient(circle, blue, purple)

Use a linear gradient when you want colors to transition in a particular direction. Choose a radial gradient when you want a central glow or a circular color transition.

You can also combine different gradient types to create more advanced background designs.

How to Combine Multiple Linear Gradients

CSS allows you to layer multiple gradients by separating them with commas.

.hero {
    background:
        linear-gradient(
            135deg,
            rgba(52, 152, 219, 0.85),
            rgba(142, 68, 173, 0.85)
        ),
        linear-gradient(
            to right,
            #1d2671,
            #c33764
        );
}

The first gradient appears above the second gradient because CSS paints background layers in the order they are listed. The partially transparent colors allow some of the lower gradient to show through.

Layered gradients can create richer backgrounds without external image files. They are especially useful for decorative hero sections, abstract designs, and overlays.

Common Mistakes to Avoid When Creating CSS Gradients

Even though the linear-gradient() function is straightforward, a few mistakes can produce unexpected results.

1. Using an Invalid Direction

CSS supports values such as to right, to bottom, and angles such as 45deg. Using an unsupported direction can cause the browser to ignore the declaration.

Correct:

background: linear-gradient(90deg, blue, purple);

Incorrect:

background: linear-gradient(rightward, blue, purple);

2. Choosing Colors with Poor Contrast

A gradient may look attractive but still make text difficult to read. Always check the contrast between the text and the background at different points along the gradient.

For example, white text might be clear over a dark blue section but difficult to read over a pale yellow section.

Consider adding a darker overlay or adjusting the gradient colors when the background contains important text.

3. Forgetting the Element's Dimensions

A gradient fills the background area of its element. If the element has little or no content and no defined height or padding, the visible gradient may appear smaller than expected.

Use min-height, padding, or an appropriate layout to create the required space.

4. Using Too Many Colors

Adding many colors does not automatically improve a gradient. Too many competing shades can make a website look distracting.

Start with two compatible colors. Add a third only when it supports the design.

5. Expecting Color Stops to Behave Like Separate Backgrounds

Color stops control positions along the gradient line; they do not automatically create independent color blocks. If you want a sharp transition, position two color stops at the same percentage.

background: linear-gradient(
    to right,
    #3498db 50%,
    #e74c3c 50%
);

This creates a sharp division between blue and red instead of a smooth transition.

Best Practices for Professional CSS Gradient Design

Follow these recommendations to make your gradients look better and remain practical for real websites.

  • Use a consistent color palette: Choose colors that complement your website's branding.
  • Keep text readable: Check contrast across the entire gradient, not just at one point.
  • Match the gradient to the content: Subtle gradients often work better for informational pages, while stronger gradients can suit promotional sections.
  • Test responsive layouts: Check how the gradient looks on mobile phones, tablets, and desktop screens.
  • Avoid unnecessary complexity: Two or three carefully selected colors are often sufficient.
  • Consider performance: CSS gradients avoid separate image downloads, although complex layered effects can still add rendering work.
  • Support accessibility: Do not use color alone to communicate important information. Provide visible focus indicators for interactive elements.
  • Maintain design consistency: Reuse a small set of gradient styles across buttons, cards, and banners rather than creating unrelated effects everywhere.

For faster experimentation, use a CSS gradient generator to preview different angles, color combinations, and stop positions. You can then copy the generated CSS into your stylesheet and adjust it to suit your project.

Frequently Asked Questions About CSS Linear Gradients

1. How do I create a linear gradient background in CSS?

Use the linear-gradient() function with a background property.

body {
    background: linear-gradient(to right, blue, purple);
}

This creates a horizontal gradient from blue to purple.

2. Can I create a gradient using hexadecimal color codes?

Yes. CSS gradients support hexadecimal values, named colors, RGB, RGBA, HSL, and other valid CSS color formats.

background: linear-gradient(90deg, #3498db, #e74c3c);

Hexadecimal codes are particularly convenient when you are working with a defined website color palette.

3. How do I change the direction of a linear gradient?

Use directional keywords such as to right, to left, to top, or to bottom. You can also specify an angle, such as 45deg or 135deg.

background: linear-gradient(45deg, #ff512f, #dd2476);

4. How can I create a three-color linear gradient?

Add a third color to the gradient function.

background: linear-gradient(to right, red, orange, yellow);

The browser blends the colors in the order they appear.

5. Can I use a CSS linear gradient on a button?

Yes. Apply the gradient to the button's background property.

button {
    background: linear-gradient(to right, #3498db, #8e44ad);
    color: white;
    border: none;
    padding: 12px 20px;
    border-radius: 6px;
}

For accessibility, also define appropriate keyboard focus and hover states.

6. How do I make a CSS gradient cover the full screen?

Set the element's minimum height to 100vh and remove the default body margin.

body {
    min-height: 100vh;
    margin: 0;
    background: linear-gradient(135deg, #667eea, #764ba2);
}

This allows the background to cover at least the full viewport height.

7. Can I make a linear gradient transparent?

Yes. Use rgba() or modern color syntax with an alpha value.

background: linear-gradient(
    to right,
    rgba(52, 152, 219, 1),
    rgba(52, 152, 219, 0)
);

This transitions from fully opaque blue to transparent. Transparency allows the background beneath the gradient to show through.

8. Do CSS linear gradients affect website performance?

CSS gradients generally avoid the need to download a separate background image, which can reduce asset requests. However, performance depends on the overall page design, device, and complexity of the effects. For most ordinary backgrounds, a simple CSS gradient is an efficient choice.

9. What is the difference between background and background-image for gradients?

Both can create a linear gradient:

background: linear-gradient(to right, blue, purple);
background-image: linear-gradient(to right, blue, purple);

The background property is a shorthand that can set multiple background-related properties. The background-image property sets the background image layer without resetting other background properties.

Conclusion

Learning how to create a linear gradient background in CSS gives you a simple, flexible way to improve your website's visual appearance. With the linear-gradient() function, you can combine colors, control transition directions, position color stops, and design everything from full-page backgrounds to buttons and content cards.

Start with two colors and a simple direction, such as to right or 135deg. Once you understand the basics, experiment with additional colors, transparency, and layered gradients. Remember to prioritize readability, accessibility, and consistency so that your backgrounds support the content rather than distract from it.

Whether you are building a personal blog, business website, portfolio, or landing page, CSS gradients can help you create attractive designs with relatively little code. Test your combinations, refine the colors, and choose effects that suit your website's purpose.

Share this article

System Message