A CSS hack for showcasing numerous div elements

I am looking to divide my window into two equal parts, with the same height but each taking up 50% of the width.

Here is the CSS I used:

.container {
    display: flex;
    height : 100%;       
}

.column {
    flex: 1;
    height : 100%;
}

.column-one {
    order: 1;

}
.column-two {
    order: 2;        
}

The structure of my HTML looks like this:

<div class="container">

<div class="column column-one"> Column 1
    <div  class="x" id="sliceX" ></div>
    <div  class="y" id="sliceY"></div>
    <div  class="z" id="sliceZ"></div>
</div>

<div class="column column-two"> Column 2
    <div  class="x" id="sliceX2"> </div>
    <div  class="y" id="sliceY2"></div>
    <div  class="z" id="sliceZ2"></div>
</div>

</div>

Everything seems to be working fine so far.

Now, I want my .x to take up 100% of the width and 50% of the height. The .y and .z elements should follow, each taking up 50% of the remaining height. Additionally, both .y and .z should only take up 50% of the width, with .y on the left and .z on the right.

It should look like this:

Col1 Col2
xxxx xxxx
xxxx xxxx
yyzz yyzz
yyzz yyzz

How can I adjust my css to achieve this layout within the current setup of two columns?

Answer №1

If you're looking to improve the layout of your website, one suggestion is to make your .column element a flex container as well and enable wrapping. Adjust the widths of .x to width:100%; and .y, .z to width:50%;, then set all three with a height of 50%.

Here's an example:

.column {
    flex: 1;
    height : 100%;
    display:flex;
    flex-wrap:wrap;
}
.x{
  width:100%;
  height:50%;
}
.y, .z{
  width:50%;
  height:50%;
}

Take a look at this example.

Answer №2

To achieve optimal results, consider implementing a second flex definition in your layout. Utilizing the flex-wrap property can effectively wrap the elements within the layout. Here is an example:

