The card spans 100% width of the parent element on larger screens but I’m trying to work out how to get the div that contains the image (ie. .card-img) to appear above the content on smaller screens. I know that I’d use media queries but I don’t know how to adjust the code to turn off flexbox to make this happen.
You need to enable wrapping for this to happen.
On the .card-list-page, flex parent element, add flex-wrap: wrap;
Then to hint at what point you want the .card-img to wrap, give it a flex-basis in the flex shorthand property.
Also note that once the divs have wrapped then the card-img div will have no height as such because it has no actual content (apart from a little height due to the parent containers min-height). You may want to ensure there is some min-height for your image to show
No you don’t need to add the min-width:320px media query at all as they are all collected in the max-width:767px media query. If you did add a min-width:320px media query then you miss any devices that are less than 320px and you never know if that may be a future fashion to have smaller phones.
Keep things simple and don’t over-complicate the issue.
That’s good advice - thanks for explaining this Paul. I noticed though that the layout falls apart in internet explorer 11 and earlier - is there a way to fix it for that browser?
Ok back at the computer now and just checked in IE11 and the changes I mentioned will fix the issues in IE11. i.e. If you have a flex-basis as zero then add the percent to it.
For the calc example you would do it like this:
flex: 1 1 50%;
flex-basis:calc(50% - .5em);
Keep the calc in the longhand rule and not the shorthand and then in works in IE11.
Thanks very much for that - it’s working now. Just with regard to keeping the flex-basis in longhand, would I need to do this for the flex-grow and flex-shrink also, or can they stay in short-hand? Also, is there an easy way to get flexbox to be backwards compatible?
When using calc just do it like I showed you with the 2 rules.
flex: 1 1 50%;
flex-basis:calc(50% - .5em);
You don;t need to do anything to the shrink or grow properties. We are just over-riding the flex-basis so that IE11 can understand it. You have to be careful where you use calc if you need to support IE11 as it doesn’t like it when its mixed with shorthand rules.
Older browsers will just ignore flex so as long as you have a reasonable linear type layout that should be enough. I would avoid adding extra rules just to cater for older browsers as they just muddy the waters. For example you could float or use inline-block for flex items to get columns but it doubles the css and is a pain to maintain.
If you use the developer tools and comment out the display:flex rule you will see how it looks in older browsers (you are basically talking older versions of IE anyway which should not be supported as they are security risks). The first thing you would notice when removing flex is that we get a linear layout but that your background image reverts to zero height. Therefore I would ensure the background image had a minimum height to start with and then it would look ok in older browsers.
It is possible to use inline-block for flex-items and get a similar look but its a bit fiddly and in my mind a waste of time unless you have no choice.