What is the best way to add a gradient background image to a container that also has margins?

I am currently working on a push menu that shifts the main content container by adding margin-left to it. I am aiming to achieve a background image effect similar to what is seen on .

Here is the progress I have made so far in my codepen:

https://codepen.io/Maynards/pen/qBVLqLL

<div class="secondaryMenu">
  <div class="secondaryMenuItem">
    <div>Secondary Menu 1</div>
  </div>
  <div class="secondaryMenuItem active overlay">
    <div>Secondary Menu 2</div>
  </div>
  <div class="secondaryMenuItem active">
    <div>Secondary Menu 3</div>
  </div>
</div>
<div class="primaryMenu">
  <ul class="primaryMenuItems">
    <li class="primaryMenuItem">
      <div class="primaryMenuItemSelector">
        <div>1</div>
        <div class="primaryMenuItemExtra">Hidden Primary Menu Content</div>
      </div>
    </li>

    ... // More HTML code here

  </ul>
</div>

// Continuing with more HTML and CSS code...

In my current work, I encountered difficulties where the background image was not resizing when the main content container was adjusted. Unfortunately, the image no longer appears in the CodePen preview, indicating a step backwards in the process.

EDIT 1: Additional Context & Image Samples

The desired outcome involves having the background image confined to the container beside the menu (where the <p> text is located). However, as it stands now, the background image seems to be truncated on the left side.

Expanding Menu https://i.stack.imgur.com/3Awcn.png

Collapsed Menu https://i.stack.imgur.com/5WF9l.png

Additionally, I have faced challenges in getting the gradient to display on top of the background image and beneath the content.

EDIT: Final Solution

By implementing the solution provided in the accepted answer, I managed to arrive at this result

Answer №1

It seems like there are a few pointers for achieving your preferred layout:

  1. The left navigation should resize fluidly, causing the content on the right to also resize accordingly along with any background images;
  2. The content on the right side should have a background image that is always contained within the width of the content container;
  3. The background image in the content section should be fixed, and a gradient overlay should scroll over it.

This example demonstrates how you can create a layout that satisfies all these requirements. Adjust the --menu-width variable to observe the changes. This should help you get started.

:root {
  --menu-width: 240px;
}

.nav {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 10;
  width: var(--menu-width);
  background-color: #ff0099;
}

.main {
  position: relative;
  margin-left: var(--menu-width);
  min-height: 100vh;
}

