Home > Web Front-end > CSS Tutorial > Why Does My Gradient Background Disappear When Using Absolute Positioning?

Why Does My Gradient Background Disappear When Using Absolute Positioning?

Mary-Kate Olsen
Release: 2024-10-29 13:36:29
Original
418 people have browsed it

Why Does My Gradient Background Disappear When Using Absolute Positioning?

Gradient Disappearing with Absolute Positioning: A Resolution

In this technical inquiry, we seek to understand why a linear-gradient background disappears when an element is positioned absolutely. The goal is to create a centered header that remains in the center of the viewport, regardless of the screen resolution.

The provided code snippet exhibits the issue. When the header is given an absolute position, the gradient background vanishes. This is because the element is removed from the normal flow of the document, leaving the background behind.

To resolve this issue, it is necessary to add some height to the body element. This will force the background to render properly, making it visible even when the header is absolutely positioned. The modified CSS code is provided below:

<code class="CSS">body {
  background: linear-gradient(20deg, #B7B0F6, #B1D5F9);
  min-height: 100vh;
}</code>
Copy after login

By adding min-height: 100vh, we ensure that the body has a height equal to the viewport height. This ensures that the gradient background covers the entire viewport, regardless of the size of the header.

With this modification, the header will now be centered vertically and horizontally in the viewport, even at different screen resolutions. The background gradient will also be visible and span the entire viewport.

The above is the detailed content of Why Does My Gradient Background Disappear When Using Absolute Positioning?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template