Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox: Align between bottom and center? [duplicate]

Tags:

html

css

flexbox

Does someone know how to create this layout, using flexbox?

enter image description here

The text is going to be placed in the center, and the button is going to be placed between the text and bottom.

I have this now:

.aligner {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
  height: 100%;
}
<div class="aligner">
  <h3>SUPERMAN</h3>
  <p>FTW</p>
  <button>BUTTON</button>
</div>

This aligns everything in the center, but I want only the text to be in center, and the button between center and the bottom.

like image 989
allegutta Avatar asked Nov 26 '15 17:11

allegutta


People also ask

Can flexbox be nested?

Nested flex boxesIt's possible to create some pretty complex layouts with flexbox. It's perfectly OK to set a flex item to also be a flex container, so that its children are also laid out like flexible boxes.

How do I add space between inline Flex?

To set space between the flexbox you can use the flexbox property justify-content you can also visit all the property in that link. We can use the justify-content property of a flex container to set space between the flexbox.

Does flexbox work with position fixed?

This works because when you give an element a fixed position and a left and right of 0 or a top and bottom of 0, the element is stretched to fill the space from left to right, or top to bottom. That in turn allows a flex-box to use the amount of space you would expect without position fixed. Save this answer.


1 Answers

You can try this layout:

  • Anonymous element with flex: 1
  • The title and subtitle (titles)
  • Element with flex: 1 and display: flex.

The elements above and below the title will take available space left by titles in equal amounts. So the titles will become centered.

Those elements can also be flex containers, and you can align their contents inside them as desired.

html, body {height: 100% } * { margin: 0; }
.aligner, .below {
  display: flex;
  justify-content: center;
  flex-direction: column;
  align-items: center;
}
.aligner {
  height: 100%;
}
.aligner::before { content: ''; }
.aligner::before, .below {
  flex: 1;
}
<div class="aligner">
  <h3>SUPERMAN</h3>
  <p>FTW</p>
  <div class="below">
    <button>BUTTON</button>
  </div>
</div>
like image 68
Oriol Avatar answered Nov 15 '22 08:11

Oriol