flexbox not wrapping items - css

I'm really new to flexbox and I'm trying to understand why my flexbox won't wrap when the browser window width gets smaller. The row never turns into a column. why is that? I appreciate any help?
stackblitz
.listingBody {
min-height: 100vh;
}
.listingCard {
width: 800px;
height: 400px;
border-width: 2px;
border-style: solid;
display: flex;
flex-flow: row wrap;
border-radius: 25px;
background: white;
margin: 0 auto;
justify-content: center;
align-items: center;
box-shadow: 10px 10px 5px -2px #888;
}
.image {
width: 200px;
height: 200px;
background-color: red;
}
.rules {
width: 200px;
height: 200px;
background-color: green;
}
.info {
width: 200px;
height: 200px;
background-color: blue;
}
<div class="listingBody">
<div class="listingCard">
<div class="image">
test
</div>
<div class="info">
test
</div>
<div class="rules">
test
</div>
</div>
</div>

It won't wrap because you set a fixed width to the parent div. You can either just use max-width or remove it altogether.
.listingBody {
min-height: 100vh;
}
.listingCard {
height: 400px;
border-width: 2px;
border-style: solid;
display: flex;
flex-flow: row wrap;
border-radius: 25px;
background: white;
margin: 0 auto;
justify-content: center;
align-items: center;
box-shadow: 10px 10px 5px -2px #888;
}
.image {
width: 200px;
height: 200px;
background-color: red;
}
.rules {
width: 200px;
height: 200px;
background-color: green;
}
.info {
width: 200px;
height: 200px;
background-color: blue;
}
<div class="listingBody">
<div class="listingCard">
<div class="image">
test
</div>
<div class="info">
test
</div>
<div class="rules">
test
</div>
</div>
</div>

This happens because you are setting a fixed with to the container div without a max width. You can just change the .listingCard style to this:
.listingCard {
width: 800px;
max-width: 100%; /* <-- add this */
height: 400px;
border-width: 2px;
border-style: solid;
display: flex;
flex-flow: row wrap;
border-radius: 25px;
background: white;
margin: 0 auto;
justify-content: center;
align-items: center;
box-shadow: 10px 10px 5px -2px #888;
}
Or just replace width: 800px and max-width: 100% with max-width: 800px which will provide the same effect.
By doing it like this it the content will have a max width of 800px in case the window is larger than 800px or adjust to the window size in case it is resized to a lower width.

The flex items are not wrapping because you have a width set on the flexbox container, so narrowing the browser window smaller than that won't change the size of the flexbox container and therefore the flex items will have no reason to wrap.
Try replacing width: 800px; on .listingCard with max-width: 800px;

Related

How can I make a div 100% height inside block div with flex-grow 1 parent?

I've searched and tried a lot of solutions but none of them is working for my case.
I have this set up where neither body nor main should change. Inside them I can add as many divs as I want and change any style.
<div class="body">
<div class="main">
<div class="should-be-full-height">
Content
</div>
</div>
</div>
.body {
display: flex;
flex-direction: column;
min-height: 100vh;
height: auto;
width: 100%;
background-color: #CCC;
border: 1px solid black;
}
.main {
height: auto;
flex-grow: 1;
display: block;
background-color: red;
border: 1px solid white;
}
.should-be-full-height {
background-color: grey;
border: 1px solid black;
}
https://jsfiddle.net/eqwu3yfh/
I added background colors and borders just to see better what's going on.
I need the div with the .should-be-full-height class to use 100% of the height of its parent (.main). How can I achieve that?
Thanks. Sorry if this has been asked, I couldn't find an answer.
You either remove flex-direction: column from body and you can use height:100%
.body {
display: flex;
/* flex-direction: column;*/
min-height: 100vh;
height: auto;
width: 100%;
background-color: #CCC;
border: 1px solid black;
}
.main {
height: auto;
flex-grow: 1;
display: block;
background-color: red;
border: 1px solid white;
}
.should-be-full-height {
background-color: grey;
border: 1px solid black;
height: 100%;
}
<div class="body">
<div class="main">
<div class="should-be-full-height">
Hi
</div>
</div>
</div>
Or you change the display of main to be flex and use width: 100%
.body {
display: flex;
flex-direction: column;
min-height: 100vh;
height: auto;
width: 100%;
background-color: #CCC;
border: 1px solid black;
}
.main {
height: auto;
flex-grow: 1;
display: flex;
background-color: red;
border: 1px solid white;
}
.should-be-full-height {
background-color: grey;
border: 1px solid black;
width: 100%;
}
<div class="body">
<div class="main">
<div class="should-be-full-height">
Hi
</div>
</div>
</div>
I know you said you cannot change body and main but I don't see any other solution.

How to shrink container in flexbox

