Maintaining Flexbox layout without triggering item re-rendering for a new container

This is the unique layout I'm aiming to create:

I am facing a challenging flexbox layout that needs to be implemented. One of the items in this layout is a Webgl player, which cannot be conditionally rendered due to the restarting issue it may cause. Each item should have the ability to occupy position 1 when clicked on. How would you approach coding this flex layout while considering that item 2 can only change its order, not its parent?

I experimented with making item 2 absolutely positioned but found it to be less than optimal.

Answer №1

Here's a demonstration showcasing how to achieve your design objectives through css grid

const find = value => [...document.querySelectorAll(value)];

find("section").forEach(el => 
  el.addEventListener("click", () => {
    const isTwo = el.classList.contains("two");
    const isThree = el.classList.contains("three");
    const isFour = el.classList.contains("four");
    const isFive = el.classList.contains("five");
    const currentOne = find(".one")[0];
    if (isTwo) {
      el.classList.remove("two");
      currentOne.classList.add("two");
    }
    if (isThree) {
      el.classList.remove("three");
      currentOne.classList.add("three");
    }
    if (isFour) {
      el.classList.remove("four");
      currentOne.classList.add("four");
    }
    if (isFive) {
      el.classList.remove("five");
      currentOne.classList.add("five");
    }
    currentOne.classList.remove("one");
    el.classList.add("one");
  })
);
body {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: 1fr 1fr 1fr;
  height: 100vh;
  margin: 0;
}

section {
  grid-column: span 2;
  display: grid;
  place-items: center;
  font-size: 18pt;
  transition: font-size 200ms;
  user-select: none;
  cursor: pointer;
  background: var(--color);
  order: var(--order);
  color: white;
  font-family: sans-serif;
}

.one {
  grid-column: span 4;
  grid-row: span 3;
  cursor: unset;
}

.four, .five {
  grid-column: span 1;
}

section:hover {
  font-size: 30pt;
}

.one { 
  --order: 1;
}
.two { 
  --order: 2;
}
.three { 
  --order: 3;
}
.four { 
  --order: 4;
}
.five { 
  --order: 5;
}

.red {
  --color: red;
}

.blue {
  --color: blue;
}

.green {
  --color: green;
}

.yellow {
  --color: yellow;
  color: black;
}

.purple {
  --color: purple;
}
<section class="one red">1</section>
<section class="two blue">2</section>
<section class="three green">3</section>
<section class="four yellow">4</section>
<section class="five purple">5</section>

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

Bring to the front the div altered by a CSS3 animation

Recently, I encountered an issue with a div card that triggers an animation when clicked. The animation involves the card scaling up larger, but the problem arises as its edges get hidden under other cards. To visualize this, take a look at the screenshot ...

I am in need of converting this code from a Class component to a functional component

I am facing a challenge in converting this code from a Class component to functional components. Despite searching for solutions, I have been unable to find a suitable answer. const App = () => { const [user, setUser] = useState(''); cons ...

iPad problem resolved: Disable click hover and double-click issue

I'm experiencing a problem with my web application specifically on Safari for iPad. I have to click twice in order to actually perform the click on the <a> tag. The first click triggers a hover effect, similar to when you hover with a mouse on d ...

Adjust the image size without losing sharpness

I'm currently working on a web application for Minecraft and I am looking for a way to resize someone's skin without losing quality. I believe javascript might be the solution to this issue. ...

Guidelines for sending data as an array of objects in React

Just starting out with React and I have a question about handling multi select form data. When I select multiple options in my form, I want to send it as an array of objects instead of an array of arrays of objects. Can anyone help me achieve this? import ...

Implementing promise-based looping for multiple GET requests in node.js

As a newcomer to promises, I've been searching for the right answer or pattern without much luck. Currently using node.js v4.2.4 and exploring The task seems simple enough... I need to execute multiple asynchronous blocks in a specific order, with on ...

Why does Axios keep timing out despite successful testing in Postman?

Trying to set up a single request for my app using axios with express/node js. Here is the code snippet that was generated through the Postman app. I have attempted different variations by creating my own form, but I always end up with the same result. co ...

Automated resizing for various monitor dimensions in HTML

Is there a way to dynamically adjust the zoom level on an HTML webpage based on the monitor size? For instance, an image that appears large on a laptop screen may look small on a PC monitor. Is there a solution available to scale the picture size as the mo ...

Image pop-ups that overlay text on the homepage

I'm facing an issue and I'm looking for a solution... Upon entering my homepage, I would like to display a popup image showcasing a new event so visitors can see it before accessing the website. I attempted to achieve this using JavaScript but w ...

Using Vue.js to eliminate duplicate values from a filtered array of objects

How can I eliminate duplicate data from a v-for loop in Vue.js? I have an array of clients and another array of categories. When filtering the categories based on clientIDs, I noticed that there are duplicates present. Please choose a client from the opti ...

When using addClass("test"), it throws an error message: TypeError: undefined is not a function

Upon examination in the console, I discovered the following: $(".myCssClass")[0].parentNode <li><span class="myCssClass">some text</span></li> I am attempting to add a CSS class to the parent span tag within the <li> element ...

What is the best way to display the remaining items in an array when a user clicks the next button?

Having a total of 12 cameras, I want to be able to select 4 cameras from a drop-down menu option and have only those 4 cameras displayed. Then, when I click on the next button, I need to show the remaining cameras in the selected layout format. How can thi ...

Update Json information within VueJsonCSV and VueJS component

I'm currently using the VueJsonCSV component to export data to a CSV file. The values being exported are retrieved from the Vuex Store. <template> <v-btn depressed> <download-csv :data="json_data"> Export Files </downl ...

Angular2: Issue encountered while processing click event

When I click a button on my client application, it sends a request to the server I created using Express. The request handler in the server simply logs 'Delete from server' every time the button is clicked. I am encountering these errors when cl ...

Issue with Bootstrap 5 navbar dropdown: It opens easily but struggles to close

I am currently using Bootstrap 5 to design a website and have incorporated a dropdown feature into the navigation bar. On desktop, when hovering over the dropdown, it should open, while on mobile, clicking the dropdown should toggle its visibility (open/cl ...

Creating a consolidated HTML table by extracting and comparing data from various JSON files

Being new to JS and JSON, I am struggling to find a suitable solution that works for me. I have two distinct json files. The first one: players.json contains the following data: { "players": [ { "id": 109191123, "surnam ...

Utilizing React and Google Code to Enhance Lead Conversion Pages

I have developed a basic react application featuring a contact form. Upon submission, I aim to display the Google Code for the lead Conversion Page within the application. <!-- Google Code for Purchase Conversion Page --> <script type="text ...

Hide the border below the active tab

Could anyone assist me in removing the border below the selected tab? I've attempted multiple approaches without success. I experimented with adding a negative margin to the tab and creating a white border, but as the border I want to conceal is from ...

The functionality of $watch in AngularJS is not meeting the desired outcomes

Within my controller, I am looking to receive notifications when the value of a certain variable changes. Specifically, I want a function to be triggered whenever this variable is updated. To achieve this, I am utilizing the $watch method in AngularJS. Her ...

Clicking within the text activates the dropdown menu, but clicking outside the text does not

My custom drop down menu is not functioning properly. When I click on the text, it successfully links to another place, but when I click beside the text, it does not link. Can you please help me identify what's wrong here? Your assistance would be gre ...