Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flex-direction column not working as expected [duplicate]

Tags:

html

css

flexbox

I have the following markup:

<div class="container">
  <div class="logo"></div>
  <div class="search"></div>
  <div class="button"></div>
</div>

CSS (desktop):

.container {
  display: flex;
}

On a desktop view they're displayed in a row, like that:

enter image description here

But on mobile view, i want them to re-reorder:

enter image description here

I've tried the following styles:

// here's media query for mobile devices
.container {
  flex-direction: column;
  flex-wrap: wrap;
  align-items: stretch // so my items would be filling width
}

.logo {
  order: 1;
  flex: 1 0 50%;
}

.search {
  order: 2;
  flex: 0 1 100%;
}

.button {
  order: 1;
  flex: 1 0 50%;
}

But my results are:

enter image description here

Is this even possible with a flexbox?

like image 531
Alexander Kim Avatar asked May 23 '19 20:05

Alexander Kim


People also ask

Why my flex direction column is not working?

You have only declared the flex-wrap component: wrap . This means that the flex-direction component remains the default value: row . Hence, your flex items are aligning horizontally in a row that wraps. Also important: If you want flex items to wrap in column-direction, you need to define a height on the container.

What is the default value for Flex direction?

The default value of flex-direction is row. It is used to specify that the item has normal text direction. It makes item to follow the normal text direction in lines.


2 Answers

You should look into "flex-grow" which allows flex items to grow if necessary in order to take up as much space as is available in its container. If all flex-items (in your case: .logo, .search, .button) have a flex-grow value of 1, the remaining space in .container will be distributed to its children equally.

Also, you should use

    flex-direction: row;

in your case if you want them to stretch horizontally

Check out this fiddle for reference! https://jsfiddle.net/hgs5w19y/2/

like image 63
dibby Avatar answered Oct 23 '22 12:10

dibby


You need to use flex-grow (great resource for understanding flexbox: https://css-tricks.com/snippets/css/a-guide-to-flexbox/)

HTML

<div class="container">
  <div class="logo">Logo</div>
  <div class="search">Search</div>
  <div class="button">Button</div>
</div>

CSS

.container {
  background-color: #ccc;
  display: flex;
  width: 300px;
  flex-wrap: wrap;
  text-align: center;
}

.container > div {
  background-color: grey;
  margin: 10px 20px;
  padding: 10px 20px;
}

.logo {
  order: 1;
  flex-grow: 1;
}

.search {
  order: 2;
  flex-grow: 2;
}

.button {
  order: 1;
  flex-grow: 1;
}
like image 35
DrCord Avatar answered Oct 23 '22 12:10

DrCord