What is the proper way to apply a background color to the entire body using CSS?

I'm facing an issue with getting the background color to cover the entire body of my webpage. Currently, it only captures a certain size and when there is scrolling on the window, two shades of colors appear. I want the background color to span the entirety of the body. Is this achievable? Below is the code I am currently using.

Screenshot

lang-css

  
body {
  background: repeating-linear-gradient(-155deg, #ea4853, rgba(125, 44, 160, 0.9));
  background-repeat: repeat;
  width: 100%;
  min-height: 400vh;
  background-size: cover;
  background-attachment: fixed;
}
<div class="center-content">
    <div class="flex-column" style="margin-top: 50px;">
        <div class="container">
            <div class="row justify-content-center">
                <div class="col-md-12">
                    <div class="card">
                        <h1>Historial </h1>
                        <div class="card-body" style="margin-top: -90px;">
                            <div class="row">
                                <div class="col-md-6" style="margin-top: 80px;">
                                    <div style="display: block;">
                                        <canvas baseChart width="400" height="300" [datasets]="lineChartData" [labels]="lineChartLabels" [options]="lineChartOptions" [colors]="lineChartColors" [legend]="lineChartLegend" [chartType]="lineChartType" (chartHover)="chartHovered($event)" (chartClick)="chartClicked($event)"></canvas>
                                    </div>
                                </div>

                                <div class="col-md-6 table-log">
                                    <table class="table table-responsive table-condensed">
                                        <tr>
                                            <th *ngFor="let label of lineChartLabels">{{label}}</th>
                                        </tr>
                                        <tr *ngFor="let d of lineChartData">
                                            <td *ngFor="let label of lineChartLabels; let j=index">{{d && d.data[j]}}</td>
                                        </tr>
                                    </table>

                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Answer №1

Consider updating your height: 100%; to height: 100vh;

However, switching to height: 100vh; may not fully resolve the scrolling issue

In my opinion, a better approach would be:

body {
  background: repeating-linear-gradient(-155deg, #ea4853, rgba(125, 44, 160, 0.9));
  background-repeat: repeat;
  width: 100%;
  height: 100vh;
  background-attachment: fixed;
}
<h1> HI!</h1>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> <br /> <br /><br /><br /><br /><br /><br />
Oooooh Yeaaa!

Key modifications include:

  height: 100vh;
  background-attachment: fixed;

This adjustment will set the body height to 100vh, matching the view height of the screen.

Additionally, setting background-attachment: fixed; ensures that the background remains in place even during scrolling.

Why did I use multiple <br /> tags?

Although one could use min-height: 400vh; for scrolling purposes, it wouldn't address the underlying issue. Maintaining 100vh is essential for resolving the problem.

Alternatively

You can enhance the body CSS with background-size: 100vw 100vh; / background-size: 100% 100%; / background-size: cover; alongside min-height: 400vh;

A more concise version:

body {
  background: repeating-linear-gradient(-155deg, #ea4853, rgba(125, 44, 160, 0.9)) no-repeat;
  width: 100%;
  min-height: 400vh;
  background-size: cover;
  background-attachment: fixed;
}

Using background-size: offers broader browser compatibility compared to vh. Refer to Can I Use (Background-size) | Can I Use (Vh) for more details.

Further Reading:

https://www.w3schools.com/cssref/pr_background-attachment.asp

Answer №2

Check out the Codepen link below to view the result: https://codepen.io/nemanjaglumac/pen/eQQMda

Here is the code snippet:

<body>
  <div>Just a spacer</div>
</body>

$colorPrimary: hsla(282, 57%, 40%, 0.9);
$colorSecundary: hsla(356, 79%, 60%, 1);

body {
  width: 100vw;
  min-height: 100vh;
  background: linear-gradient(-155deg, $colorSecundary, $colorPrimary) no-repeat ;
  background-size: cover;
}

div {
  height: 110vh;
}

If you're looking for more information, check out the following resource: MDN: CSS Background

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Numerous hyperlinks leading to different products within the same image

I am looking for a solution to create clickable links on a picture that contains multiple items (3 in my specific case, as shown in the image above). Currently, I am using position:absolute to place links on each item, but this method is causing overlap ...

Efficiently centering content in a grid layout using automatic fit repetition for optimized responsiveness

I've implemented a responsive grid where each item has its own hidden details section that is revealed upon clicking the item. The structure of the HTML/CSS setup is as follows: <div class="grid"> <div class="item"> ...

What is preventing NgClass from applying the CSS styles?

I'm currently facing an issue in Angular2 where I am trying to apply different styles using ngClass within an 'NgFor' loop, but for some reason, it's not working as expected. Apologies for any language errors. <div class='line ...

Interact with the :before pseudo element when hovering over an element

Is there a way to manipulate the :before pseudo element when hovering over the main element? My goal is for the pseudo element to change its height when I hover over the main element. This is how my code looks: HTML <div class="object">& ...

Utilizing multiple address parameters within a single-page application

Apologies for the lengthy post. I have encountered an issue with my code after running it through a bot that I can't seem to resolve. In my code, I aim to create functionality where you can visit the address #two, followed by two separate parameters ...

The SVG path is not aligned properly with the container's viewbox dimensions

Recently, I came across an icon provided by a plugin I am using called calendar. However, I wanted to enhance my icons set with a new addition - the twitter icon. svg { display: inline-block; height: 16px; width: 16px; margin: 2px 0; ...

The deployment of the Angular application to Azure using Github Actions was unsuccessful due to the lack of a package.json file

When attempting to deploy an Angular(10) WebApp to Azure using Github Actions for CI/CD, I encountered an error indicating that the package.json file could not be found during the execution of the npm install command. Below is my yml file: name: Deploy to ...

Guide for using a CSS variable in a dynamic CSS class within a dynamic component

I'm working with library components and running into an issue with CSS when importing the same component multiple times within a parent component with different styles. import "../myCss.css" const CircleComponent = ({size , color}) => { ...

Steer clear of pre-made CSS divs when incorporating them into your

I have created a CSS file that includes the following styles for div elements: div { height:auto; float:left; padding:0px; margin:0px; overflow:hidden; text-align:left; width:auto; } img { border:none; } ul { padding:0; margin:0; } ...

Creating an HTML template in an Angular service and utilizing it as an HTMLTemplateRef

I'm currently working on a major project that has a specific requirement. As part of this project, I am utilizing a library which allows me to open dialog (modal) popups. In order to do so, I need to configure some options for the modal opening proce ...

Ways to adjust the width of grid columns within an implicit row

I am curious about changing grid columns on an implicit row. While I won't provide the exact code that I'm working on, I will offer a brief example to clarify my question. <div class='container'> <p>item-1</p> <p& ...

The flash movie covers part of the menu that is relatively positioned

While designing a website, I encountered an issue with the Flash slider (CU3ER) overlapping the menu bar. Despite trying various z-index configurations to ensure the drop-down menu stays on top, none of them seem to work. Initially, the drop-down menu rem ...

Ways to showcase alerts on dashboard

I have a specific need to incorporate a notification bell icon in my dashboard. Essentially, I want the user to be alerted about any actions that they need to take through this notification system. For example, if a task is rejected, approved, or pending v ...

Drawing a personalized cursor using JavaScript on a canvas

I've been working on creating a canvas that allows for drawing in vanilla JavaScript. Although I've successfully enabled drawing on the canvas, I'm now looking to implement a feature where a preview dot shows up when the user presses down be ...

What is the best approach to display a fluctuating price depending on specific options chosen in Next.js?

When working with 2 different select tags and rendering images based on the selection, I also want to display a price determined by the options chosen. For instance, selecting "Your Current Division" as Iron and "Your Desire League" as Bronze/Silver/Gold s ...

The jQuery method .on gathers and retains click events

I created a component that manages a view containing articles with games. In order to prevent memory overload and optimize performance, I implemented a solution where when a user clicks on an article (each having the class "flashgame"), they can choose to ...

A common inquiry: how can one pinpoint a location within an irregular figure?

I have been studying Html5 Canvas for a few weeks now, and the challenge mentioned above has puzzled me for quite some time. An irregular shape could be a circle, rectangle, ellipse, polygon, or a path constructed by various lines and bezier curves... I ...

Guide to creating unit tests for a button click function in an Angular project

I wrote some code for unit testing, but it doesn't seem to be working. I'm new to unit testing and would appreciate some guidance on how to use it. COMPONENT.HTML <button class="set-button" (click)="clickFunction()"> SA ...

Angular 6.0.2 - No specific target found in npm error

After checking the release schedule of Angular, it seems that Angular 6.0.2 has been announced as a stable version. However, I encountered an error named 'notarget' when trying to install this version using npm (error message displayed below). F ...

Dynamic Content Sorting with JavaScript and jQuery

I am presenting various courses that are available, and I am utilizing JavaScript/jQuery to display or hide them based on element classes. You can view the page here. Currently, the script conceals all content on the page and then reveals items that matc ...