0

Question

Can you produce such layout with CSS?

I want to have a single row with 2 children:

  • Child A has multiple rows,
  • Child B has a single one.

I want the the layout in which:

  1. Child A accomodates on a single line as many children as possible for A and B to still fit on a single line.
  2. Child A has the smallest width possible to achieve #1.
  3. Child B then takes all the remaining space.

My HTML:

<div class="Container">
  <div class="ChildA">
    <input type="text" />
    <input type="text" />
    <input type="text" />
    <input type="text" />
  </div>
  <div class="ChildB">
    <button>
      Click me please
    </button>
  </div>
</div>

My desired layout:

Desired layout

Sketch of a solution using Flexbox

So far I came up with the following CSS using Flexbox:

.Container {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  border: 4px solid black;
}

.ChildA {
  flex-grow: 0;
  flex-shrink: 1;
  flex-basis: auto;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  border: 4px solid red;
}

.ChildB {
  flex-grow: 1;
  flex-shrink: 0;
  flex-basis: auto;
  border: 4px solid blue;
}

It produces layout pictured below. The problem is it violates rule #2 - child A does not have the smallest width possible, but instead takes all the remaining space, which I want to be taken by child B instead.

Layout I was able to produce

CSS Grid?

It seems the solution may be possible using CSS Grid (and it's auto-fit value), but I haven't been able to produce a fully working one myself yet. The closes I've come is that pen, which works fine on some window widths, but I cannot work out how to remove max-width: 50vw constraint.

Note

I want a fully dynamic/flexible solution, which would work with content of any size in the containers.

Children content in my example are inputs and button, but that's just a random example - they can be <div>s or any other elements, if it makes the layout easier to implement.

Fiddle

https://codepen.io/anon/pen/mjdmXG?editors=1100

Robert Kusznier
  • 6,471
  • 9
  • 50
  • 71
  • You cannot achieve this because : https://stackoverflow.com/questions/34995740/css-when-inline-block-elements-line-break-parent-wrapper-does-not-fit-new-width – Temani Afif Jul 10 '18 at 09:01
  • But this answer relates specifically to `inline-block` display. That doesn't mean you can't achieve it with Flex or Grid. Especially that the last answer in that question posts a Grid solution. I'm trying to work how to port it to my situation... – Robert Kusznier Jul 10 '18 at 09:07
  • and your elements are inline-block (input) that's why I said you cannot achieve it with what you have but this doesn't mean it's impossible at all ;) – Temani Afif Jul 10 '18 at 09:10
  • But in my question I don't put any restrictions on what kind of `display` you set on the elements. So you can set some other `display` value on those inputs (actually they are just an example, the content could be anything else). I added a note about it in the question, so that it's clear. – Robert Kusznier Jul 10 '18 at 09:18
  • in this case you have your answer in the link I shared ;) simply use grid for the A element and make the B element to fit the remaining space – Temani Afif Jul 10 '18 at 09:21
  • Then if you post it as an answer, I'll accept it :). – Robert Kusznier Jul 10 '18 at 09:24
  • well, working ont it ;) not easy as expected – Temani Afif Jul 10 '18 at 09:30
  • I came up with [this](https://codepen.io/anon/pen/bjGWYy?editors=1100) which works as desired on some page widths, but `max-width: 50vw` constraint makes it not working on others. I'm not sure how to remove it, but maybe it'll help you. – Robert Kusznier Jul 10 '18 at 11:14

0 Answers0