html,
body {
  height: 100%;
  margin: 0;
}
.container {
  display: flex;
  height: 100%;
  align-items: stretch;
}
.column {
  flex: 0 0 50%;
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
}
.x {
  flex: 0 0 100%;
}
.y, .z {
  flex: 0 0 50%;
}
.x {background-color: #ff8080}
.y {background-color: #80ff80}
.z {background-color: #8080ff}
<div class="container">
  <div class="column column-one">
    <div class="x" id="sliceX"></div>
    <div class="y" id="sliceY"></div>
    <div class="z" id="sliceZ"></div>
  </div>

  <div class="column column-two">
    <div class="x" id="sliceX2"></div>
    <div class="y" id="sliceY2"></div>
    <div class="z" id="sliceZ2"></div>
  </div>
</div>

In this implementation, the .x element occupies 100% width, which moves the .y and .z elements to the second row. Subsequently, each of these elements take up 50% of the width, resulting in a visually appealing layout. You have the flexibility to further adjust the sizing proportions to suit your preferences, such as a 30/70 split.

Kudos on utilizing Flex for this task - it's indeed a powerful tool for achieving the desired layout :)

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

Responsive height using Material Design Lite

I have a website using MDL, with a prominent header centered on the page. To enhance visibility, I added a background box behind the text. To view the CodePen example, click here. The challenge is to ensure that when the window is resized, such as on a m ...

Unable to input text into text fields or interact with buttons by clicking or hovering in a React form

I am new to React and currently working on a login page that asks for both a username and password. While I haven't implemented much logic yet, I have been exploring online resources to learn more. It appears that I should be able to interact with the ...

Need help with styling for disabled autocomplete? Check out the attached image

I'm currently working with material-ui and react to create a basic form. Everything is functioning properly except for a small UI issue that I can't seem to figure out how to resolve. When I utilize the browser's auto-complete feature, the i ...

How to center a text box within a div using CSS

Looking for equal margins on top and bottom? Here's how to achieve it: If you have a container div like this: <div id="search"> <input id="txtSearch" type="txt"> </div> Your CSS should look something like this: #search{ m ...

Visibility of text compromised when placing transparent button inside input field

After adding a clear button inside the input box in Angular, I am facing an issue where the text in the input box is being overlapped and not fully visible. Below you will find detailed information on this problem. HTML Code <ng-template pTemplate=&quo ...

Div that scrolls independently without affecting the position of other elements

Visit this link for reference <div class="col-10 content" id="content"> <ul> <li>News</li> <li>News</li> <li>News</li> <li>News</li> < ...

What is the best method for efficiently wrapping contents within a div container?

On the website cjshayward.com/index_new.html, there is a div wrapped around the content of the body, which is approximately 1000 pixels wide. In Chrome and Firefox, the wrapper works perfectly for the top section of about 100 pixels. Further down the page, ...

Enforce a restriction on the user's input value for the amount field within a React application

I'm looking to limit the user from entering more than 50000 in the input value. How can I achieve this using React? I am currently handling this on the onchange event. onPaymentAmountChanged = (e) => { let inputValue = e.target.value; if (i ...

Is it possible for JavaScript to access and read a local file from a web page hosted locally

I am interested in using html, css, and javascript to develop a user interface for configuring a simulation. This interface will be used to generate a visualization of the simulation and an output parameters file based on multiple input files. It is impor ...

Discover the index of the row when the value in the dropdown list is updated

I'm faced with a challenge regarding an HTML Table that contains a dropdown list in every row. I would like the background of each row to change whenever the value in the dropdown list is modified. Below is the code snippet: <table id="table1"> ...

Can a substring within a string be customized by changing its color or converting it into a different HTML tag when it is defined as a string property?

Let's discuss a scenario where we have a React component that takes a string as a prop: interface MyProps { myInput: string; } export function MyComponent({ myInput }: MyProps) { ... return ( <div> {myInput} </div> ...

Calculating the total square footage divided by square feet and the annual rent divided by square feet using Javascript

I need help with running multiple calculations. One of them involves dividing the annual rent by the square feet. However, this calculation fails if a comma or dollar sign is included. I want the rent/sq ft field to display nothing instead of NaN if the fi ...

What could be causing the excess space in the right corner of my website's mobile version?

After creating a practice website deployed on Vercel, I ensured it was responsive by incorporating breakpoints for different screen sizes. However, upon viewing the dashboard page on my mobile device, I noticed some unwanted extra space in the top right co ...

Determine if the webpage is the sole tab open in the current window

How can I determine if the current web page tab is the only one open in the window? Despite searching on Google for about 20 minutes, I couldn't find any relevant information. I would like to achieve this without relying on add-ons or plugins, but if ...

What causes materialui styles to vanish upon refreshing in nextjs?

After consulting the materialui documentation (https://material-ui.com/guides/server-rendering/), I was able to find a solution, but I am still unsure of the underlying reason. Why does the style work initially during rendering but disappear upon subs ...

Retrieving an image from an input file and setting it as the background of a div element

I am currently developing a whack the mole game using Vanilla JS, and I'm attempting to allow players to choose their own target by uploading an image file. However, despite days of searching for a solution, I have not been successful. HTML: <inpu ...

Altering the placement of a single element from floating to fixed positioning in CSS

Currently in the process of designing a basic website, I am looking to modify the behavior of the navigation bar on the left-hand side. I want it to remain fixed in its position so that users can scroll through the page without losing sight of it. My main ...

the box is having trouble with its animation

This issue revolves around the use of CSS3 properties like -webkit-keyframes and -webkit-box-align. To learn more about this problem, please check out http://codepen.io/pleasureswx123/pen/fljFH /* sample css code */ <style> .box { di ...

Guide to creating a responsive layout using CSS and HTML

I am looking to position the main contents (content 1 and content 2) in between two side menus on my exercise page. At the moment, the main contents are overlapping with the two menus. Can someone guide me on how to fix this alignment issue? Thank you. ...

Web Development using HTML and JavaScript

Here is the code for a search bar engine that I am working on. <form id="frmSearch" class="search1" method="get" action="default.html" /> <input class="search" id="txtSearch" type="text" name="search_bar" size="31" maxlength="255" value=" ...