The red boxes are the items, the grey background is the container:


I have 3 items that I want to display in a container. Please see the included image. What is the best way to go about this using flexbox? It should be the same on mobile view. Thanks

2 Answers

Community On Best Solutions

Use flexbox, with nth-child to change the specific heights of the flex objects.


<div class="container">
    <div class="flex"></div>
    <div class="flex"></div>
    <div class="flex"></div>

  background: #AAA;
  border: 1px solid black;
  display: flex;
  height: 15vw;
  width: 20vw;
  background: #F00;
  height: 7vw;
  margin-left: 1.25vw;
  width: 5vw;
  margin-top: 6vw;
  margin-top: 1vw;
  margin-top: 5vw;

See an example here:

Andu Andrici On

with flex you have the align-self property, which can be used to align an element on the cross-axis differently from the others, within the same flex parent.

.container { display: flex; }
.item--1 { align-self: flex-end; }
.item--2 { align-self: flex-start; }
.item--3 { align-self: center; }

I could almost bet my life that this is a 'homework' exercise, and you would do well to read up on flex and align-self to make the most of it.