CSS: Applying a full-width background gradient works perfectly as long as the page remains stationary

Showing two screen captures demonstrating the effect with a small viewport that requires scrolling.

This is how the HTML code appears: (excluding head and html tags)

<body>
 <div id="grad1"></div>
 <div id="wrapper">
 <header>
  <h1 class="logo"><a href="/">Business Name</a></h1>
 </header> 
 <nav>
  <ul>
   <li><a class="first" id="index" href="/index.php">Home</a></li>
   <li><a  id="whatwedo" href="/whatwedo.php">What we do</a></li>
   <li><a  id="communicating" href="/communicating.php">Communicating</a></li>
   <li><a class="last" id="contact" href="/contact.php">Contact Us</a></li>
  </ul>
 </nav>
 <div style="clear:both;"></div>
 <section>
  <?= $content ?>
 </section>
 <footer>
  &copy; 2010
 </footer>
 </div> 
</body>

A snippet of the CSS related to body, grad1, and wrapper is as follows:

body {
 color: #111;
 background-color: #3E9C9D;
}

#grad1 {
 height: 600px;
 position: absolute;
 top: 0;
 left: 0;
 z-index: -100;
 width: 100%;
    background-image: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#3E9C9D));
    background-image: -moz-linear-gradient(#fff 0%, #3E9C9D 100%);
}


#wrapper {
 max-width:960px;
 min-width:840px;
 margin: 0 auto;
}

Any suggestions on how to resolve this issue? The gradient needs to be on a separate div for specifying its height.

