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.

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

The scaling transformation is not accurate for uneven pixel widths

I'm attempting to resize a div while keeping the inner element in the same position and at the same size. To achieve this, I am using transform: scale(value) on the wrapper and transform: scale(1/value) on the inside div. The issue arises when the in ...

Show basic HTML elements on a single page of an Angular Material web application

I am working on an Angular (6) web app with a customized theme using Angular Material. Currently, I am trying to incorporate a popup dialog that includes basic HTML elements like checkboxes, buttons, and inputs. Even though I have successfully displayed ...

The order of event handlers in jQuery

I am currently setting up event binding for a text element dynamically. Take a look at the following code snippet: <input type="text" id="myTxt" /> <script type="text/javascript"> function attachEvent1(element){ element.keyup(func ...

Is it possible to set the input form to be read-only?

I need to create a "read-only" version of all my forms which contain multiple <input type="text"> fields. Instead of recoding each field individually, I'm looking for a more efficient solution. A suggestion was made to use the following: <xs ...

Problem with icon color not being applied to lightning-tab component in LWC

.html <lightning-tabset variant="vertical" > <lightning-tab class="class1" icon-name="utility:adduser" label="demo label1"> demo label1 </lightning-tab> <lightning-tab class=" ...

Manipulating jQuery Element Values

I have a set of checkboxes that I need to bind to my MVC model. To achieve this, I am trying to capture the checkbox values and store them in a hidden input field with the appropriate Id for binding to my model. @foreach (var answer in question.Answers) { ...

Processing an HTML form submission with the help of PHP and AJAX

Having trouble saving basic email data from a website to a MySQL database. I've written the code, but it's not functioning correctly and I can't seem to figure out why. <form class="subfield"> <input type="text" id="em ...

Decrease in internal width

Seeking assistance to adjust the internal border width for better text alignment. I've been struggling with this issue as a beginner in the web development field and would appreciate some guidance. Link to image Here is a snippet of my HTML code: & ...

The placeholder string is being accepted as the input value for the number

My issue is with a form input of type number. When the form is submitted without entering any number, it treats the placeholder text "rating" as the value. How can I stop this from happening? I need to make sure that the input number field is marked as in ...

Steps to turn off see-through selection in Material-UI

I am facing an issue with displaying select options using Material UI in my ReactJS application. The options appear to be transparent, making it difficult for users to see them clearly. Is there a way to remove this transparency from the options? Below is ...

Utilize a DIV element to apply a class to CKEditor

Currently, I am utilizing CKEditor in DIV mode instead of an IFRAME and I am facing a challenge in assigning a class to the editor itself. While I have managed to add classes to elements within the editor, figuring out how to assign one to the editor itsel ...

Chat box custom scrollbar always positioned at the bottom

I have a personalized chat box where I included overflow-y for scrolling functionality. However, every time I input a message in the text box, it displays at the top of the scrollbar window. Is there a way to automatically show the most recent message I ...

The NodeJS server encountered an issue while trying to load the XMLHttpRequest from the localhost. The specified link type of

When accessing the same link to a jsx file (containing ReactJS code) online, everything works fine. However, when attempting to open it on NodeJS localhost, an error occurs: "XMLHttpRequest cannot load http://.../js/r1BodyBabel.js. No 'Access-Contro ...

What strategies can be utilized to raise the max-height from the bottom to the top?

I am facing the following coding challenge: <div id = "parent"> <div id = "button"></div> </div> There is also a dynamically generated <div id="list"></div> I have successfully implem ...

What is the process for implementing the Google Analytics tag on a Streamlit-built website?

I want to monitor my Streamlit UI in Google Analytics. To achieve this, Google Analytics requires the following code snippet to be inserted into the <head> section of the HTML file. <script async src="https://www.googletagmanager.com/gtag/js? ...

Encountering an error message stating "The variable 'App' is declared but not used" when running the index.tsx function

This React project is my attempt to learn how to use a modal window. The tutorial I've been following on YouTube uses JavaScript instead of TypeScript for their React project. However, I'm facing some challenges. Could you possibly help me ident ...

When the user clicks on an element, my JavaScript code dynamically updates the CSS property by changing the window

When a tag is clicked in HTML triggering an onclick event, the CSS property that was updated in the JavaScript does not persist. The changes appear momentarily and then disappear once the window is refreshed. Javascript: <script type="text/javascript"& ...

There appears to be an issue with the functionality of the Bootstrap toggle tab

Currently, I am facing a toggle issue and having trouble pinpointing the root cause as there are no errors appearing in the console. The problem involves two tabs that can be toggled between - "pay" and "method". Initially, the default tab displayed is "pa ...

When consecutive DOM elements are hidden, a message saying "Hiding N elements" will be displayed

Provided a set of elements (number unknown) where some elements should remain hidden: <div id="root"> <div> 1</div> <div class="hide"> 2</div> <div class="hide"> 3</div> <div class="hide"&g ...

tips for implementing Material-UI's grid system

I am working with a component that has a grid layout structured like this: Component Region: <Grid container spacing={10} > <Grid item xs={4} ></Grid> <Grid item xs={4} ></Grid> <Grid item xs={4} > ...