React - Separate colors for different containers

I have been struggling with this issue repeatedly, yet I still haven't found the most efficient solution for it. Imagine having 2 DIVs, A and B, both nested inside a container with columns assigned to each. What would be the optimal way to fill a background color for both DIVs while also occupying the remaining space on the left and right sides of the screen? Essentially, I want the left background color to cover the full width of DIV A plus the remaining unused space on the left (likewise for the right side).

For a clearer visual representation, you can view this image: example illustration

I am aware that using linear-gradient can achieve this effect, but it may pose challenges in different viewport sizes.

I hope my explanation is clear and not overly complicated, and I apologize if it seems trivial.

Answer №1

Why not add some color to the containers and give the div a transparent look?

<div class="colored-container">
  <div class="transparent-div">
    // Content goes here
  </div>
</div>
<div class="different-colored-container">
  <div class="transparent-div">
    // More content here
  </div>
</div>

Answer №2

Alright, my suggestion for achieving this is to create the following structure:

<div style="background: linear-gradient(90deg, #8A66B9 50%, #ffff 50%);">
  <div class="container">
    <div class="row">
      <div class="col-7" style="background-color:#8A66B9">
        A
      </div>
      <div class="col-5" style="background-color:#ffff">
        B
      </div>
    </div>
  </div>
</div>

This setup consists of a parent DIV with a 50/50 background split. The individual background colors are then defined for each part.

Answer №3

One approach is to set the entire container to orange and then use a pseudo element after component A to cover the right-hand side of the container with a blue background. This pseudo element can be made taller and wider than necessary to ensure it covers the desired area.

https://i.sstatic.net/vEj39.png

It's important to note that the positioning and sizing of the components in this example are arbitrary and for demonstration purposes only.

<style>body,
.container,
.A,
.B,
.A::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.container {
  background: orange;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  position: relative;
}

.A {
  width: 30%;
  height: 30%;
  display: inline-block;
  position: relative;
  top: 10%;
  left: 20%;
  border: solid 1px black;
}

.B {
  width: 20%;
  height: 30%;
  position: absolute;
  display: inline-block;
  left: calc(20% + 30%);
  /* just for demo */
  top: 10%;
  border: solid 1px black;
}

.A::after {
  content: '';
  display: inline-block;
  position: absolute;
  top: -100vh;
  left: 100%;
  width: 100vw;
  height: 200vh;
  background: blue;
}

</style>
<div class="container">
  <div class="A">A</div>
  <div class="B">B</div>
</div>

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

Tips for Validating Radio Buttons in Angular Reactive Forms and Displaying Error Messages upon Submission

Objective: Ensure that the radio buttons are mandatory. Challenge: The element mat-error and its content are being displayed immediately, even before the form is submitted. It should only show up when the user tries to submit the form. I attempted to use ...

Why is there a CSS reference failure in Express and how can it be resolved?

Below, you'll find a simple express server I've put together: // check out my basic Express server var express = require("express"); var app = express(); var bodyparser = require("body-parser"); app.use("/public/", express.static("./public/")); ...

Solution to trigger CSS :hover to refresh following a transition (such as expanding a menu)

While there are existing discussions on this topic, I am presenting my query for two specific reasons: It introduces a potential alternative solution The demo code could be helpful to individuals looking to emulate a menu Following a CSS transition, the ...

What could be causing the issue with my Material-ui React combo-box not updating properly?

After putting together a React component with a combo-box, here is the code I came up with: import React from 'react'; import MenuItem from '@material-ui/core/MenuItem'; import TextField from '@material-ui/core/TextField'; ex ...

What steps can be taken to avoid a component, generated from an array, being re-rendered every time the array is altered but the key remains unchanged

<div className="app"> {[1, 2].map((i) => { return <Test key={i} />; })} </div> In order to achieve the desired outcome where the Test component with key=2 does not re-render when the array is ...

Implementing image uploads in a React application on a Digital Ocean hosted server

I am currently working on a project that involves uploading images from a react application. const accessToken = getCookies(null, 'token'); const formData = new FormData(); formData.append('image', imageFile); ...

Switching from ejs format to html

I've been working on a tutorial that uses ejs, but I'm not too familiar with it. I'd like to know how to convert the server.js from using ejs to html. Here's the code snippet: app.set('view-engine', 'ejs') app.use( ...

I came across a wlwmanifest.xml file while browsing through the source directory of my website

<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="../preload/wlwmanifest.xml"> I am curious about the purpose of including this code in my website. If I were to delete this file, how would it impact the f ...

CSS navbar with a unique design: Issue with List Hover not Persisting

I'm facing a common issue with a pure CSS navbar. I have constructed a navbar using ul and li elements, but I am unable to make the menus stay visible when I hover over them. The problem lies in the fact that the menu opens only when I hover over the ...

Tips for designing a tilted CSS layout from scratch

I am looking to design a stylish interface using HTML & CSS. Here is the style I want to achieve: Currently, my interface looks like this: Although these are just reference images, you can see that my design does not have the same sleekness as the one in ...

Enhancing an image with zoom effect in a 2-column layout on hover

Could someone help me achieve a 2-column layout where the left column contains text and the right column an image? I want the image in the right column to zoom when the user hovers over either the left or right column. The issue is that when the user hove ...

I am facing issues with getting create-react-app to function properly on my computer

While attempting to start my React project, an error log appeared C:\Users\Ezhil & rishii>npx create-react-app firstapp 'rishii\AppData\Roaming\npm\' is not recognized as an internal or external command, opera ...

The custom layout in NestJS version 13 failed to display

I have implemented NextJs 13 in my project for building purposes. I am trying to use CustomLayout as the primary layout for my entire website. Even though there are no errors, I am facing an issue where the CustomLayout does not display as expected. ...

Obtain the full HTML code for a Facebook album

I am attempting to extract the HTML code from a Facebook album in order to retrieve the photo links. However, since not all photos load at once, cURL only retrieves the links of photos that are initially loaded. Is there a way to fetch the entire loaded H ...

Eliminate the table background in a single cell

Apologies if the title is not very descriptive, I couldn't find the right words to use. Below is the table in question. I'm looking to remove the white border (background of the table) from just the upper-left cell. I still want the white backgr ...

Rendering a component six times in React

Currently, I am facing an issue with my custom hook that is used to fetch data from an API. The problem arises when I try to integrate this custom hook into my Context API. The main issue is that the body of the hook keeps getting rendered multiple times ( ...

Achieving nested classes in CSS: a comprehensive guide

I am encountering an issue with my CSS while trying to create a dropdown on hover. The problem seems to be related to nested classes inside a div structure. It appears that the dropdown list is not displaying properly, leading me to believe there might be ...

Implementing React component that clears state upon selecting a place with Google Autocomplete

I've encountered a issue while using the Google Autocomplete component. Whenever I select a place and use the onPlaceSelected function to save it into a state array (input) of the parent component, the previous value gets replaced with an empty array ...

Tips for populating an array with information gathered from an axios request within a React application

Currently, I am using axios to fetch data from an API and attempting to store the retrieved data in a variable that represents an array within the state of the component. However, when I try to do so, I encounter the following error: Objects are not valid ...

What Happens When You Click on a Link in Google News Using the Mobile Chrome Browser

When using the Chrome Browser on mobile to open a news link from Google News, a new tab is opened. To return to Google News, users can click on the white-highlighted "left arrow" below or find a small header with a "<" symbol above to navigate back. Ho ...