Center the middle item in a flexbox column arrangement

I'm working on a layout where I have three items stacked vertically within a column. My goal is to center the middle item (green) in the column and have the other items positioned around it.

Here's my current setup:

.flex-row {
   display: flex;
   flex-direction: row;
}

.flex-col {
   display: flex;
   flex-direction: column;
}

.flex-start {
  display: flex;
  justify-content: flex-start;
}

.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}

.flex-end {
  display: flex;
  justify-content: flex-end;
}

.col {
  width: 50px;
  min-height: 250px;
  margin: 5px;
}

.item {
  width: 40px;
  height: 40px;
  margin: 5px;
}

.long {
  height: 60px;
}

.red {
  background: red;
}

.blue {
  background: blue;
}

.green {
  background: green;
}
.yellow {
  background: yellow;
}
<div class="flex-row">
  <div class="red col flex-center">
    <div class="flex-col">
      <div class="blue item"></div>
      <div class="green item"></div>
      <div class="yellow item long"></div>
    </div>
  </div>
  <div class="red col flex-center">
    <div class="flex-col">
      <div class="blue item long"></div>
      <div class="green item"></div>
      <div class="yellow item"></div>
    </div>
  </div>
  <div class="red col flex-center">
    <div class="flex-col">
      <div class="blue item long"></div>
      <div class="green item"></div>
      <div class="yellow item long"></div>
    </div>
  </div>
</div>

My desired outcome:

https://i.stack.imgur.com/l88wR.png

Keep in mind that the center items (green) have fixed height, while the height of the others (blue and yellow) varies based on content length. I could set fixed heights for the columns too, but I'd prefer them to adapt to the height of the items inside.

Answer №1

To achieve the desired layout, you can utilize positioning as the green box has a fixed height.

.flex-row {
  display: flex;
  flex-direction: row;
}

.flex-col {
  height: 100%;
  position: relative;
}

.col {
  width: 50px;
  min-height: 250px;
  margin: 5px;
}

.item {
  width: 40px;
  height: 40px;
  margin: 5px;
}

.long {
  height: 60px;
}

.red {
  background: red;
}

.blue {
  background: blue;
  position: absolute;
  bottom: calc(50% + 25px); /* 5px for margin + green box's height / 2 */
}

.green {
  background: green;
  position: relative;
  top: 50%;
  transform: translateY(-50%)
}

.yellow {
  background: yellow;
  position: absolute;
  top: calc(50% + 25px); /* 5px for margin + green box's height / 2 */
}
<div class="flex-row">
  <div class="red col flex-center">
    <div class="flex-col">
      <div class="blue item"></div>
      <div class="green item"></div>
      <div class="yellow item long"></div>
    </div>
  </div>
  <div class="red col flex-center">
    <div class="flex-col">
      <div class="blue item long"></div>
      <div class="green item"></div>
      <div class="yellow item"></div>
    </div>
  </div>
  <div class="red col flex-center">
    <div class="flex-col">
      <div class="blue item long"></div>
      <div class="green item"></div>
      <div class="yellow item long"></div>
    </div>
  </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

Load additional content seamlessly using Ajax in Django without needing to reload the entire navigation bar

I recently started working with Django as I develop my own website. I have created a base.html file which includes the main body, CSS, js, fonts, and navbar that will be present on all pages of my site. The navbar is located in another HTML file called nav ...

Failure of default option to appear in dropdown menu in Angular

I currently have a dropdown list in my HTML setup like this: <select id="universitySel" ng-model="universityValue" ng-options="university._id for university in universities"> <option value="-1">Choose university</option> ...

The method for storing a user's choice in my array and subsequently selecting an element at random

Seeking assistance in developing a program that can play an audio list at various durations. The plan is to have the duration for elements 4 through 8 based on user selection, with the program playing each element within that range during the initial round ...

Capture all parameters in the URL, regardless of their names

Hello, I am currently utilizing the s:url tag to create a URL in my JSP file for pagination purposes. The issue I am facing is that when clicking on the previous link, it generates a URL in the address bar like someAction?previuos=prev. Similarly, when cli ...

