html - How to center and corner flex items in flexbox? -
i trying learn flexbox having trouble getting want.
what want:
1 box have 2 labels (a number on top label)
second box have 4 labels (1 in top left, 1 in top right, 1 in center , 1 in bottom middle)
.flex-container { display: flex; background-color: lightgrey; flex-direction: row; align-items: stretch; align-content: stretch; } .flex-item { display: flex; background-color: cornflowerblue; margin: 10pt; } .flex-item-2 { display: flex; background-color: cornflowerblue; margin: 10pt; flex: 2 0 0; } .flex-qty-container { font-size: 27pt; margin: 0; } .flex-sub-container { display: flex; background-color: yellow; flex-direction: column; flex-wrap: wrap; align-items: flex-start; flex: 2 0 0; } .flex-item-left-corner { background-color: red; } .flex-item-right-corner { background-color: red; align-self: flex-end; font-size: 10pt; } .flex-item-center { background-color: red; font-size: 12pt; } .flex-item-bottom-middle { background-color: red; }
<div class="flex-container"> <div class="flex-item"> <div class=""> <p class="flex-qty-container">7</p> <p class="flex-qty-label">label</p> </div> </div> <div class="flex-item-2"> <div class="flex-sub-container"> <p class="flex-item-left-corner">top left corner</p> <p class="flex-item-right-corner">top right corner</p> <p class="flex-item-center">center of box</p> <p class="flex-item-bottom-middle">bottom middle</p> </div> </div> </div>
1 box have 2 labels (a number on top label)
this part seem have done already. didn't change there.
second box have 4 labels (1 in top left, 1 in top right, 1 in center , 1 in bottom middle)
this layout can achieved nested flex container.
in code, .flex-item-2
has 1 flex item: .flex-sub-container
. flex item doubles flex container , has 4 flex items (your labels).
<div class="flex-item-2"> <div class="flex-sub-container"> <p class="flex-item-left-corner">top left corner</p> <p class="flex-item-right-corner">top right corner</p> <p class="flex-item-center">center of box</p> <p class="flex-item-bottom-middle">bottom middle</p> </div> </div>
instead of having .flex-sub-container
wrap 4 labels, have wrap first two. apply justify-content: space-between
, top left , top right labels aligned intended.
<div class="flex-item-2"> <div class="flex-sub-container"> <p class="flex-item-left-corner">top left corner</p> <p class="flex-item-right-corner">top right corner</p> </div><!-- end .flex-sub-container --> <p class="flex-item-center">center of box</p> <p class="flex-item-bottom-middle">bottom middle</p> </div> .flex-item-2 { display: flex; flex-direction: column; } .flex-sub-container { display: flex; justify-content: space-between; }
with .flex-item-2
column
-direction flex container, cross axis horizontal , align-self
property can used center lower labels.
.flex-item-center { align-self: center; } .flex-item-bottom-middle { align-self: center; }
Comments
Post a Comment