Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button with display: flex does not work in Safari. Causing size and alignment issues

I'm having some problem with display: flex in both Safari and iOS Safari. Safari has alignment issues and iOS Safari has both alignment and size issues. Is this expected or a valid bug in Safari?

Edit: Also, height in percentage on the child SVG inside the Button does not work either. This works in all other browsers. I mane another more advanced example with SVG elements, and I added an anchor to compare to the button. More advanced example here

My simple example

HTML:
<div class="flex">
  <div class="col col-1">
    <button class="sub-col">
      <span class="span"></span>
    </button>
    <div class="sub-col">2</div>
    <div class="sub-col">3</div>
  </div>
  <div class="col col-2"></div>
  <div class="col col-3"></div>
</div>


CSS:
.flex {
  display: flex;
  flex-direction: column;
  height: 80vh;
  background: #666666;
  position: fixed;
  width: 100%;
}

.col {
  display: inherit;
  flex: 1 1 auto;
  background: #ccffcc;
}
.col-1 {
  flex: 0 0 10vh;
  background: #ffcccc;
}
.col-3 {
  flex: 0 0 10vh;
  background: #ccccff;
}

.sub-col {
  display: inherit;
  flex: 0 0 25%;
  justify-content: center;
  align-items: center;
}

.span {
  width: 10px;
  height: 10px;
  background: #ffaaaa;
}

Screenshots:

This is what it should look like, and does in Chrome, FF and Edge (Haven't tested IE)

Chome, FF and Edge

This is what it looks like in Safari

Safari Desktop

And this is iOS Safari

iOS Safari

like image 637
Jompis Avatar asked Jun 01 '17 07:06

Jompis


People also ask

Does Flexbox work in Safari?

Safari versions 9 and up support the current flexbox spec without prefixes. Older Safari versions, however, require -webkit- prefixes. Sometimes min-width and max-width cause alignment problems which can be resolved with flex equivalents. See Flex items not stacking properly in Safari.

Why is flex not working?

If you're having trouble activating your Flex streaming TV Box, the most common solution is to restart it by reconnecting your USB-C power cord and HDMI cable to both the Flex streaming TV Box and TV.

How do you center items with display flex?

To center the inner div element we will make the parent a flex container. By adding the display: flex; property we make the section element a flex container allowing us to adjust the layout of the div which is now a flex item. To center out item horizontally we use the justify-content: center; .

How do I display my flex Right align?

Answer: Use the CSS margin-left Property You can simply use the CSS margin-left property with the value auto to right align flex item within a flex container. Please note that all child elements of flex container automatically become flex items.


2 Answers

You can't set display flex to button and fieldset elements. chk below link. Wrap your element with div or span tags

https://github.com/philipwalton/flexbugs#9-some-html-elements-cant-be-flex-containers

like image 94
Naren Srinivasan S Avatar answered Oct 18 '22 20:10

Naren Srinivasan S


The button element, and a couple other elements for that matter, cannot be flex containers in some browsers. Safari is one of them.

You can try a simple solution though, if you like. The idea is, you wrap your .span inside .span-wrapper. Then you let .span-wrapper be your flex container.

Like that:

HTML:

<div class="flex">
  <div class="col col-1">
    <button class="sub-col">
      <span class="span-wrapper">
        <span class="span"></span>
      </span>
    </button>
    <div class="sub-col">2</div>
    <div class="sub-col">3</div>
  </div>
  <div class="col col-2"></div>
  <div class="col col-3"></div>
</div>

CSS:

.flex {
  display: flex;
  flex-direction: column;
  height: 80vh;
  background: #666666;
  position: fixed;
  width: 100%;
}

.col {
  display: inherit;
  flex: 1 1 auto;
  background: #ccffcc;
}
.col-1 {
  flex: 0 0 10vh;
  background: #ffcccc;
}
.col-3 {
  flex: 0 0 10vh;
  background: #ccccff;
}

.sub-col {
  display: inherit;
  flex: 0 0 25%;
  justify-content: center;
  align-items: center;
}

.span {
  width: 10px;
  height: 10px;
  background: #ffaaaa;
}

.span-wrapper {
  display: flex;
  flex-basis: 100%;
  justify-content: center;
  align-items: center;
}
like image 20
Chris Elioglou Avatar answered Oct 18 '22 20:10

Chris Elioglou