Arranging divs in various sequences using CSS flex positioning

I am attempting to achieve a specific css layout using Flexbox (as an alternative to a css grid layout), but I am unsure if it is feasible to create what I have envisioned.

The HTML structure I am working with is as follows:

<div class="container">
    <div class="div1">DIV 1</div>
    <div class="div2">DIV 2</div>
    <div class="div3">DIV 3</div>
</div>

Here is the layout I am aiming for (easily achievable with css grid):

https://i.sstatic.net/JjiIP.jpg

Furthermore, I would like divs 1 and 3 to remain fixed when scrolling, while the content of div2 should be scrollable.

I am uncertain if this can be accomplished; I have experimented with various flex settings, but I have not been successful in achieving the desired outcome.

Answer №1

If you have a container with a set height (let's assume it is 100% of the viewport), you can utilize column direction along with wrap and order properties to structure your layout:

html, body {
  margin:0;
  height:100%;
  color:white;
}
.container {
  height:100%;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
}

.div1,
.div3 {
  height: 50%;
  width: 30%;
}

.div1 {
  background: red;
}

.div2 {
  max-height:100%; /* enable overflow scroll */
  overflow:auto;
  flex-grow: 1;  /* ensure div fills height */
  width: 70%;
  order: 3;      /* place this div at the end */
  background: grey;
}

.div3 {
  order: 2;       /* position this div below div1 */
  background: blue;
}
<div class="container">
  <div class="div1">DIV 1</div>
  <div class="div2">DIV 2<br>DIV 2<br>...<br></div>
  <div class="div3">DIV 3</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

Creating a personalized LinkedIn share button through POST requests: here's how

I am interested in using my own image for the LinkedIn share button. After referencing a previous post on this topic: How to make a custom LinkedIn share button It was mentioned that: In order to create a custom Share button for LinkedIn, POST calls need ...

Removing a CSS class using JQuery

Within my website layout, I have a div that is dynamically included using PHP. This div is inserted in two different locations, each inside its own parent div. <div id="parent_div"> <div id="contact_details_div" class="contact_details_div sam ...

What could be causing my php code to fail to trigger an alert when using Ajax technology?

As a novice in web programming, I have successfully created a website but now I am facing challenges while trying to integrate a database. Despite having everything properly set up, my ajax function doesn't seem to be working and I can't figure o ...

One inner DIV positioned in the center flanked by two others on either side

In this scenario, we encounter two different situations: 1. Divs A & B are placed side by side within a container 2. Div A is centered in the same container For reference, you can view an example here .container_one, .container_two {width:200px; heig ...

Adding as HTML in Angular

I am looking to insert HTML content dynamically in Angular <div class="storycontent" ng-class="{show: show}"> <a href="#/profile?{{review.id}}">{{review.name}}</a>: {{review.story}} </div> Within the {{review.story}}, there ...

Ways to prevent the jQuery simple slider from transitioning slides while it is in an invisible state

There is a jQuery slider on my website that behaves strangely when I navigate away from the Chrome browser and return later. It seems to speed through all pending slides quickly when it was not visible. Now, I want the slider to pause when it becomes invi ...

Does the CSS overflow property affect the borders of an element?

Consider a situation where a "div" element has a border applied to it. When the overflow rule is set to 'hidden', any content that falls directly on the border will vanish. Is there a solution for this issue? It is crucial in my case to ensure t ...

What are the steps to designing a footer or toolbar in an iPhone web application?

I am currently developing a web application and I am facing an issue where I need a div to remain fixed at the bottom of the screen, always visible. There is an example of what I want to achieve on this link: footer. However, it seems that this solution ...

Troubleshooting problem with displaying background images on iPad and iPhone devices

A recent project involved building a website for a new client, but there seems to be an issue with the background rendering on his iPad and iPhone. The image is displaying incorrectly, showing only a small portion from the top right corner rather than the ...

Is there a way to adjust the background hues of a React component when a click event is triggered?

Currently, I have 3 React components that each consist of a div element. When I click on one component, I want to change its background color to black. If I then select another component, the previously selected component should return to its normal back ...

Administering changes to JSON data files through PHP

I am currently working on a code that creates a JSON file on the server to save form data. However, I have encountered an issue where every time the submit button is clicked, the form data appends to the existing data in the JSON file instead of updating i ...

The width of a <div> element in CSS is not dependent on the width

Is there a way to make tables within a div have relative widths to each other? For example, if one table has a large image or long word causing it to extend to 100%, how can we ensure that the other tables in the same div also adjust to 100% width? CSS . ...

Set up a trigger to activate when a WordPress User Role is selected, or alternatively, execute JavaScript

Looking for a solution to detect when a new user is created with a custom User Role set to lessee. Options include adding a trigger through WordPress filters or actions, or detecting the change in JavaScript using an event listener. I have identified the ...

Divide the result by using ajax

In my webpage, I have an input field labeled "Model" with a datalist attribute and a select menu for "Brand". When a user selects an option from the Model input, it should dynamically update the options available in the Brand select menu. Both the Model an ...

Deactivate the rest of the buttons by utilizing the e.target.id

I have ten expansion panels. When I click a button in one expansion panel, I want to disable other buttons in the remaining expansion panels. The issue is that when I try to target an id, it does not return e.target.id but instead returns as the value pass ...

Is there a problem with using Nth-Child / Nth-Of-Type in CSS when classes are dynamically added or removed using jQuery?

Currently, I have a set of LI tags. As users filter the LIs, some may be removed from view. My goal is to assign alternating background colors to the visible LIs. When an LI is visible, it is given the class "LI-Shown." If an LI gets filtered out, I remov ...

Can Angular's built-in internationalization features be used to translate bindings?

Challenge The task at hand involves integrating translations into an Angular 6 application to support static text in multiple languages. The objective is to have the ability to choose a language during the build process without requiring dynamic translati ...

Guide to inserting an HTML file into an article's content

After downloading an extension from freefrontedit and uploading it to my server in the directory /accordeon, I successfully accessed it by pointing my browser to . However, I am now faced with the challenge of loading the index.html into my Joomla article ...

Show the chosen value from the dropdown menu on all JSP pages

I have a header.jsp file containing a dropdown box labeled "Role". This header.jsp is designed to be included in all other JSP files using a directive. Once a user logs in, they are directed to a homepage where they must select a value from the dropdown ...

How can I stack a div on top of two columns in Bootstrap?

My goal is to design something like this: I want to have a single-color strip that extends over three columns: https://i.sstatic.net/1XfAu.png What I am aiming for is a layout where the grey line overlaps columns 2 and 3. /* added by editor for demo p ...