<fieldset> flex-wrap does not work properly in Chrome

Asked

Viewed 306 times

2

I’m using the CSS property flex-wrap in some <div> so that I can show them or hide them without breaking the fluidity of the layout. However I noticed that when my container is a <fieldset>, the wrap does not happen in Google Chrome.

Do you have any idea how to render correctly in Chrome?

Or some explanation for this behavior?

If you want to test Here is a Fiddle, below has a snippet, and soon after screenshots of behavior on my machine (Windows 10, Chrome 69.0.3497.100 64-bit, Firefox 62.0 64bit).

[EDIT There is a a repository with the implementation bugs in the browsers.]

/* vvv apenas para melhor apresentação vvv */
*, :before, :after { box-sizing: border-box;}
.content {
  border: 1px solid lightblue;
  padding: 5px;
}
fieldset {
  margin: 0;
  padding: 0;
  border: none; min-width: 0;
}
/* ^^^ apenas para melhor apresentação ^^^ */

.row {
  display: flex;
  flex-wrap: wrap;
}

.row > .column {
  width: 50%;
  padding: 5px;
}
<h3>&lt;div&gt;</h3>
<div class="row">
  <div class="column">
    <div class="content">child</div>
  </div>
  <div class="column">
    <div class="content">child</div>
  </div>
  <div class="column">
    <div class="content">child</div>
  </div>
  <div class="column">
    <div class="content">child</div>
  </div>
</div>


<h3>&lt;fieldset&gt;</h3>
<fieldset class="row">
  <div class="column">
    <div class="content">child</div>
  </div>
  <div class="column">
    <div class="content">child</div>
  </div>
  <div class="column">
    <div class="content">child</div>
  </div>
  <div class="column">
    <div class="content">child</div>
  </div>
</fieldset>


Screenshot in Firefox

Screenshot no Firefox


Screenshot in Chrome

Screenshot no Chrome

  • Apparently it is a known bug, but I will be waiting for the answers to serve as a future reference to someone.

  • Hell, I couldn’t fix it with flex at all... Just with float:left. Oh you take the flex. Row and float: left; in the column you solve. Another option is to try Grid to see how it behaves inside the fieldset

  • Yes, how bizarre right?! I fell into this problem because bootstrap columns 3 break when you have one help-text and they get different heights. Only in this form I divide by sector with fieldset to be able to give a disable on the whole set with less overhead. Then I got a little crazy with this and I remembered that the guys here can give a hand

1 answer

2


The problem is that a <button> or <fieldset> is not designed to be a flex container (or grid).

Affected browsers:

  • Chrome
  • Edge
  • Firefox (partially corrected in version 52)
  • Opera
  • Safari (fixed in version 11)

It is worth noting that this behavior is applied to only three elements. It is they: <button>, <fieldset> and <legend>.

I believe that the idea behind this concept is to prevent them from turning a button into a table.

To tell this principle you can use a simple technique:

wrap/package the contents of a button with a span and do the span flex container.

<div>
    <button>
        <span><!-- using a div also works but is not valid HTML -->
            <span>Test</span>
            <span>Test</span>
        </span>
    </button>
    <p>
        <span>Test</span>
        <span>Test</span>
    </p>
</div>

CSS

button > span, p {
    display: flex;
    flex-direction: row;
    justify-content: center;
}

To a fieldset we can use another technique that is similar to the first.

Remove the flex property from fieldset and pass the same to a div which will also encapsulate the div item.

.FlexContainer {
  display: flex;
  margin: 1em;
}

.FlexItem {
  background: hsla(0,0%,0%,.1);
  padding: 1em;
  margin: 1em;
}

fieldset {
  border: 0;
  margin: 0;
  padding: 0;
}

input {
  background: #fff;
  border: 1px solid #aaa;
  margin: 1em;
  padding: 1em;
  width: 8em;
}
<fieldset>
  <div class="FlexContainer">
    <div class="FlexItem">
      <input placeholder="esses campos">
    </div>
    <div class="FlexItem">
      <input placeholder="não deveriam estar">
    </div>
    <div class="FlexItem">
      <input placeholder="em vertical">
    </div>
  </div>
</fieldset>

NOTE: Although they may not be flex containers, a button can be a flex item.

Response drawn from this question of OS

  • I managed to find the bugs on this page. I completely understand the bug with the buttons, but a fieldset was meant to be a container, I find it completely bizarre that they didn’t think of it when implementing flex in the browser. Don’t think?

  • Ahhh.. and in Firefox the single remaining bug is with the <button> same... Fieldset is already released. :)

  • I fully agree. A fieldset is to be a container but does not work with flex in most browsers.

  • What’s worse is I only use fieldset because giving a disabled in it I turn off all the inputs children and this is very practical. Because otherwise I would not use such an abandoned element. hahahaha

  • @fernandosavio, I found a way to correct, take a look at the answer,

  • 1

    I discovered the solution of the problem while asking the question John. hahahaha I posted the question because someone will still have the same problem and is already documented here. I will give

  • @fernandosavio, the more responses the more value you add to the community (good answers hahaha).

  • 1

    Yes.. I’m hoping someone will come up with a very crazy solution. I’m just for the creativity of the guys. hahahaha

  • Cade the other responses hahaha, wanted to see other solutions

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.