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

Guide to counting the number of image tags within a parent div and selectively removing them starting from a specific position

My dynamic image tag <img> is generated inside a div from the database using fetching. Here is how my HTML looks: <div class='forimgbox'> <p class='palheading'>Pals</p> <img src='userpic/2232323.p ...

Using jQuery to apply a CSS border to an image within a textarea

Working with TinyIMCE, I am looking to incorporate a border around all images within the textarea whenever a user submits the form. $("form").submit(function() { var content = $("#edit-message1-value").val(); // Afterwards, I want to manipulate the cont ...

What is the best way to position my header at the top of my navigation bar?

I am new to the world of HTML and CSS! My goal is as follows: https://i.stack.imgur.com/hmLNS.png This is my progress so far: https://i.stack.imgur.com/rav8P.png I am also looking to have the header fill the entire browser window and remain fixed, wit ...

The background-size property in CSS does not seem to properly display on iPhones

Hello there! I'm facing an issue with my CSS code when it comes to displaying the image on Safari browser. It works perfectly fine on other browsers, but for some reason on Safari, the image doesn't show up correctly. I tried using a media query ...

Ways to identify when a React child element is overflowing

tl;dr How can I create a component that hides overflow and toggles views with a button? For example, the user can initially see tabs 1, 2, and 3 while tabs 4, 5, and 6 are hidden. Clicking a button will hide tabs 1, 2, and 3, and show tabs 4, 5, and 6 with ...

Having trouble with this code// Does anyone know how to make the div slide in line with other divs when hovering over it?

After spending countless hours analyzing and trying various solutions, I have come to the realization that I need to seek help with my code. The task at hand is proving to be incredibly frustrating, and I have exhausted all other options before resorting t ...

Can a Dashcode Webkit Web app be transformed into traditional HTML and CSS?

I have developed a blog using Dashcode, incorporating HTML, CSS, and Javascript to pull data from an xml file. It's pretty simple... My perspective on this is: 1. JavaScript should be compatible with all browsers (with some variations). 2. I may need ...

I'm looking for an easy way to generate a special effect when my mouse interacts with a div using HTML, CSS, or JavaScript

I'm attempting to replicate this interesting effect where a div is surrounded by a container when the mouse hovers over it. It looks pretty cool, like in this image here: https://i.stack.imgur.com/p0epq.png Does anyone have any suggestions on how I ...

placing a div inside another div

As I work on my website, I have created several panels with a repeating texture. To enhance the visual appeal of the site, I decided to add colored divs and adjust opacity for a tint effect instead of using separate images. The issue I'm facing is th ...

Tips for keeping your avatar contained within a Bootstrap grid

I am attempting to adjust the positioning of my "image holder" (avatar) with a top and bottom margin of 3px, but when I implement this change, the image holder shifts outside of the grid. Below is the code snippet in question: <div class="container con ...

Insert text within a span tag next to a picture

Greetings everyone, Here is the current structure I am working on: <div class="payment-option clearfix"> <span class="custom-radio pull-xs-left">...</span> <label style="display:inline;width:100%"> <span style="fl ...

Is there a way to set an image as the background of my HTML screen?

{% extends "layout.html" %} {% block app_content %} <div> {% from "_formhelpers.html" import render_field %} <form method="post" enctype="multipart/form-data"> <div class = "container"> < ...

Using jQuery's .html function to insert images

I have a unique counter that counts in currencies, such as Yen and Euro. Each number, as well as the currency sign and separator, are all displayed on the webpage using custom-designed icons. I utilize the display: flex; property in my container div and ap ...

relative font sizing based on parent element

My container is flexible in size, adjusting based on how the user moves elements around the screen. Inside this container, there is both an image and text. The image takes up 80% of the height of the container while the text should take up the remaining ...

Steps to dynamically reveal a table row within another row when clicked:

I have a table and I want to show the contents of another row inside the current row when the plus icon is clicked. I'm not sure which HTML element to use for this. Please help me figure it out. Thank you in advance. <div class="pro ...

Leveraging the CSS-Element-Queries Library for emulating the functionality of CSS media queries

Recently, I used a nifty CSS-Element-Queries tool to perform basic element manipulations that trigger whenever the window is resized. In simple terms, my goal was to dynamically adjust an element's attribute based on the current width of the window â ...

Capture the beauty of Safari with images displayed in the input file element

I'm facing an issue with the file upload element on my website. Most browsers, like Chrome and FF, display the image name when a file is chosen. However, Safari seems to show a tiny thumbnail instead. My goal is to create a custom upload button that ...

Uploading images using the Drag and Drop feature in HTML

Hello, I'm having an issue with the drag and drop functionality. I want to expand the size of the input to cover the entire parent div, but for some reason, the input is appearing below the drag and drop div. Can anyone assist me with this? https://i. ...

Tips for positioning elements on an HTML page by utilizing CSS styling

I have a primary container with the following CSS: .major { width:30%; height:100%; position:absolute; } Now, I want to insert web information at the bottom part which includes: About | Terms | Policies | Contact Us This content will be within the .web ...

Applying CSS styling to PHP documents

I am a beginner, so please go easy on me. I am looking to style variables like $product_name, $author, and $details. I was thinking of styling the echos, but in this scenario, the only echo is: <?php // Establish connection with the MySQL database ...