(Acknowledging that the CSS gradient may not function in IE - there's a background-image serving as an emulation. However, it encounters the same problem.)

Answer №1

If you're struggling to figure it out, here's the CSS code that should work for you:

body {
 color: #111;
 background-color: #3E9C9D;
 background-image: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#3E9C9D));
 background-image: -moz-linear-gradient(#fff 0%, #3E9C9D 100%);
 background-repeat:repeat-x;
 background-size: 100% 600px;
 -o-background-size: 100% 600px;
 -moz-background-size: 100% 600px;
 -webkit-background-size: 100% 600px;
}

#wrapper {
 max-width:960px;
 min-width:840px;
 margin: 0 auto;
}

Note: This was edited a couple of years later and the gradient syntax might have changed. Make sure to do your research before implementing this code.

Answer №2

When using the CSS property position: absolute, the element's coordinates are based on the viewport when it is rendered.

While there may be a way to work around this issue while still keeping the grad1 DIV, consider placing the background image or gradient in the body tag instead. It seems that the background should cover the entire document regardless.

Answer №3

html {
height: 100%;
}
body {
height: 100%;
margin: 0;
}

.gradient {
background: linear-gradient(top, #243d6e 0%,#031b4d 61%,#011132 100%); /* W3C */
background-repeat: no-repeat;
background-attachment: fixed;
}

For a stylish gradient effect, apply the .gradient class to either the body tag or a container div.

Answer №4

Avoid the creation of web pages that are specific to certain browsers.

To achieve a gradient background that is compatible with all browsers, simply use a background image.

  1. Start by creating a narrow image with a gradient effect, even just one pixel wide.
  2. In your CSS code, specify the background image to repeat only in the x direction and not in the y direction.
  3. Ensure that the body's background color matches the color located at the bottom of the image.

Below is an example of how this can be implemented using CSS:

body {
 color: #111;
 background-color: #3E9C9D;
 background-image: URL(gradient_green.png);
 background-repeat:repeat-x;
}

#wrapper {
 max-width:960px;
 min-width:840px;
 margin: 0 auto;
}

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

"Trigger a hover effect on one element, but have it impact a

I have a simple question that I couldn't find a direct answer to, so here it is: I have two boxes and I want to hover over the first one but have it affect the second one (meaning when hovering over the first box, the second box should scale up with a ...

Infinite Results Caused by Angular Nested *ngFor in an HTML Template

Currently, I am utilizing the Github API to create a Repositories Viewer. The approach involves taking repo.name as an input for another variable and passing it as a parameter to a function that fetches the languages used in a specific repository. However ...

Differentiating the background color of the active tab in Bootstrap while keeping other active classes unaffected

I am currently working with Bootstrap and have created a list as shown below: https://i.sstatic.net/PqBip.png The active state of this list item is blue, but I would like to change it to green or another color. Here is the code snippet: <a class="l ...

Using scale transformations to animate SVG group elements

I am currently experimenting with an SVG example where I hover over specific elements to expand or scale them. However, I seem to have made a mistake somewhere or missed something important. Can someone offer me assistance? View the demo on JSFiddle here ...

How to Link Django URL to an HTML Button

I have the following URL defined in my urls.py file: url(r'^inline-formset/$', 'order.views.addrow', {'form_class': OrderedItemForm}, name='addrow'), Now, I want to link to this URL from a form button, but I am str ...

Guidelines for implementing drag and drop functionality for my specific scenario

Looking to create a restricted area for dragging elements within my app. Specifically, I want the element to only be draggable within a certain defined space. Here is what I currently have: http://jsfiddle.net/L73T5/10/ html <div id='background ...

Stylish CSS menu that adjusts to different screen sizes, featuring a centrally aligned h1

I am trying to center an h1 element in my navigation, alongside two other elements, without using an image. I want it to be responsive as well. I have attempted various methods but have had limited success, as most solutions require the use of background i ...

I am encountering issues with my XPath in certain scenarios

When attempting to do an assertion with the total of a dynamic table, I am encountering difficulties writing XPath that works for all scenarios. The issue arises because the table sometimes contains only 1 image and other times it may contain 5 images. In ...

I'm having difficulty integrating boostrap with my react application due to issues with the css-loader

In my React index.js file, I am importing Bootstrap and using Webpack 4: import React from 'react'; import ReactDOM from 'react-dom'; import './assets/css/index.css'; import App from './App'; import * as serviceWork ...

Steps to creating an elliptical border using CSS

Is there a way to apply CSS styles specifically to the left border of a div to achieve an elliptical shape, while keeping other borders normal? I've managed to create a semi-ellipse with the following code, but the rest of the border remains unchanged ...

Is there a method to ensure that Text First and Image First layouts are consistently displayed in a uniform manner?

When creating a Shopify theme for a client, I encountered an issue with the layout of my liquid file. Specifically, when using the text-first layout, the image in the image-with-text div is partially hidden due to the content that follows it causing the he ...

Utilize jQuery to set a cookie, then apply the bodyclass retrieved from the cookie upon page

I have a button that changes the body class to .blackout For this, I am utilizing js-cookie library to manage cookies. The code snippet associated with my button is shown below: <script> $('#boToggle').on('click', function(e) { ...

Dynamic jQuery snapping effect after scrolling horizontally

I'm currently working on a horizontal-scrolling site that utilizes the mousewheel jQuery Plugin. The scrolling functionality is functional, however, I am looking to implement a feature that snaps each "article" to the left side of the document when sc ...

Clicking on elements within a Scrollview does not work when the Scrollview is set to an

Currently, I am facing an issue with my Scrollview as its children are not clickable despite having a position of absolute. Click events seem to just pass through them and interact with components below. I have searched extensively for a solution online, b ...

Using Vue to display a Div inside a TD element of a table does not result in a reactive behavior

I am encountering an issue with a table where the last column contains a div with three options (View, Edit, and Delete). This submenu is initially hidden, but when clicking on the options button in the last column of the table, the array used to control i ...

Once you utilize the register function with react-hook-form, the input text becomes uneditable

Struggling to configure react-fook-form for form validation, I encountered an issue where the text input (specifically the username field) becomes uneditable after using the register function. Despite my efforts, I can't type anything into it. const { ...

CSS media query for minimum and maximum width seems to be ineffective

Check out this Codepen example: https://codepen.io/codepenuserpro/pen/ExQrEbo HTML: <div></div> CSS: div { height:400px; width:400px; background-color:red; } @media only screen and (min-width: 1068px) and (max-width: 1380px) { backgr ...

Generating a Random CSS Class in JavaScript with Math.random() Function

I'm currently enrolled in a Basic HTML/Javascript course and I'm struggling with how to "modify the following JavaScript so that it picks one of the style classes at random each time it is called." The code provided below is functional, but lacks ...

Tips for adding a CSS class to an HTML element on-the-fly

As a newcomer to JavaScript and AngularJS, my question may be considered naive by some. I am currently working on a tutorial project involving AngularJS. Here is the HTML code snippet I am dealing with: <link href="http://netdna.bootstrapcdn.com/boo ...

Adjusting the value of a variable in an Scss selector will result in a change, regardless of the presence of an element matching the selector

CSS File $dark-mode: false!default; $primary-dark: blue; [dark-mode='dark'] { background: gray!important; $dark-mode: true!global; } [dark-mode='light'] { background: yellow!important; $dark-mode: false!global; } @if $dark-m ...