Designing a webpage with CSS flexbox layout featuring three boxes and a sidebar

Is it possible to achieve a layout like this using flexbox?

https://i.sstatic.net/dmvZN.png (I'm unable to insert the image directly...)

Here is the structure:

<main>
  <section class="Div1">
  </section>
  <section class="Div2">
  </section>
  <aside class="Div3">
  </aside>
</main>

Can I rearrange the elements inside <main> but without adding any additional wrappers for Div1 and Div2?

I want to change the order of elements for mobile devices (placing Div3 between Div1 and Div2), but keep the sidebar for higher resolutions...

Thank you!

Answer №1

Do you think it's a good idea to utilize flexbox exclusively for organizing blocks on mobile devices, and for larger screens design the layout without it?

Check out my suggestion on jsfiddle.net.

main {
    width: 300px;
    display: flex;
    flex-direction: column;
}

.Div1 {order: 1;}
.Div2 {order: 3;}
.Div3 {order: 2;}

@media (min-width: 768px) {
    main {
        display: block;
    }
    main:after {
        display: block;
        content: '\0020';
        clear: both;
    }
    .Div1, .Div2 {
        width: 60%;  
        float: left;
    }
    .Div3 {
        margin-left: 60%;
    }
}

Answer №2

Flexbox may not be the ultimate solution for all layouts, but it certainly adds more possibilities to your design capabilities.

If you're looking to create a layout that requires changing the number of columns, you might need to explore alternative solutions.

A simple static solution using Flexbox could look like this:

html, body, .container {
  width: 100%;
  height: 100%;
  overflow: hidden;  
}

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

.container div {
  flex: 1 0 auto;
  height: 50%;
  width: 50%;
}

#alfa { background: red; }
#beta { background: green; }
#gama { background: blue; }
<div class="container">
  <div id="alfa">a</div>
  <div id="beta">b</div>
  <div id="gama">c</div>
</div>

If you require a more dynamic solution, consider creating separate elements for each column with nested content:

<div class="container">
    <div id="column">
        <div id="alfa">a</div>
        <div id="beta">b</div>
    </div>

    <div id="column">
        <div id="gamma">c</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

Preventing the collapse of the right column in a 2-column layout with Bootstrap 3

I've recently begun exploring Bootstrap and am attempting to achieve the following: (Left column - larger) Heading Heading 2 Heading Image Heading Image Heading Address (Right column - smaller) Heading Paragraph List items My issue ...

Prevent carousel from rotating while video is playing in Bootstrap 4

Currently, I am in the process of constructing a Bootstrap 4 carousel featuring various videos and other content. My goal is to have the carousel pause when a video is playing (with auto-play enabled) and resume once the video reaches its conclusion. I&ap ...

Basic adaptable menu for easy navigation

I have designed a custom menu in HTML: <div class="trigger" style="display:none;">menu</div> <div class="nav"> <ul> <li><a href="#">Home</a></li> <li><a href="#">News</a& ...

Tips on customizing the appearance of the "time" input type

Are there any resources that explain how to style the input type "time" completely? I have been searching but unable to find a comprehensive guide with all the necessary style attributes! The only example I came across is: input[type="time"]{ /**styl ...

React onClick event not firing

I'm new to working with React and I'm having trouble getting the onClick event to work properly. My goal is to render a modal when an icon is clicked, but for some reason the onClick event isn't responding. I've tried implementing it in ...

The MediaSource API is not supported on Chrome 27

My current browser version is Chrome 27, and based on this source, it is indicated that the MediaSource API comes pre-installed. However, upon further examination on the same page, the test section states that Your browser does not support the MediaSource ...

Is there a way to dynamically reference an image within a webpage?

I am looking to access pic1 through pic2 ... picn with the object named image. Example code for the image object. for (var src in images) { image[src] = new Image(); image[src].onload = function () { counter++; }; image[src].src = ...

vue-awesome-swiper / The button's name is synchronized, causing all other swiper elements to move in unison

<template> <swiper v-for="item in items" :key="item.id" :options="swiperOption" ref="mySwiper" @someSwiperEvent="callback"> <swiper-slide>I'm Slide 1</swiper-slide> <swiper-slide>I'm Slide 2</swiper-slid ...

Using the window.setInterval() method to add jQuery/AJAX scripts to elements at regular intervals of 60 seconds

I'm having trouble with automatically updating a div. I have a script that refreshes the page content (similar to Facebook) every minute. The issue is that this div is newly added to the page and contains some ajax/jQuery elements for effects. functi ...

The HTML image file input restricts the user's selection to images stored in the gallery

On mobile devices, when I press the input it displays two options: camera and gallery. However, I only want to select from the gallery. How can I disable the camera option? ...

Is there a way to stop text selection in Internet Explorer?

When it comes to CSS, I have the following code implemented: * { -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -o-user-select: none; user-select: none; } Despite this being ef ...

Javascript can determine the percentage values of the top and left positions

I am currently working on a project that involves capturing mouse clicks within a square region and adding a new div at the clicked spot. However, I need to convert the Xpx and Ypx coordinates into percentages using JavaScript because the size of the squar ...

Retrieve and manipulate the HTML content of a webpage that has been loaded into a

Hey, let's say I have a main.js file with the following code: $("#mirador").load("mirador.html"); This code loads the HTML content from mirador.html into index.html <div id="mirador"></div> I'm wondering if there is a way to chan ...

What are some ways I can ensure my webpage remains consistent and user-friendly when scrolling, rather than appearing distorted and jumbled?

Is there a way to automatically scroll my page when the window is at a small height, instead of adjusting it to the height by squeezing it? I've tried using overflow in different ways, but haven't been successful in getting the entire page to scr ...

Issue with image position shifting due to Bootstrap 4 Modal opening and closing

In the code snippet below, a bs4 modal is opened through a button when hovered over. The problem arises when the modal is closed after being opened, causing the image to move unexpectedly. This issue can be resolved by hovering over the container again. T ...

Creating custom styles using Material-UI's makeStyles function with both before and after

Currently, I'm tasked with a project that involves utilizing the CSS code below. .hexagon, .hexagon::before, .hexagon::after { width: 67px; height: 116px; border-radius: 18%/5%; } I am wondering if there is a method to apply the specified style ...

Enhancing the appearance of page navigation

In my WordPress theme, I have set up a pagination at the bottom of search results using the following code: function custom_pagination($pages = '', $range = 2) { $showitems = ($range * 2)+1; global $paged; if(empty($paged)) $paged = ...

Using inline CSS won't style the H3 element, but including an external CSS file will

I encountered an issue while trying to convert an image grid from using external CSS to inline style. When I moved all the attributes inline, the h3 element disappeared. Can anyone assist with this problem? Here is the original external CSS: <script&g ...

Eliminate image of variant's dimensions from the slider on Shopify

I am facing an issue with our Shopify store where duplicate variant images for colors are being displayed on the storefront. Although everything looks fine in the dashboard, the duplication is causing confusion for customers. Shopify support has advised ma ...

Adding a div element with a background behind a text element using Jquery

Is there a way to position an absolute div behind text or images while staying in front of background images? I have content with background images behind the text, so setting the z-index of the absolute div to -1 won't work. Any suggestions on how to ...