Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i keep centered div with max-width from collapsing when using flexbox column?

Tags:

html

css

flexbox

I have 3 divs vertically stacked in container. I want middle div to be centered but continue expanding horizontally as much as it can but less than max-width. Just like normal div would with max-width without flexbox involved.

https://codepen.io/anon/pen/XaQXOW

css

.cont {
  display: flex;
  flex-direction: column;
}

.b {
  max-width: 500px;
  background-color: cyan;
}
.a, .b, .c {
  border: 1px solid #000;
  height: 100px;
}

html

<div class="cont">
  <div class="a"></div>
  <div class="b"></div>
  <div class="c"></div>
</div>
like image 349
Muhammad Umer Avatar asked Sep 05 '17 07:09

Muhammad Umer


People also ask

Does flexbox work with position fixed?

position:fixed makes flexbox not workable, since it overrides the position of <p>Hello</p> Try to remove it and see the difference. If you want align-items:center works; try to set height: 100vh; since the body height of webview is not set to screen height by default.

Does CSS columns have effect on a Flex container?

display. This defines a flex container; inline or block depending on the given value. It enables a flex context for all its direct children. Note that CSS columns have no effect on a flex container.


1 Answers

Set align-self: center; to center your div and add width: 100%:

.cont {
  display: flex;
  flex-direction: column;
}

.b {
  max-width: 500px;
  background-color: cyan;
  align-self: center;
  width: 100%;
  box-sizing: border-box;
}

.a, .b, .c {
  border: 1px solid #000;
  height: 100px;
}
<div class="cont">
  <div class="a"></div>
  <div class="b"></div>
  <div class="c"></div>
</div>
like image 63
Huelfe Avatar answered Nov 15 '22 04:11

Huelfe