Wrapping two divs in CSS without altering the HTML code is a convenient

Having a display flex with three divs, which is a shared component among multiple teams, makes it challenging for me to edit.

My only option is to update the CSS. In the fiddle, the first part displays the current layout while the second part shows the expected layout.

Using CSS only, I am required to modify the following:

<div class="flex">
  <div class="square blue">
    x
  </div>
  <div class="square red">
   x
  </div>
  <div class="square yellow">
    x
  </div>
</div>

In order to achieve the desired rendering as shown in the second html snippet (Refer to JSFiddle) :

<div class="flex">
  <div class="square blue">
  x
  </div>
  <div class="square">
   <div class="square red">
    x
   </div>
   <div class="square yellow">
    x
   </div>
  </div>
</div>

https://jsfiddle.net/h8o523tL/33/

Answer №1

To achieve your desired layout, you can utilize CSS grid in the following way:

.flex {
  display: grid;
  grid-template-areas: 
   "blue red" 
   "blue yellow";
}

.blue {
  grid-area: blue;
  background-color: blue;
}

.red {
  grid-area: red;
  background-color: red;
}

.yellow {
  grid-area: yellow;
  background-color: yellow;
}
<div class="flex">
  <div class="square blue">
    x
  </div>
  <div class="square red">
    x
  </div>
  <div class="square yellow">
    x
  </div>
</div>

UPDATE

If you need more browser support, consider this alternative approach:

.flex {
  position:relative;
  overflow:hidden;
}
.blue {
  position:absolute;
  height:100%;
  top:0;
  left:0;
  width:50%;
  background-color: blue;
}

.flex:before {
  content:"";
  min-height:1px;
  width:50%;
  float:left;
}

.red {
  background-color: red;
  width:50%;
  float:left;
}

.yellow {
  background-color: yellow;
  width:50%;
  float:right;
}
<div class="flex">
  <div class="square blue">
    blue
  </div>
  <div class="square red">
    red
  </div>
  <div class="square yellow">
    yellow
  </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

Navigating up and down effortlessly using bootstrap

My webpage has a collapsible form located near the bottom, but when it's opened users must scroll down to see all of it. Is there a way to automatically scroll down when it's opened and then scroll back up when closed? Take a look at my code: & ...

Enhance the input by incorporating more fields without altering the existing content

Currently, I am working on creating an input form that includes multiple fields and has the capability to generate a preview of the entered content in a separate div. One key feature I would like to implement is allowing users to add additional fields as n ...

What is the best way to adjust the visible height of a div and enable scrolling to view the rest of the content?

My challenge lies within a <div id='x' /> that has a height of 300px Within this div, I aim to place another <div id='y' /> with a height of 600px The issue arises when the Y div displays in its full height rather than up ...

Using a variable with the :contains selector

function checkAndUpdateStatus(newName) { $('#myTable').children('tr').remove(":contains('" + newName + "')"); }; The function above is attempting to remove table rows in 'myTable' containing a string matching the ...

Clicking the save button in the modal updates the text on both the current tab and any previously clicked tabs

I'm looking to implement tabs that, when clicking on the edit icon, open a modal for editing the content of each tab. The issue I'm currently encountering is that if I open the editor for one tab, close it without saving, then open the editor fo ...

Tips for enhancing the presentation of JSON information

I recently ventured into the world of JSON and JS, managing to create a JSON file to showcase data using .onclick event. While I have successfully generated and displayed the JSON data on screen, my next goal is to present it in a table format for better c ...

Bootstrap 4 Cards in a single line

I'm attempting to arrange my cards (panels) side by side and I am utilizing the templates provided on mdbootstrap.com. Here is my code: <div class="row"> <div class="col-md-12"> <div class="card mdb-color lighten-2 text-cent ...

What is the process for translating a single line of code from jQuery to vanilla JavaScript?

How should the code block $('#output').on('input ','.dynamic-Qty', function(e){ be handled? $('#output').on('input ','.dynamic-Qty', function(e){ let z = e.target.dataset; console.log(z.id) conso ...

What is the best way to remove table cells from a TableBody?

Currently, I am in the process of designing a table to display data retrieved from my backend server. To accomplish this, I have opted to utilize the Table component provided by Material UI. The data I retrieve may either be empty or contain an object. My ...

The scrollbar remains unresponsive and fixed until the screen size is adjusted

Need some assistance with a website project for a client. The scrollbars on each page are not loading or changing height until the window is resized. I have a jQuery scrollbar in place, but disabling it does not solve the issue as the normal scrollbar beha ...

Image missing aria-label attribute

I am facing an issue with getting the aria-label to display on my image. Despite checking my code, I cannot figure out why it is not working. Any ideas on what might be missing? Here is the code from my NextJS app: The svg in my public folder. <?xml v ...

Ways to automatically assign default select options using an array

When fetching options and values from my database in the form of an array, I aim to have these options/values pre-selected by default in a multiple select list for users to view and update their data as needed. //data stored in the database $mytitle = a ...

When an image is clicked, I would like it to redirect to a different page

In my slider image list, each image has an ID. If the ID becomes equal to a specific number, such as 24, I want that particular image to move to another page when clicked. Here is the code snippet: <?php $sql = "select * from ".$tblImages." Where cid= ...

Markdown boasts of a sturdy partially-horizontal line

Is there a way to create a partially colored line in Markdown? I attempted using the following code: <hr style="border:0.3px solid green; width:40%"> </hr> However, instead of a partial green line, I am seeing a full gray line. Any idea on w ...

Content in tab remains stagnant

I am having trouble creating different tabs on a page with unique content in each tab's body. Whenever I click on a new tab, the body content remains the same. I'm not sure if it's an issue with how the tabs are set up in the HTML or if ther ...

What is the best way to showcase data from an external model without having to constantly refresh the page?

I am attempting to showcase a User's name at the top of a box where they input their Employee # in a form, all done without needing to refresh the page. The process is simple: the user enters their # and upon clicking or tabbing onto the next field, ...

What is the best way to ensure my images are centered when transitioning to full screen mode?

While working on my site with a grid of images, I noticed that the h1 element centers itself when I go into full screen mode, but the grid of images does not. It seems like the grid is not responsive. How can I ensure that the grid of photos stays centered ...

"Troubleshooting problem with aligning divs in Angular using

I need to design a grid layout for displaying cards with responsiveness, adjusting the number of cards per row as needed. I am currently using angularfxlayout, but facing an issue where the divs are not aligned properly between rows. Current Layout: http ...

Challenges with finding web elements using Selenium

Having trouble finding a specific web element on a dynamic website? The elements you're looking for have similar attributes to others, with slight differences like changing integers. Unfortunately, these integers also change upon page refresh, making ...

Loss of styling is observed with jQuery's html() function

Here is the HTML code I am working with: <div class="myList"> <select> <option value="1">Item1</option> <option value="2">Item2</option> </select> </div> Everything looks great in terms of CS ...