Is it Possible to Create a Three Div/Two Column Layout Using Flexbox?

I've been experimenting with flexbox in an attempt to achieve the following layout. Originally, I had a sidebar on the left containing both an image and navigation, along with a main content area. However, when viewed on mobile devices, the sidebar would wrap underneath the main content.

The issue arises from my need for the image to always stay at the top on mobile screens. To address this, I have been working on incorporating three sibling div elements within one wrapper div.

I'm wondering if achieving this layout is possible using flexbox, or if I should consider utilizing CSS grid instead?

https://i.sstatic.net/Qk5MM.png

Answer №1

To achieve the desired layout, CSS Grid is typically recommended, but it can also be done using CSS Flexbox.

Simply create a wrapper div with three nested divs inside for a mobile-first approach, and set .content to flex: 1 to expand the height of the viewport.

For desktop (using

@media screen and (min-width: 1000px)
), adjust the order of .navigation and .content, and assign appropriate widths to each div. The only change needed for div.wrapper is adding flex-flow: column wrap for correct wrapping.

.wrapper {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 100%;
}

.box {
  display: flex;
}

.content {
  flex: 1;
}

@media screen and (min-width: 1000px) {
  .wrapper {
    flex-flow: column wrap;
  }
  
  .navigation {
    order: 2;
  }
  
  .content {
    order: 3;
  }
  
  .image,
  .navigation {
    width: 200px;
    flex: 50%;
  }
  
  .content {
    width: calc(100% - 200px);
    flex: 0 0 100%;
  }
}


/* Generic styling */

html,
body {
  height: 100%;
}

body {
  margin: 0;
  padding: 0;
}

.image {
  background: orange;
  height: 60px;
}

.content {
  background: lightblue;
}

.navigation {
  background: lightgreen;
  height: 60px;
}
<div class="wrapper">
  <div class="box image">Image</div>
  <div class="box content">Content</div>
  <div class="box navigation">Navigation</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

Conceal the div by clicking outside of it

Is there a way to conceal the hidden div with the "hidden" class? I'd like for it to slide out when the user clicks outside of the hidden div. HTML <!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.c ...

Struggling with applying mixins

Is it possible to select only the top border using this mixin? Currently, I only need a top border but would like to have the option to use others in the future. Using separate mixins for each side of the border seems inefficient. .bordered(@top-width: 1 ...

Revealing non-div elements with the CSS hover attribute

I'm looking for a way to utilize the CSS hover property on individual elements within a div in order to affect specific span elements. For example, hovering over "something1" should reveal "text1" and so forth. Is there a way to achieve this without ...

What's the best way to align grid items that are positioned next to each other in CSS Grid?

I'm having trouble aligning two grid elements next to each other. What I want is for the heading, paragraph, and bottom links to be in one grid element and the image to be in another. When I tried using grid-template-columns: 1fr 2fr;, one element got ...

Canvg | Is there a way to customize canvas elements while converting from SVG?

Recently, I encountered an issue with styling SVG graphics dynamically generated from data. The SVG graphic appears like this: https://i.sstatic.net/xvIpE.png To address the problem, I turned to Canvg in hopes of converting the SVG into an image or PDF us ...

Javascript and CSS combo for creating a stylish fade effect

Hey everyone, I have a design challenge where I need to change the color of a picture inside a box when the user hovers over it. I have four boxes like this and my goal is to make everything else fade out when a user hovers over a specific box. Anyone kn ...

Issue with multi-level bootstrap navbar: Unable to hover on child elements

Currently, I am working on implementing a multi-level navbar in my project using Bootstrap Navbar along with additional CSS and Jquery. If you want to review the codes, they can be found at: CodePen $(function() { // ------------------------------- ...

Issue with WordPress navigation menu bug

On my website, I have implemented WP nav menu and it seems to be functioning correctly. However, I am facing an issue with the sub link under the home link. The screenshot shows the problem clearly. In the provided image, the "support our work" sub link a ...

Why is Chrome ignoring the flexbox CSS in the footer section of my website?

My footer flexbox CSS is working on various browsers and devices, but for some reason, Chrome on my iMac is not responding correctly. The website is www.batistaelectric.com. It works on my iPhone Chrome browser, Safari iPhone browser, iMac Safari browser, ...

Placing an image on top of a background image in CSS while maintaining a fixed aspect ratio

My CSS code is currently resizing the background image to fit the browser size while keeping the center of the image in the center of the window. Now, I need to add another image, like an icon in a circle, at the center following the same aspect ratio rule ...

Floating point expansion caused by the addition of padding

Currently working on a basic 2 column layout design for a website, but ran into a hiccup. Whenever I try adding padding to the left-floated column, it ends up expanding beyond the width that I have set. Unfortunately, I haven't been able to find a sol ...

issues with the functionality of bootstrap modal

I'm currently working on a project where I need to set up a modal popup using bootstrap. The website I'm working on is organized by departments, so the only part of the code that I have control over is the main body of the site. I have included t ...

Troubleshooting Issue with Absolute Positioning and Hover Effect on Material UI Button

I have a div containing a hidden button and an inner div filled with graphs and text. Occasionally, I need to blur the inner div and make the button float on top in the center of the blurred section, similar to the layout seen on subscription prompts found ...

Customize the appearance of buttons and tabs located in the header section

Front end development is new to me and I'm trying to learn, but I've hit a roadblock. I have 4 components - 2 buttons and 2 tabs that I want to style in a header at the top of the page. Specifically, I want one button on the top left with padding ...

Conflict between iOS 7+ swipe back gesture and stateChange animations

When transitioning between states in an AngularJS application, I utilize CSS animations to add visual appeal to the view change. This may involve applying fades or transforms using classes like .ng-enter and .ng-leave. In iOS 7+, users can swipe their fin ...

Playing around with adjusting the size of a div that is positioned absolutely on the right side

Snippet: http://jsfiddle.net/scottbeeson/TU6Zw/ HTML Code: <div id="itemMap" class="shadow"> <div class="mapHeader">List of Items</div> <div class="mapItem">Item 1</div> <div class="mapItem">Item 2</div& ...

2 column setup in the last row of a CSS grid

Can anyone help me troubleshoot an issue with aligning two buttons to the right in a form using CSS grid? I've attached a screenshot for reference. https://i.stack.imgur.com/DFGrx.png **css ** .wrapper { width:75%; padding-top: 15px; padd ...

Showing a collection of cards within a dynamic container set up in a 3x3 grid layout to start

In an attempt to showcase cards within a responsive container utilizing bootstrap and django, my goal is to create a 3x3 grid layout on extra-large screens with scrollable overflow that adjusts based on device width. Initially, I experimented with wrapping ...

I wonder where this unique design originated from on my webpage?

This is a screenshot of Chrome Developer Tools that shows the styling information. Usually, the style is clearly defined in a specific CSS file (like in this screenshot, datepicker.css). I am attempting to modify the styling at the very top of the screens ...

Attempting to create a slider utilizing jQuery

I'm currently working on creating a slider using jquery. I have downloaded the cycle plugin for the slider and included it in my file. The slider consists of 7 pictures. Below is the code I am using, can someone please help me identify any issues? &l ...