just getting started with flexbox.
After managing to align img div and text div, I added the "felx-wrap:wrap" to the container so that img and text would be stacked on top of each other on smaller screens. Now they are always stacked on top of each other. Why? Do I need a media query?
HTML:
<div class="banner-container">
<div class="banner-img">
<img src="https://www.region-muenchen.com/fileadmin/region-muenchen/Dateien/Fotos_Abb/Landkreiskarten/DAH_Ausschnitt.jpg" alt="map Dachau area">
</div>
<div class="intro-text">
<h1>Something</h1>
<h4>Something Something Something Something Something Something Something Something Something Something Something Something Something Something Something Something </h4>
</div>
</div>
CSS:
.banner-container{
display: inline-flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
flex-direction: row;
}
.intro-text{
border: 2px solid blueviolet;
margin: 10px;
}
.banner-img{
border: 2px solid blue;
max-width: 50%;
margin: 10px;
}
.banner-img img{
width: 100%;
height: auto;
}
I was expecting to stack my divs only when there isnt enough space to display them next to each other.
3
Answers
Can you use this css code
It’s because the intro-text div is taking 100% width, you have two options make some css media queries to control the behaviour for different screen sizes, or you can simply add
flex: 1;
to make both intro-text and .banner-img which will make both elements have the same weight in the flexbox.updated css:
Here’s the code, refined:
Explanation:
Setting ‘min-width’ will make sure that child divs take the least needed width, and if is not fitting, then it will stack.
Setting ‘flex: 0 0 40%’ will make sure the child divs are one beside the other on larger screens.
I hope this helps! 🙂