I’m building a cards kind of structure, and I’ve made a minimal example in a codepen here.
If that doesn’t work, here’s the HTML and CSS:
.my-div-container {
width: 100%;
height: 100%;
}
.my-div {
border: solid #FF0000 5px;
width: 200px;
height: 200px;
margin: 10px;
}
<!DOCTYPE html>
<head>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div class="my-div-container">
<div class="my-div"></div>
<div class="my-div"></div>
<div class="my-div"></div>
<div class="my-div"></div>
<div class="my-div"></div>
<div class="my-div"></div>
<div class="my-div"></div>
<div class="my-div"></div>
<div class="my-div"></div>
<div class="my-div"></div>
</div>
</body>
What I want is for the my-div
boxes to take up as much space there is horizonally, and then, when there are too many, to start a new line and start coming there. Kind of like how text works.
What I see now is that they are all vertically stacked. When I try something like putting display: inline-flex
in the my-div-container
, they are all squished together horizontally.
How can I allow them to behave like text?
3
Answers
Adding
inline-block
to themy-div
CSS fixes the issue.You can use
display:flex;
withflex-wrap:wrap;
(change the elements width and see that that will wrap)flex is correct, but you also need to define
flex-wrap: wrap;
to tell it how it should wrap.https://css-tricks.com/snippets/css/a-guide-to-flexbox/ is a really good guide for understanding flexbox, it visually explains the options with illustrations.
Also if you can I recommend changing the size of the items to not have as much blank space.