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

The information overflow occurs outside the tooltip specifically in Chrome, while functioning perfectly in IE11

As a newcomer to the CSS world, I am experimenting with adding a block-level element (div) inside an anchor tag's :hover pseudo-class popup. The div element contains a table that needs to have dynamic sizing for both the table itself and its cells (wi ...

The FormData function is unable to retrieve the input fields of a form

My Unique Code <!DOCTYPE html> <html> <body> <div id="custom-form-sample"> <form enctype="multipart/form-data"> <input type="text" name="custom-text" /> <input type="radio" name="custom-radio" ...

Tips for eliminating the white flash while transitioning background images with animation?

I am facing an issue with my code where the background image of the site transitions between 5 different images, but every time an animation completes and the next one starts, there's a white flash. I'm not sure how to resolve it or what I might ...

Issue with transforming rotation in Quirks mode

Currently, I'm developing a website in quirks mode which does not allow the use of . In my project, I am facing an issue where CSS3 translate image is not working in IE10 and 11, although it works fine in IE9 due to the limitations mentioned above. Is ...

Tips on choosing a button and applying custom styles with emotion styles in MUI

Looking to apply a margin-right to my button. Currently utilizing mui 5 with Button variant='contained'. I created a custom CSS style using the styled component in mui and targeted the Box. const Wrapper = styled(Box)({ display: 'flex&ap ...

Split-button dropdowns within tabs implemented with Bootstrap

I'm looking to create a navigation menu that combines both split buttons and dropdown tabs. I've found examples of each individually, but can't figure out how to merge the two: "Tabs with dropdowns" Is it possible to have a tab with a drop ...

The upper margin is ineffective

Apologies, I am new to CSS. Here is the HTML code I have: <div class="box-A" >Box A content goes here</div> <div class="box-B" >Box B content goes here</div> I attempted to apply the following CSS to it: .box-A{ background-c ...

Customizing Your WordPress Menu with HTML5 Features

Can someone assist me in creating a customized WordPress menu? I am facing issues with unnecessary classes added by WordPress and the lack of certain attributes on dropdowns. Below is the HTML code: <nav class="navbar navbar-default navbar-static- ...

clicking the anchor will toggle the div

Here is the HTML code that I am working with: <? while($arr as $data) { ?> <td> <div> <a href="javascript:void(0)" class="status <?= $data['SUGGESTION_CD'] ?>"> <img src="images/activate.gif" /> &l ...

Modify the CSS formatting of a specific element with its designated ID

On my HTML page, I have an element that looks like this: <label id="lb">Label1</label> In my corresponding CSS file, I have defined the following style for it: #lb{ width:100px; } I want to override this style. What would be the most effect ...

Accelerate loading times of HTML files on Android apps

After clicking on an image, I want the corresponding HTML page to load instantly. However, there is a delay of a few seconds before the page actually appears. How can I reduce or eliminate this waiting time and ensure that the page loads as quickly as po ...

Is there a way to send decimal values from a view to a controller using Vue.js?

I am encountering an issue when trying to pass decimal values from the view to the controller using Vue.js. Only decimal values seem to be arriving as NULL, while integer or string values work fine. Below is the code snippet: salvarProdutos: function ( ...

Strategies for delaying the loading of CSS when importing

import 'react-dates/lib/css/_datepicker.css' The CSS mentioned can be deferred since it is not critical. Is it possible to defer the loading of CSS when utilizing import? I found information on deferring CSS loading using <link> from Goo ...

Webpage not resizing when WebView orientation changes

While developing my android app, I encountered an issue with the WebView. When I am at the very top of the WebView and switch my phone to landscape mode, the webpage does not resize properly to fit the screen. It only takes up half of the screen. However, ...

Is there a way to remove the unwanted code ​ from the client side of my website?

Upon inspecting the source code of a webpage, I noticed an odd character ​ appearing before the <script> tag on the client side. Strangely, this mysterious character is not present anywhere in the server-side PHP file. I attempted removing a ...

Switch effortlessly between one animation and another with Angular.js

I have two animations named "prev" and "next". Upon clicking the prev button, the "prev" animation should play. Similarly, when you click on the "next" button, the "next" animation should be triggered. I am using ng-class to achieve this behavior. How can ...

Formatting Outlook HTML Signatures

I'm currently designing an HTML email signature for Outlook, but I'm having trouble with formatting and ensuring it looks consistent across different devices. When I preview the HTML in my browser, it appears exactly as I intended. However, I am ...

Achieve full parent width with floated divs

My goal is to arrange 10 boxes horizontally on a webpage. Each box must have a minimum width of 150px and a maximum width of 299px. The challenge is to fit as many boxes as possible across the page without any gaps, ensuring that each box has an equal widt ...

Creating personalized fonts in SAPUI5 Theme Designer

Recently, I have been working on creating a personalized theme for the HANA cloud platform Portal. In order to give my site a unique touch, I decided to incorporate custom fonts. To achieve this, I uploaded the fonts as an HTML application in HCP. Now, my ...

Getting data from an HTML file with AJAX

I have a JavaScript application where I am trying to retrieve an HTML file in order to template it. Currently, I am using the following method: var _$e = null; $.ajax({ type: "GET", url: "/static ...