.main-background {
  content: ''; 
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: var(--menu-width);
  background-image: url(https://images.unsplash.com/photo-1477233534935-f5e6fe7c1159?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1740&q=80);
  background-repeat: no-repeat;
  background-size: 100% auto;
}

.main::before,
.main::after {
  position: absolute;
  z-index: 1;
  right: 0;
  left: 0;
  content: '';
  background-image: linear-gradient(180deg, rgba(2,0,36,0) 0%, rgba(0,0,0,1) 100%);
}

.main::before {
  top: 0;
  height: 100vh;
  background-image: linear-gradient(180deg, rgba(2,0,36,0) 0%, rgba(0,0,0,1) 100%);
}

.main::after {
  content: '';
  top: 100vh;
  bottom: 0;
  background-color: #000000;
}

.container {
  position: relative;
  z-index: 2;
  margin: 0 auto;
  padding: 1rem 2rem;
  padding-top: 24rem;
  max-width: 960px;
  color: white;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css" rel="stylesheet"/>
<div>
  <nav class="nav"></nav>
  <main class="main">
    <div class="main-background"></div>
    <div class="container">
      <h1>This is a title</h1>
      <p><!-- The rest of the content goes here--></p>
    </div>
  </main>
</div>

To avoid the need for an empty <div>, you could apply the background images as a pseudo-element to the <body>. However, using an empty <div> maintains code flow and context. Here's a Codepen link where you can experiment with the code: https://codepen.io/neutraltone/pen/XWzvYbP

Answer №2

I'm uncertain about the specific goal you are aiming for, but in your amazon illustration, you employ this technique:

div.global_background_class{
    position: fixed;
    left: 0; top: 0;
    width: 100%; height: 100%;
    z-index: -1;
    background-image: url('/path/to/image');
    background-size: cover;
}

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

What is the best way to design a scalable row of square elements using Bootstrap 5?

Looking to create a set of 6 Bootstrap 5 cards in square dimensions to function as buttons. Each card should be col-xl-2 wide and exactly square in height. I've started creating the cards with the code below. Can anyone provide guidance on how to ach ...

Preventing Content Jumping When Concealing Elements: Tips and Tricks

I've been working on a project at , where you can click on a small map to expand a larger map below it. However, I noticed that when you scroll down to the large map and then try to close it using the button at the bottom, the content on the page jum ...

Tips for showing the chosen value of a ModelChoiceField in Django

Is there a way to display only the selected value from a forms.ModelChoiceField on a view page? I'm struggling to find a clear solution as a Python newbie, despite searching through various forums. Here is the form code snippet: class Manufacturer1F ...

The implementation of modal pop-ups is resulting in unresponsive touch areas on mobile devices and tablets

I recently finished putting together a one-page website using Twitter Bootstrap, based on a theme from Themeforest that I heavily customized. However, when viewing the site on a mobile or tablet, I noticed an issue where the top 2/3 of the page is unrespon ...

Utilizing Jquery for Enhancing Annotations

I am in the process of developing a website for essay writing tests. I have implemented a feature where users can input their essays using a text area, and now I want to be able to make comments on that text similar to PDF annotations or highlighting. I at ...

Increase the padding for each child element within its corresponding parent

Is it possible to add padding to child items within each "folder" element without using JavaScript if there is more than one? This scenario would be hardcoded as follows: folder folder inner { padding-left: 14px; } folder folder folder inner { pad ...

Encountered an error while trying to click the cookie button using Selenium: "object[] does not have a size or

I've been struggling to interact with a button inside a pop-up using Selenium, but I keep encountering this error: object [HTMLDivElement] has no size and location I initially tried to simply click the button since it is visible on the page and I wai ...

Is it possible to move a directive within a limited parent element?

Is there a way to limit the movement of an angular.js drag directive within the confines of its parent element? You can see the problem I'm facing in this jsbin: http://jsbin.com/maxife/3/edit?html,css,js,output ...

Using CSS to style an alternate list elements to float

I'm working on a webpage that displays messages from a database using ajax. Currently, the output is just stacked downwards. I'm hoping to style the list elements to alternate from left to right, similar to an iOS Message chat. Does anyone have ...

Setting the height and width of a table and utilizing rowspans may not produce the desired outcome

I've encountered an issue with my table that I can't seem to resolve. The CSS should dictate the width and height, but something seems off. Am I approaching this incorrectly or is there something missing? Also, why are the borders not aligning p ...

Can you guide me on where to find the login with Facebook button on this site?

I am currently working on integrating a Facebook authentication system into my app, but I find the default log in button provided by Facebook developers section to be quite unappealing. My Question: I am curious about how websites like Stack Overflow man ...

How to use Vue.js to automatically redirect to a URL when a button is clicked

I have created a button that, when clicked, should redirect to a URL passed in it. Below is the code snippet: <template> <button type="button" v-on:click="gotosite(modalDetails.final.product_url)">Buy</button> </template> <s ...

"Pairing div/span elements with sprites for improved web

I've been updating my website by replacing most inline images with sprites. Here's a basic CSS code snippet for the sprite class: .sprite{ background-image:url(...); background-position:...; width: 16px; height:16px; } After learning that nest ...

"Send the selected radio button options chosen by the user, with the values specified in a JSON format

My current task involves inserting radio button values into a MySql database using Angular. The form consists of radio buttons with predefined values stored in a json file. Below is an example of how the json file is structured: //data.json [{ "surve ...

Inject HTML code containing a function that triggers upon button press

I am facing a challenge in inserting code through a submit button. Essentially, I am looking for this button to trigger a PHP function that adds a block of HTML code. Below is the function: function addTrainee(){ print_r('<form method="post&qu ...

Can CSS be utilized to define the dimensions and background of a <div>?

Does applying inline styles like <div style="width: ;height: ;background: "> fall under the realm of CSS? ...

Ways to determine if an element has exceeded its container's boundaries

After creating the codesandbox, I have developed a webapp that heavily relies on user input. To keep it simple for demonstration purposes, I am displaying various authors on an A4 formatted page using `page` and `font-size` with the `vw` unit for responsiv ...

Modify the text when clicked on, but do not change the span

I'm currently attempting to change the text within an anchor (<a href=>) element when another element is clicked. The structure of the HTML is as follows: <h1> <a href=""> replace this text <span class="breadcrumb" ...

Tips for utilizing ng-model in an Angular application

<input type="search" placeholder="{{'COMPONENT_PROPERTIES.SEARCH_ICON' | translate}}" ng-model="icon.name" list="classIcon" ng-change="changeFn(icon.name)"> <i class="{{$select.selected}}"></i> &nbsp;&nbsp; {{$s ...

Ensuring a dependable detection of WebSocket connection status

I've been researching how to create a dependable method for recovering a WebSocket connection. After exploring various options, I discovered that one approach involves sending heartbeats (ping/pong) to the server and monitoring if the entire pong is ...