Utilize CSS to automatically reposition the left and right divs under the center div, creating a seamless layout

When styling divs inline, they will automatically wrap to the next line if they don't fit in the viewing window. This is often used for creating a quick and simple mobile version of a website.

I am interested in achieving this effect with both sides simultaneously. To better understand what I mean, please refer to the jsfiddle link provided below. Is it possible to accomplish this using CSS alone, or is CSS not powerful enough for this task?

Please see the code snippet below:

<!DOCTYPE html>
<html>
<head>
<style>
.a {
  display: inline-block; 
  width: 100px;
  height: 100px;
  padding: 5px;
  float:none;
  border: 1px solid blue;  
  background-color: yellow; 
}

.b {
  display: inline-block;
  width: 200px;
  height: 100px;
  padding: 5px;
  float:none;
  border: 1px solid blue;    
  background-color: yellow; 
}

.c {
  display: inline-block;
  width: 100px;
  height: 100px;
  float:right;
  padding: 5px;
  border: 1px solid blue;    
  background-color: yellow; 
}
</style>
</head>
<body>

<div> <div class="a">should end up below on left</div> <div class="b">center div should end up on top</div> <div class="c">should end up below on right</div></div>


</body>
</html>

Answer №1

To implement a flexible layout, you can utilize the 'flex' property along with the 'order' attribute. Below is a customizable example that you can adjust to suit your requirements:

html, body{
  height: 100%;
}

body{
  margin: 0;
}
.wrapper {
  display: flex;
  width: 100%;
  height: 100px;
}
.box {
   width: 20%;
   height: 100px;
   padding: 5px;
   border: 1px solid blue;  
   background-color: yellow; 
}
.box.a {
  order: 1;
}
.box.b {
  width:30%;
  order: 2;
}
.box.c {
  margin-left: auto;
  order: 3;
}

@media all and (max-width: 600px) {
  .wrapper{
    flex-wrap: wrap;
  }
  .box.a  {
    width: 47%;
    margin: 0;
    order: 2;
  }
  .box.b {
    width: 100%;
    order: 1;
  }
  .box.c {
    width: 47%;
    margin: 0;
  }
}
<!DOCTYPE html>
<html>
<body>

<div class="wrapper"> 
   <div class="a box">should end up below on left</div> 
   <div class="b box">center div should end up on top</div> 
   <div class="c box">should end up below on right</div>
</div>


</body>
</html>

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

Troubleshooting: Unable to Sort Column in ngx-DataTable Angular 4

As a newcomer to Angular, I have been encountering some challenges while using ngx-DataTable. I am currently utilizing a simple ngx-DataTable for basic operations. The issue I am facing is that the sorting functionality is not working on a specific column, ...

The error message "Uncaught ReferenceError: req is not defined with [email protected]" indicates that the variable

Discovered a helpful post that seems to address the problems. https://github.com/angular/angular-cli/issues/8359 I'm unsure about the steps to take next to resolve this issue. Can anyone clarify? Could you please advise on which version of Angular ...

Is the number 9933272057275866 truly magical?

I encountered a strange issue that I cannot quite understand. It has left me quite baffled. When I attempt to increase the number 9933272057275866 by 1, it somehow adds 2 instead!!! Here is the code snippet: let test = 9933272057275866; let test2 = test+1 ...

The Bootstrap navbar collapse feature is malfunctioning

My navigation bar is not functioning correctly with Bootstrap 5. I have all the necessary CSS and JavaScript files included in my HTML, but when I try to toggle the navbar, the dropdown menu does not appear. I have checked my code multiple times and it a ...

Jquery is not displaying the background color of the progress bar

Greetings to all! I am currently working on implementing a progress bar for my website, but I am encountering an issue where the background-color is not displaying correctly on scroll. I would greatly appreciate any assistance with this matter. Below you c ...

The shadow feature in Three.js doesn't seem to be functioning properly

I'm having trouble getting the Three.js shadow effect to work in version r82. I believe I have everything set up correctly, but for some reason it's not working. Can anyone point out what I might be missing? Here is an example link for referen ...

Ensuring the safe transmission of NodeJS applications

Query: Is it possible to distribute NodeJS apps as binaries by compiling the .js app through V8 into its native binary form for client distribution, assuming total access to the NodeJS server? Or are we limited to just minifying the code? Motivation: Our ...

Ways to reveal a concealed element by simply clicking on a different element

Check out this example on JSFiddle: http://fiddle.jshell.net/ggw6hqsj/1/ Currently, I am facing an issue where: Clicking the first button hides the second button. However, I am struggling with making the hidden button reappear when the first but ...

The jQuery function $.fn.myfunction appears to be malfunctioning

Here's the code I'm working with: [[A]] // jquery.fn.code (function( $ ){ $.fn.flash = function(duration) { this.animate({opacity:0},duration); this.animate({opacity:0},duration); }; })( jQuery ); 1. $(document).ready ...

Properly storing information in the mongoDB

Having trouble saving data in my Angular application correctly. Here is a snippet of my API code: .post('/type/add', function(req, res){ var type = new NewType({ type: req.body.type, subtype: req.body.subtype, }); type.save ...

Content within a div that is floated left

The scenario at hand is: HTML Structure: <div id="main"> <div id="a"></div> <div id="b">Some Text</div> </div> CSS Styles: #a{ float:left; width:800px; height:150px; background-color:#CCC; } ...

What causes the scope to shift when incorporating a Lazy function within the module pattern in JavaScript?

Implementation 1: Working code in normal var foo1 = function() { var t1 = new Date(); console.log("initialize - one time only " + this); foo1 = function() { console.log("Executes every time after initializing (initialize should not e ...

When the ::after pseudo element is utilized for creating a bottom border, it will display following each individual list item contained within the specified div

I have been using a pseudo-element to create a division line between sections, and it was working perfectly until I added a list of items. Now, the division line is appearing after every <strong> and <li> tag. I have tried various approaches, s ...

Sorting items in jQuery by height

Currently, I have a jQuery sortable set up based on a list structure: <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> </ul> To ensure consistency in the appearance o ...

"Effortlessly emptying the text field with material-ui in React

I need assistance with clearing the content of two text fields and a button using Material-UI in React-JS. I am new to React and unsure how to achieve this. Below is the code I currently have: import React from 'react'; import RaisedButton from ...

observing the pathway and modifying a variable within a directive's scope

A specialized directive has been crafted below, designed to generate a series of buttons relying on the data supplied to it. angular.module('btnbar.directive', ['messaging']). directive("btnBar", function(){ return { restrict: &a ...

Guide on utilizing angularjs $q.all() method with a dynamically generated set of promises

I am currently facing an issue with using $q.all() to wait for multiple promises to be resolved. I have created an array of promises and passed it to the all() method, but it doesn't seem to be working as expected. The promises in the array are gener ...

jQuery enables the creation of fresh form fields

Currently, I am working on a form that initially consists of 2 text input fields. The typical use case involves the user entering a number in one field and their name in the other. Following this, the page updates itself without reloading. However, there a ...

Iterate through the array and add each number to a separate array

I am currently facing an issue with the code snippet provided below. var array = [1, 3, 2] var newArray = [] getNewArray() { for (let i = 0; i < array.length; i++) { for (let x = 0; x < array[i]; x++) { this.newArray.pus ...

Difference in values of $(document).height() as compared to $(document).scrollTop() added to $(window).height()

I am currently working on a project where I need to detect when an element enters the view in order to make it fade in. My initial approach was to track the element's vertical position on the page and trigger the fade-in effect when the scroll value w ...