Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make flexbox responsive?

I have a div with two elements that I want to stack horizontally. Div#C has fixed width, and div#B will fill up the rest of the space. However the contents of div#B might be fixed width (which is dynamic) or 100% width (of div#B).

The effect I want is, if the screen width is small enough such that, right before div#B and div#C start to overlap or if the width of div#B is small enough, I want div#B and div#C to stack vertically, and each have width 100% of div#A. The problem is if I use a media query, I have to give a fixed min width for it to stack horizontally. With a fixed width, it doesn't account for any fixed width content in div#B. Does anyone know how to fix this preferably only in CSS?

#A {
    display:flex;
}
#B {
    flex:1;
}
#C {
    width:300px
}
<div id="A">
    <div id="B">b</div>
    <div id="C">c</div>
</div>
like image 308
omega Avatar asked Aug 16 '16 14:08

omega


People also ask

How is flexbox responsive?

Responsive Web Design using Flexbox This layout is one-dimensional and permits the placement of elements inside a container with equally distributed space. It makes elements responsive which means that the elements change their behavior according to the kind of device displaying them.

How do I make my flexbox grid responsive?

Creating the responsive gridUsing display: flex , our grid-row stretches to the full size of the container. We use flex-flow: wrap to designate that child divs (our columns/grid-items) should wrap if they exceed the width of the row. Then, flex-flow: row means our grid-items will flex from left to right.

Is flex good for responsive?

The flex value is an alternative to block elements floated and manipulated using media queries. Instead, developers can build a flexible container, flexbox for short. It's great for mobile screens and responsive content for dynamic layouts and webapps.

Is Flex layout responsive?

Flexbox provides a clean, consistent way to build out a responsive layout. Its popularity and cross-browser support make it a great resource to implement into your current or future projects.

Is flex-wrap responsive?

The flex-wrap property is a quick way to make parent elements more responsive on various screen sizes. As with flexbox in general, it simplifies page layouts so you don't have to manually set breakpoints or manage the page overflow yourself.


2 Answers

Although I had initially thought this might not be possible there is one option I can think of.

Give div#B a ridiculous flex-grow value in comparison and give div#C just flex:1 0 300px

div {
  padding: 2em;
}
#A {
  padding: 0;
  display: flex;
  flex-wrap: wrap;
}
#B {
  flex: 99;
  background: pink;
}
#C {
  flex: 1 0 300px;
  background: orange;
}
<div id="A">
  <div id="B">Lorem ipsum dolor sit amet</div>
  <div id="C">c</div>
</div>

Codepen Demo

When div#B eventually shrinks small enough to force wrapping, the flex-grow:1 on div#C wil cause it to expand to full width and the 'upper' div#B will now take up the full width also since it cannot expand past 100% width of that 'row'

enter image description here

like image 145
Paulie_D Avatar answered Oct 19 '22 21:10

Paulie_D


Try this👇

#A {
    display:flex;
    flex-flow: row wrap;
}
#B {
    flex-basis: 60%;
}
#C {
    flex-basis: 40%;
}


@media (max-width: 540px){
   #B {
        flex-basis: 100%;
        //whatever width you want in mobile view
    }

    #c {
        flex-basis: 100%;
        //whatever width you want in mobile view
    }
}
like image 1
DINESH Adhikari Avatar answered Oct 19 '22 20:10

DINESH Adhikari