I have a flex-container which will be dynamically filled by elements. Container doesn't have fixed width (I use max-width: max-content;) and can contain as many element as I want. The problem is that I need spacing between these elements, but don't need spacing on the left and right side between an element and the container.
Of course I can do it with .element{margin-right: 10px} and .element:last-child{margin-right: none;}, but is there a way to achieve it without pseudo-classes and JS?
<div class="container">
  <div class="element">my element</div>
  <div class="element">my element</div>
  ...
  <!-- nubmer of elements changing -->
  ...
</div>
                You can use flex with the column-gap property.
Also, setting justify-content: space-between will ensure an even space between elements if the width of the parent container increases.
.container {
  display: flex;
  column-gap: 20px;
  justify-content: space-between;
}
.element {
  background: yellow;
}
<div class="container">
  <div class="element">my element</div>
  <div class="element">my element</div>
  <div class="element">my element</div>
  <div class="element">my element</div>
  <div class="element">my element</div>
  <div class="element">my element</div>
  <div class="element">my element</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With