What is the best way to replicate floats using flexbox?

Currently, I am exploring the possibility of using flexbox in a similar manner as floats. My main aim is to have one child element create a column on the right side, while the rest of the children form another column on the left side. The challenge is that I can only manipulate the layout using CSS and cannot make any changes to the HTML structure.

Although I have successfully implemented this layout, there is an issue where the two columns are not aligning at the top as desired. Instead, the left column appears slightly below the right column.

You can view the code snippet here

.parent {
  display: flex;
  flex-wrap: wrap;
  flex-direction: column;
}

.child {
  max-width: 48%;
}

.right {
  margin-left: auto;
}
<div class="parent">
  <div class="child right">Right</div>
  <div class="child">Left</div>
  <div class="child">Left</div>
  <div class="child">Left</div>
</div>

Answer №1

To adjust the order of elements, you can utilize the order property. Additionally, I have eliminated flex-direction: column and included max-width: 100% for elements following the second one.

.parent {
  display: flex;
  flex-wrap: wrap;
}

.child {
  width: 100%;
  max-width: 48%;
}

.child:nth-child(2) {
  order: 1;
}

.child:nth-child(3) {
  order: 3;
}

.child:nth-child(4) {
  order: 4;
}

.right {
  order: 2;
}

.child:nth-child(n + 3) {
  max-width: 100%;
}
<div class="parent">
  <div class="child right">Right</div>
  <div class="child">Left</div>
  <div class="child">Left</div>
  <div class="child">Left</div>
</div>

If the element with class .right has greater height, you may want to consider using grid-area.

.parent {
  display: grid;
  grid-template-areas:  "left1 right" 
                        "left2 right" 
                        "left3 right";
}

.child {
  width: 100%;
  background: blue;
  height: 30px;
}

.child:nth-child(2) {
  grid-area: left1;
}

.child:nth-child(3) {
  grid-area: left2;
}

.child:nth-child(4) {
  grid-area: left3;
}

.right {
  background: red;
  height: 80px;
  grid-area: right;
}
<div class="parent">
  <div class="child right">Right</div>
  <div class="child">Left</div>
  <div class="child">Left</div>
  <div class="child">Left</div>
</div>

Answer №2

Achieve the desired layout using flexbox in its original form!

.parent {
  display: flex;
  justify-content:space-between;
}
<div class="parent">
 <div>
  <div class="child">Left</div>
  <div class="child">Left</div>
  <div class="child">Left</div></div>
  <div class="child right">Right</div>
</div>

Answer №3

With CSS-Grid, you can achieve the desired layout:

.container {
  display: grid;
  grid-template-columns: auto auto;
  grid-auto-flow: column;
}

.item {
  max-width: 48%;
  grid-column: 1;
  border: 1px solid green;
}

.right {
  grid-column: 2;
}
<div class="container">
  <div class="item right">Right</div>
  <div class="item">Left</div>
  <div class="item">Left</div>
  <div class="item">Left</div>
  <div class="item right">Right</div>
</div>

Answer №4

If you're looking to steer clear of using grid (possibly due to IE 11 compatibility?), another option is to utilize the old method involving table-display:

.parent {
  display: table;
  width: 100%;
  direction: rtl;/* creating a reversed column flow effect here */
  text-align: left;
  border: solid;
}

.child {
  direction: ltr;/* resetting flow direction ;)*/
  border: solid;
}

.right {/* will expand if the first column is longer than itself */
  display: table-cell;
  width: 50%;/* specify column width here - any leftover space will be for other columns */
}

