Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox gap workaround for Safari

Tags:

flexbox

safari

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;
    }
}
like image 386
RDX10290 Avatar asked Dec 25 '20 21:12

RDX10290


People also ask

Does Flex gap work on Safari?

Safari 14.1 now supports the gap property inside Flexbox containers, along with row-gap and column-gap . Gaps in Flexbox make it possible for web developers to create space between Flex items without resorting to annoying margin hacks.

Does grid gap work on Safari?

Unfortunately, Safari already supports gap in grid, so this doesn't work. This is one of those weird cases where there is no easy CSS-only solution, though there have been proposals for workarounds.

Does Safari support CSS gap?

The gap property is an effort to provide a property similar to the CSS grid and flexbox. It will be in charge of the space (gutters) between columns and rows. However, certain browsers, including Safari, do not enable the gap property when using the flex layout.

Is flexbox gap supported in all browsers?

As you see, the gap works perfectly for both CSS grid and flex - on the browsers that support them. However, for the non-supporting browsers like old versions of Chrome and Safari, the result will look like the figure below.


Video Answer


2 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.

like image 193
BDB88 Avatar answered Oct 12 '22 21:10

BDB88


Property gap with display: flex is not supported by Safar version < 14 https://caniuse.com/flexbox-gap . You might want to replace display flex with grid:

display: grid;
grid-gap: 8rem; /* Safari 10-11 */
gap: 8rem;      /* Safari 12+ */

because grid's gap is supported in older Safari versions: https://caniuse.com/mdn-css_properties_gap_grid_context

like image 33
Vincente Avatar answered Oct 12 '22 20:10

Vincente