Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox - Horizontally Center 3 elements but align on center element

Tags:

css

flexbox

I'd like to use flexbox to center 3 elements together on a page. The middle element is ultimately the center of the page while the left and right elements can vary in width. Please refer to image below.

description of centering

I don't know if this is possible with flexbox. I'd just like it to be.

No real structural requirements other than I'd like to be able to align the elements vertically on a mobile device or as soon as the window width is too small for them to be aligned horizontally. mobile view of elements

like image 434
Tyler Graf Avatar asked Aug 03 '15 15:08

Tyler Graf


People also ask

How do I put 3 items in Flexbox?

For 3 items per row, add on the flex items: flex-basis: 33.333333% You can also use the flex 's shorthand like the following: flex: 0 0 33.333333% => which also means flex-basis: 33.333333% .

How can you position the Flex items to center on main axis?

To center our box we use the align-items property to align our item on the cross axis, which in this case is the block axis running vertically. We use justify-content to align the item on the main axis, which in this case is the inline axis running horizontally.


1 Answers

OK...it took some tweaking and possibly involves more wrappers than you might be comfortable with but I think I've gotten close.

Basic principle

We need rows, each of which contains three colums for this. Each of those column divs holds a "content" div which recieves the actual content (and thus bg color).

Using flexbox we can tell the center column (and it's content div) to be a fixed width while each of the side divs is flexible.

The content divs inside the side columns are auto-width and are aligned as required using flex-end/start etc.

When the media query kicks in, the rows become columns instead and the previous 'column' divs become rows in that column.

.row {
  display: flex;
  margin-bottom: .5em;
}

.content {
  text-align: center;
  color: white;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 1em;
}

.col {
  flex: 1;
  display: flex;
}



.center {
  flex: 0 0 350px;
  display: flex;
  margin: 0 .5em;
}

.center .content {
  background: pink;
  flex: 1 1 100%;
}

.left {
  justify-content: flex-end;
}

.left .content {
  flex: 0 1 auto;
  background: blue;
}

.right .content {
  flex: 0 1 auto;
  background: green;
}

@media screen and (max-width:700px) {
  .row {
    flex-direction: column;
  }
  
  .col {
    flex: 1;
    justify-content: flex-end;
  }
  
 
  .center {
 margin: 0;
  }
}
<div class="container">
  <div class="row">
    <div class="col left">
      <div class="content">Lorem ipsum dolor sit amet.</div>
    </div>
    <div class="col center">
      <div class="content">Lorem </div>
    </div>
    <div class="col right">
      <div class="content">Lorem ipsum.</div>
    </div>
  </div>
    <div class="row">
    <div class="col left">
      <div class="content">Lorem ipsum dolor sit. Lorem ipsum.</div>
    </div>
    <div class="col center">
      <div class="content">Lorem ipsum dolor.</div>
    </div>
    <div class="col right">
      <div class="content">Lorem</div>
    </div>
  </div>
</div>

Codepen Demo

There's still some work to do depending on the break-point you want and exacly how the 'mobile' sized design is supposed to look...but I think I've made a fairly good start.

like image 128
Paulie_D Avatar answered Nov 15 '22 12:11

Paulie_D