Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 3, 2022 02:52 pm GMT

Simple, beginner steps: CSS Flexbox!

Let's talk through the basics of one of the most versatile modules in CSS -- the flexbox!

The flexbox has two elements: the parent element (the container that's 'flexing') and the child element (the flex items). It's amazingly versatile because it allows us to format, layout and organize even when the size of the space is unknown-- or it's dynamic!

Parent properties:

1. display This is the building block, that is going to define the flex container.

.container {  display: flex;}

2. flex-direction This is going to dictate which axis your flexbox parent expands on, and which direction on that axis.
row: left to right
row-reverse: right to left
column: north to south
column-reverse: south to north

.container {  flex-direction: row (OR) row-reverse (OR) column (OR) column-reverse;}

3. flex-wrap Flexbox will always try to fit items into one line-- this property can allow it to wrap to a new line.
nowrap: all on one line
wrap: wrap onto multiple lines (top to bottom)
wrap-reverse: multiple lines (bottom to top)

.container {  flex-wrap: nowrap (OR) wrap (OR) wrap-reverse;}

4. justify-content This property defines the alignment on the axis declared originally for the flex.
flex-start: items are packed toward the start of the flex-direction
flex-end: packed toward end of flex direction
start: packed toward start of writing-mode direction
end: packed toward end of writing-mode direction
left: left packed toward left edge of container
right: left packed toward right edge of container
center: centered
space-between: items are evenly spaced on line
space-around: items are evenly spaced, space around them is also even
space-evenly: spacing between items and edges is even

.container {  justify-content: flex-start (OR) flex-end (OR) center (OR) space-between (OR) space-around (OR) space-evenly (OR) start (OR) end (OR) left (OR) right (OR) you get the idea;}

5. align-items This defines how the items are laid out on the crossing axis of the current line.

stretch: (default) stretches to fill container
flex-start: start of the cross axis
flex-end: end of the cross axis
center: items are centered on cross axis
baseline: items are aligned so their baselines are aligned

For this one, imagine your flex goes L-->R. Flex-start would mean all items align at the top (North). Flex-end would align them at the bottom (South). Center would center them along the center of north-south, centered by the item's center. Stretch would make them fill equal North-South space from the center. Baseline will center them North-South by baseline.

.container { align-items: (value here)}

6. align-content This aligns the lines of a flex container within the extra space on the cross axis. This property only effects multi-lines flex containers.

normal: (default)
flex-start: items packed to start of container
flex-end: items packed to end of container
center: items centered in container
space-between: items evenly distributed from start to end of container
space-around: items evenly distributed with equal space around each line
space-evenly: items are evenly distributed with equal space around them
stretch: lines stretch to take up space

.container {  align-content: (value here)}

7. gaps The gap property handles the space between flex items. It only applies between items, and doesn't affect edges.

you can declare:
gap: 20px (default)
gap: 20px 20px (row-gap then column-gap)
row-gap: 20px
column-gap: 20px

.container {  display: flex;  ...  gap: 10px;  gap: 10px 20px;  row-gap: 10px;  column-gap: 20px;}

Child properties:

1. order This can control the order items appear in the flex container. If items have the same order, they default to their source order.

.item {  order: 5;}

2. flex-grow This defines the flex item's ability to grow if necessary. The value is a proportion.

For example, if all items had a value of 1, the space would be distributed equally between the children. If one item had a 2, it would take up twice the allotted space of all other items.

.item {  flex-grow: 4;}

3. flex-shrink This is the opposite of grow, and allows items to shrink if necessary

.item {  flex-shrink: 3;}

3. align-self This allows the default alignment (the one in align-items in the parent) to be overridden in a specific item

.item {  align-self: auto (OR) flex-start (OR) flex-end (OR) center (OR) baseline (OR) stretch;}

It can take awhile to get a hang of the flexbox, but it's worth it!

Check out this amazing visual guide to CSS flexbox here!


Original Link: https://dev.to/aprilskrine/simple-beginner-steps-css-flexbox-54b8

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To