I want my container to shrink when page is smaller. How can i do that?
.home-container {
display: flex;
flex-direction: column;
background-color: #ffefd0;
border-radius: 15px;
box-shadow: 3px 3px 3px 1px grey;
margin: 30px auto 0 auto;
width: 800px;
}
Don't use a set width. It's best to avoid setting height and width (when possible) to avoid problems with responsiveness. Instead of width, use max-width: 800px.
Do not provide width property as you have passed it ... instead add padding to that container accordingly and set width property to max-width like this
.home-container {
display: flex;
flex-direction: column;
background-color: #ffefd0;
border-radius: 15px;
box-shadow: 3px 3px 3px 1px grey;
margin: 30px auto 0 auto;
padding:20px;
width: max-width;
}
maybe like this ??
.home-container {
display: flex;
flex-direction: column;
background-color: #ffefd0;
border-radius: 15px;
box-shadow: 3px 3px 3px 1px grey;
margin: 30px auto 0 auto;
max-width: 800px;
grid-gap:10px;
height:auto;
}
.box {
height:80px;
width:80px;
margin:20px;
background-color:blue;
border:1px solid gray;
}
<div class="home-container">
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
<div class="box">
</div>
</div>

How to force a div to top/bottom of viewport while not taking it out of document flow?

I'm wondering how I can make the blue div 'stick' to the top and bottom of the page (so you can't scroll beyond it) without breaking the document flow (I don't want it to overlap the content div). A crude version is here in the code snippet. If I set 'absolute' on it, it breaks flow and the content goes under the menu. If I leave it relative, you can scroll beyond it (I want it to stay at top: 0, bottom: 0 for a full 100vh with its own scrolling).
Is there something that I'm missing?
* {
box-sizing: border-box;
}
.main {
display: flex;
flex-direction: row;
}
.sideNav {
width: 100px;
height: 100vh;
border: 5px solid red;
}
.sidePanel {
width: 50px;
height: 100vh;
border: 5px solid blue;
}
.content {
display: flex;
flex-direction: column;
flex-grow: 1;
height: 200vh;
margin: auto;
width: auto;
border: 5px solid green;
}
<div class="main">
<div class="sideNav">Side Nav</div>
<div class="content">This is the content area</div>
<div class="sidePanel">Side Panel</div>
</div>
You really can't do it without removing it from the document flow.
But if you know the width of the side panel, you can apply it as a right margin to the content div.
.main {
display: flex;
flex-direction: row;
}
.sideNav {
width: 100px;
height: 100vh;
border: 5px solid red;
}
.sidePanel {
width: 50px;
height: 100vh;
border: 5px solid blue;
position: fixed; /* new */
right: 0; /* new */
}
.content {
display: flex;
flex-direction: column;
flex-grow: 1;
height: 200vh;
margin: auto;
width: auto;
border: 5px solid green;
margin-right: 50px; /* new */
}
* {
box-sizing: border-box;
}
body {
margin: 0;
}
<div class="main">
<div class="sideNav">Side Nav</div>
<div class="content">This is the content area</div>
<div class="sidePanel">Side Panel</div>
</div>

How to have a fluid flex layout

I am trying to make a fluid flex field where if there is no enough space then it should drop to next line. As you can see in this example if you decrease the size of the field it doesnt drop to next line because I am using flex.
.container {
width: 80%;
border: 2px solid #ddd;
padding: 20px;
display: flex;
overflow: hidden;
}
.container .panel {
flex: none;
}
.container .panel-info {
display: flex;
align-items: center;
}
.container .panel-info .dot {
background-color: #ccc;
width: 4px;
height: 4px;
border-radius: 50%;
margin: 0 8px;
}
<div class="container">
<div class="panel">Some Long Info</div>
<div class="panel-info">
<div class="dot"></div>
<div class="info">Information</div>
</div>
</div>
Use flex-wrap: wrap.
More information on MDN about the flex-wrap property

Firefox flex and max-width

I’m trying to display an image centered and resized by CSS (max-width / max-height, flex).
This is working fine in Chrome but not in Firefox. What’s wrong?
a {
border: 1px solid red;
display: flex;
height: 100px;
width: 100px;
justify-content: center;
align-items: center;
box-sizing: border-box;
padding: 5px;
}
img {
max-width: 100%;
max-height: 100%;
border: 1px solid green;
}
<a class="frame">
<img src="http://placehold.it/500x150"/>
</a>
See: http://jsfiddle.net/ymLb50w0/2/
Try this solution...
a {
border: 1px solid red;
display: inline-block;
height: 100px;
width: 100px;
justify-content: center;
align-items: center;
box-sizing: border-box;
padding: 5px;
max-width: 100%;
}
img {
max-width: 100%;
max-height: 100%;
border: 1px solid green;
}
<body style="text-align: center;">
<a class="frame">
<img src="http://placehold.it/500x150"/>
</a>
</body>
You may try to align it vertically like it's explained here
Horizontal alignment shouldn't be a problem

Resources