I currently have this:
But I want to achieve this:
Is there a way to do it by maintaining the same HTML structure?
Here’s the example code:
.container {
display: flex;
/* Makes the container an inline block element */
vertical-align: top;
/* Aligns the top edge of the container with the top edge of the previous element */
width: 600px;
}
label {
background-color: #8ff;
display: inline-block;
/* Makes the label element an inline block element */
}
.content-wrapper {
background-color: #f8f;
display: flex;
/* Makes the content-wrapper an inline block element */
flex-wrap: wrap;
white-space: nowrap;
/* Prevents the content from wrapping to the next line */
overflow-x: auto;
/* Allows horizontal scrolling if the content overflows */
}
span {
display: inline-block;
/* Makes the child elements inline block elements */
margin-right: 10px;
/* Add some spacing between the child elements */
}
<div class="container">
<label>Label</label>
<div class="content-wrapper">
<span>Content</span>
<span>Content</span>
<span>Content</span>
<span>Content</span>
<span>Content</span>
<span>Content</span>
<span>Content</span>
<span>Content</span>
<span>Content</span>
<span>Content</span>
<span>Content</span>
<span>Content</span>
<span>Content</span>
<span>Content</span>
</div>
</div>
I tried using flex, grid but nothing works, it works with the label inside the div but I’d prefer not to use that approach or use it as very last resouce
2
Answers
Here’s a potential way using
display: contents;
You don’t really need a lot of code for this. Simply make the wrapper an inline element.