Ignore flex container children but not grandchildren

前端 未结 2 738
滥情空心
滥情空心 2020-12-22 04:01

I have a simple flex element with some children, something like this:

相关标签:
2条回答
  • 2020-12-22 04:22

    Use display:contents (https://css-tricks.com/get-ready-for-display-contents/) but pay attention to the support (https://caniuse.com/#feat=css-display-contents)

    .container {
      display: flex;
      flex-wrap: wrap;
    }
    
    .container div.flex-box {
      width: 200px;
      margin: 20px;
      padding: 20px;
      font-size: 40px;
      border: 1px solid;
    }
    
    .subcontainer {
      display: contents
    }
    <div class='container'>
      <div class='flex-box'>one</div>
      <div class='flex-box'>two</div>
      <div class='flex-box'>three</div>
      <div class='subcontainer'>
        <div class='flex-box'>four</div>
        <div class='flex-box'>five</div>
      </div>
      <div class='flex-box'>six</div>
    </div>

    0 讨论(0)
  • 2020-12-22 04:33

    If the element is a child of a flex container, then it becomes a flex item. That's the general rule.

    However, most browsers will ignore that rule when the child is absolutely positioned.

    I'm not sure that's a useful solution in this case, but that's what you would have to do: absolutely position .subcontainer and make it flex container, so that the children become flex items.

    0 讨论(0)
提交回复
热议问题