Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make boxes align side-by-side instead of on top of each other in flexbox?

Tags:

html

css

flexbox

Wondering why the itemContainer and priceContainer won't show up next to each other?

Do I need to include any kind of file for FlexBox?

I thought it was built into CSS3. Is there a standard add-on to utilize?

.container {
  display: flex;
  display: -webkit-flex;
  width: 100%;
  height: 100%;
  flex-direction: row;
}
#orderContainer {
  width: 15%;
  border: 1px solid #f2f2f2;
  height: 100%;
  flex-direction: row;
}
#selectionsContainer {
  width: 85%;
}
#catagoryContainer {
  width: 100%;
  height: calc(20% - 2px);
  border: 1px solid #f2f2f2;
}
#menuContainer {
  width: 100%;
  height: 80%;
  border-top: 0px;
  border: 1px solid #f2f2f2;
}
#itemContainer {
  width: 70%;
  height: 80%;
  border: 1px solid #f2f2f2;
  display: flex;
  order: 1;
}
#priceContainer {
  width: calc(30% - 2px);
  height: 80%;
  border: 1px solid #f2f2f2;
  display: flex;
  order: 2;
}
<div class="container">
  <div id="orderContainer">
    <div id="itemContainer">
      itemContainer
    </div>
    <div id="priceContainer">
      priceContainer
    </div>
  </div>

  <div id="selectionsContainer">
    <section id="catagoryContainer">
      catagoryContainer
    </section>
    <section id="menuContainer">
      menuContainer
    </section>
  </div>
</div>
like image 339
Brandon Korenek Avatar asked Jun 11 '16 22:06

Brandon Korenek


People also ask

How do you flex things on top of each other?

To display the items vertically, use flex-direction: column and the items will stack on top of one another in a column instead of side-by-side in a row.

How do you change the alignment of Flex items on the main axis in a Flex container?

If you are working with flex-direction set to row , this alignment will be in the inline direction. However, in Flexbox you can change the main axis by setting flex-direction to column . In this case, justify-content will align items in the block direction.

How do I align content in flexbox?

Main Axis Alignment With justify-content # We will start with the main axis alignment. On the main axis, we align using the justify-content property. This property deals with all of our flex items as a group, and controls how space is distributed between them. The initial value of justify-content is flex-start .


1 Answers

The only thing necessary to make two sibling elements show up next to each other is declare display: flex on the parent container.

You've applied display: flex to .container. This is not the parent container of #itemContainer and #priceContainer. You need to apply display: flex to #orderContainer.

A flex formatting context is limited in scope to a parent / child relationship. Descendants beyond the children will not accept flex properties from an ancestor.

You will always need to apply display: flex (or display: inline-flex) to parent elements in order for flex properties to work on the children (more details).


Once you've established the flex container, several default settings come into play. Here are two:

  • flex-direction: row - child elements (flex items) will align horizontally
  • flex-wrap: nowrap - flex items are forced to stay in a single line

Flexbox is a CSS3 technology. You don't need to add any library or other file to make it work. It runs like any other CSS. Just be aware of browser support.

like image 142
Michael Benjamin Avatar answered Jan 06 '23 12:01

Michael Benjamin