1

Im new to flexbox. so probably a rookie question.

What i'm trying to achieve is the following: 2 columns with unknown width / height aligned one column left of the parent and one column centered.

enter image description here

Yens
  • 915
  • 4
  • 12
  • 24
  • 2
    I think you will need to use `position: absolute` for left column to take it out of normal DOM flow. That's seems to be the only possible way for this. – Mohammad Usman Dec 13 '16 at 08:25

1 Answers1

2

It depends on your situation but a little workaround might help by adding a third invisible element.

Example HTML:

<div class="parent">
    <div class="left"></div>
    <div class="center"></div>
    <div class="right"></div>
</div>

Example CSS:

.parent {
    display: flex;
    justify-content: space-between;
 }
.left {
    width: 100px;
    height: 60px;
    background: red;
}
.center {
    width: 50px;
    height: 70px;
    background: blue;
}
.right {
    width: 70px;
    height: 100px;
    background: yellow;
    visibility: hidden;
}

Fiddle: https://jsfiddle.net/eqcndn3m

As an alternative to flex you can use 3 floating elements with equal width and keep the last one empty:

<div class="parent">
    <div class="column align-left">
        <div class="left"></div>
    </div>
    <div class="column">
        <div class="center"></div>
    </div>
    <div class="column"></div>
</div>

.parent {
    text-align: center;
}
.column {
    float: left;
    width: 33.33%;
}
.align-left {
    text-align: left;
}
.left {
    display: inline-block;
    width: 100px;
    height: 60px;
    background: red;
}
.center {
    display: inline-block;
    width: 50px;
    height: 70px;
    background: blue;
}

Updated Fiddle: https://jsfiddle.net/eqcndn3m/1

Johann Kratzik
  • 764
  • 5
  • 11
  • Thanks. I've tried this hack, but for this to work, the left and right column should have equal width. If the right column is larger then the left column, the center column will move to the right – Yens Dec 13 '16 at 11:25