/* margins ? */
.bis {border-spacing:2px;}
.bis .child {margin-right:2px;}
.bis .right + .child ~ .child {margin-top:2px;}
.bis .right{text-align:center;vertical-align:middle;
<div class="parent">
  <div class="child right">Right</div>
  <div class="child">Left</div>
  <div class="child">Left</div>
  <div class="child">Left</div>
</div>
<hr>
<div class="parent bis">
  <div class="child right">possible gaps /  VH-align</div>
  <div class="child">Left</div>
  <div class="child">two<br>lines</div>
  <div class="child">Left</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

What could be causing html-webpack-inline-source-plugin to prevent the page from refreshing after editing CSS files?

Currently, I am utilizing a plugin that embeds all CSS within the final HTML file. This method prevents the application from refreshing in development mode. Whenever I make edits to the CSS, the build process completes normally, but the styles do not updat ...

Using :nth-child on elements with the same class name does not produce the desired result

I attempted to utilize the :nth-child selector to target the first, third, and fifth elements with the class name "personal", but it did not work as expected. Is there an alternative method to specifically select these elements sharing the same class? I ha ...

Creating dynamic transformations and animations for characters and words within a paragraph in 3D

Looking to add animation effects to specific parts of a paragraph, but transforming the entire box instead. Remembering seeing a solution on StackOverflow before, now regretting not saving it. Spent over an hour searching for a similar answer without succ ...

Issues with Bootstrap-slider.js functionality

I'm having trouble getting the bootstrap-slider.js library from to function correctly. All I see are textboxes instead of the slider components. You can view an example at I have verified that the CSS and JS files are pointing to the correct direc ...

Transient Flash of a jQuery Dropdown Menu

I'm currently developing a jQuery/CSS dropdown menu for a website, and everything is functioning well except for one issue. When navigating between sub-menu items, the text color of the selected main menu item briefly changes because of the CSS border ...

What is the process for adjusting the color of the browser tab?

My chat application is running smoothly, but a client has requested the ability to highlight or make the browser tab blink when they receive a visitor call. This will help them respond promptly to incoming inquiries. Does anyone know how to implement thi ...

Using jQuery to modify a CSS property when a specific option is chosen

I'm currently working on creating an option dropdown menu and I've hit a roadblock trying to display a textbox only when the 'other' option is selected. Here's what I have so far: <span id="con-country"><label for="count ...

"Implementing a dynamic loading effect in Highcharts triggered by a button

Take a look at this sample highchart fiddle: http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/series-setdata/ $('#button').click(function () { var chart = $('#containe ...

How can I make my modal box appear after someone clicks on selecting a college?

Can someone help me figure out how to display my modal box after clicking into the selecting list? I've already coded it, but I'm struggling with getting it to show up after the click event. Any assistance is appreciated! In the image provided, ...

Exploring Scroll Functionality for Inner Components Within Mat-tab

I am currently working on a small attendance project that involves using table components within mat-tab elements. However, I am facing an issue where the overflow from the table causes the entire component to scroll, when in fact I only want the table its ...

Is it possible to dynamically alter a CSS property using styled components when an onClick event occurs?

Hey there, I'm pretty new to React and currently exploring styled components for the CSS styling of my components. One of the components I've created is a button called SummonButton: const SummonButton = styled.button` height: 50px; borde ...

Tips for utilizing FixedHeaderTable - Basic instructions required

After doing extensive research, I stumbled upon this plugin called Fixed Header Tables. Despite following the instructions provided on the main website, I couldn't get it to work. I was aiming to create a table with a fixed top row and left column fu ...

Wrapping is disabled for all elements within the container div that contains three additional div elements

I am trying to prevent one of the three divs inside a fixed top navbar container from sliding underneath another, causing the navbar to become taller. The code below is using Bootstrap and the issue is with the navbar-header div sliding underneath the he ...

Elements styled as inline blocks with static dimensions, irregular in size

Is there a way to ensure that all three boxes are displayed at the same level? Currently, box 2 appears below box 1 and 3 due to having less content. I believe there must be some styling element missing to make each div display at an equal level regardless ...

Implementing position sticky on a div depending on its content text - step by step guide

If the text inside the .newsDate matches the previous or next .newsDate, I want to make its position sticky when scrolling, until it reaches the next .newsMiddleCont div. What I'm trying to achieve: The same date on multiple news items should s ...

Ways to unveil a concealed div element using JavaScript

If javascript is disabled, I want to hide a div. If javascript is enabled, however, I don't want to rely on using the <noscript> tag due to issues in Chrome and Opera. Instead, my approach is as follows: <div id="box" style="display:none"> ...

Is it possible to modify the CSS styling in React using the following demonstration?

I am attempting to create an interactive feature where a ball moves to the location where the mouse is clicked. Although the X and Y coordinates are being logged successfully, the ball itself is not moving. Can anyone help me identify what I might be overl ...

Difficulty integrating custom CSS with Bootstrap framework

I attempted to incorporate a custom CSS file with my Bootstrap code, but unfortunately it does not seem to be functioning properly. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta htt ...

Having trouble with your CSS dropdown menu?

I've been struggling to implement a dropdown menu on my website following an online tutorial. Despite following the steps, I can't seem to get it working. It's frustrating because I want to move on to backend development. Hoping someone can ...

Adaptive Style Sheets Strategy

The images below depict the concept I am trying to achieve. The first image shows a longer width while the second one represents a shorter width. The red blocks are positioned on the right and left sides, with the yellow block adjusting its width based on ...