Flexbox gap workaround for Safari

Viewed 25357

I finished my website but I didn't realize that safari doesn't support the flexbox gap. Is there a way around this without having the mess anything up? This is mostly for my media queries.

<div class="social-media">
  <a href="https://github.com/">
    <img class="social-media__icon" src="img/github.png" alt="Github">
  </a>
  <a href="https://www.linkedin.com/">
    <img class="social-media__icon" src="img/linkedin.png" alt="LinkedIn">
  </a>
</div>
.social-media {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    gap: 8rem;
    margin-top: 10rem;
    padding-bottom: 2rem;
}

.social-media img {
    width: 90px;
    height: 90px; 
}
    
@media only screen and (max-width: 400px) {
    .social-media {
        gap: 3rem;
        margin-top: 5rem;
    }
    .social-media img {
        width: 62px;
        height: 62px;
    }
}
5 Answers

Use the Lobotomized owl selector: .parent > * + * {} to add a margin-left that gives you space between the elements that come after another element, this way you eliminate the undesired margin it creates when you put the margin directly to the child's first element (.social-media img{})

    .social-media > * + * { margin-left: 8rem;}

Here you can read more about the Lobotomized Owl Selector

Edit: Gap is now supported in safari so you should be able to use it no problem.

I think you could make a div container and put justify-content: space-between; then i think it should work

The accepted answer has the problem, that you will have a wrong margin on the first element if when there is only one row. Also centered elements will always be 8rem too far the right.

Better solution that will always work with correct spacings:

.container {
  display: flex;
  // the trick is to set margins around boxes, but correct the margins around elements that are situated at the border of the container with negative margins
  margin: 0 -10px -20px -10px;
}

.box {
  min-width: 100px;
  height: 100px;
  background-color: deeppink;
  margin: 0 10px 20px 10px;
}
<div class='container'>
  <div class='box'>1</div>
  <div class='box'>2</div>
  <div class='box'>3</div>
  <div class='box'>4</div>
</div>

You can remove the gap class and add another one to child elements

<div class="d-flex"> // it was "d-flex gap" previously
  <div class="mx-2">
    //content
  </div>
  <div class="mx-2">
    //content
  </div>
</div>
Related