Linked Dropdownlists Connected to a Single Dropdownlist

I am struggling to find the best way to phrase my question, but I need some help. I want to connect three dropdown lists together in a specific way. Dropdown list 2 and 3 should be disabled so the user cannot interact with them, while dropdown list 1 remai ...

Offspring element overrides styling of its parent

#popBox{ width:100%; height:100%; position:fixed; left:0; top:0; border-collapse:collapse; background:black; opacity:0.8; display:none; } <table id="popBox"> <tr> <td></td> <td></td> ...

Click the button to trigger the pop-up

After creating a photo gallery using a plugin, my goal is to display this gallery when the user clicks on my homepage button. I am unsure how to make this happen and would appreciate any assistance. Below is my button code: <div class="ow-button-base ...

Controlling the Quantity of Selected Checkboxes with JavaScript

I am facing an issue with implementing multiple checkboxes with limits in JavaScript, as shown below. $(".checkbox-limit").on('change', function(evt) { var limit = parseInt($(this).parent().data("limit")); if($(this).siblings(':checked&ap ...

I am curious if there is a method to vertically center text without being affected by the font-family or font-size

Searching for a font-independent method to center text within a div. My div has a button-style fixed height, and I need the text (one line only) to be centered vertically within it. The issue arises when changing the font-family, as some fonts do not ali ...

Adjusting the grid for various mobile screen sizes

I am currently working on designing a user interface (UI) for mobile screens that includes a header, grid, and buttons. I want the UI to look consistent across all mobile devices. Here are screenshots from various mobile screens: Samsung S5 Pixel 2 XL I ...

Implementing a jQuery change event to filter a specific table is resulting in filtering every table displayed on the page

I have implemented a change event to filter a table on my webpage, but I am noticing that it is affecting every table on the page instead of just the intended one. Below is the code snippet: <script> $('#inputFilter').change(function() { ...

Unexpected output from Material UI Textfield

When attempting to print a page of my React app using window.print(), everything prints correctly except for the Textfield component from Material UI. It works fine when there are only a few lines of text, but when there is a lot of text, it appears like t ...

The material UI styled component is not appearing as expected

I'm having trouble getting the MUI styled() utility to apply styles to <MyComponent>Styled div</MyComponent> in my index.jsx file. Any ideas why? import Button from '@mui/material/Button' import Grid from '@mui/mater ...

Is there a way to modify the color scheme of my webpage while utilizing Bootstrap?

As I delve into programming with Bootstrap, I encountered an issue while attempting to change the background color of my body. Even though I used the following CSS code: body { background-color: #eba; width: 100%; height: 100%; } The color change ...

Generate an HTML document from a website page

Having difficulty grasping this concept. I am completely lost on where to begin. It is imperative that I determine how my website can generate a file (whether it be HTML or Text format) and enable users to download it, similar to the functionality in Goo ...

Looking for a skilled JavaScript expert to help create a script that will automatically redirect the page if the anchor is changed. Any t

Seeking a Javascript expert, In my custom templates, I have used a link as shown below: <a href='http://www.allbloggertricks.com'>Blogging Tips</a> I would like the page to redirect to example.com if the anchor text is changed from ...

Retrieving an HTML element that has been added through DOM manipulation

After successfully creating a Jquery function that inserts a 'save button' into the page when a specific button is clicked, I encountered an issue with another function meant to be activated when the save button is clicked. The first function see ...

What is the method for choosing an Object that includes an Array within its constructor?

Is there a way to retrieve a specific argument in an Object constructor that is an Array and select an index within the array for a calculation (totaling all items for that customer). I have been attempting to access the price value in the Items Object an ...

Which CSS properties can I implement to ensure that the text remains legible regardless of the background behind it?

Looking ahead, I am displaying an issue where the colors are almost perfect, but when different percentages are applied, some or all of the text becomes obscured. My goal is to assign the font color based on the background difference. I vaguely remember s ...

Divs should be of consistent and adjustable height

I am in need of a layout with three columns: left column (with a red background) center column (with a yellow background) right column (with a black background) This is the HTML structure I have in mind: <div id="container"> <div id="left_ ...