Simple layout with flexbox question:
Desktop Layout requirements
- Two responsive columns
- 1st column with 30% of the width
- 2nd column with 70% of the width
Mobile Layout requirements
- Single responsive column
- 1st row with 100% width (this is the 1st column on Desktop)
- 2nd row with 100% width (this is the 2nd column on Desktop)
What I've got so far:
When I toggle to mobile
(via button click for example on snippet below) mode I set flex-direction: column
on the flex container. It seems to work.
#flexContainer {
display: flex;
}
#flexItem1 {
background-color: lightblue;
flex: 30%;
}
#flexItem2 {
flex: 70%;
background-color: lightgreen;
}
NOTE:
This question is about which CSS flexbox properties need to be changed in order to go from Desktop Layout to Mobile Layout. It's not about how to detect the window size or how to set a media query. I'm sorry if the way I wrote it didn't make it very clear.
QUESTION
Is there a cleaner way on how to do this ?
Do I need to set the flex
property of the flexItems
to 100%
when on mobile mode or can I leave them as 30%
and 70%
as they don't seem to matter when flex-direction
is set to column
?
function App() {
const [mobile,setMobile] = React.useState(false);
return(
<React.Fragment>
<div id="flexContainer" style={mobile ? {flexDirection: 'column'} : null}>
<div id="flexItem1">Item 1</div>
<div id="flexItem2">Item 2</div>
</div>
<button onClick={()=>setMobile(prevState=>!prevState)}>Toogle</button>
</React.Fragment>
);
}
ReactDOM.render(<App/>, document.getElementById('root'));
#flexContainer {
display: flex;
}
#flexItem1 {
background-color: lightblue;
flex: 30%;
}
#flexItem2 {
flex: 70%;
background-color: lightgreen;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>