Intro
HTML Learning One - Basics:
https://blog.yexca.net/archives/146
HTML Learning Two - Lists, Tables, and Forms:
https://blog.yexca.net/archives/150
HTML Learning Three - Web Layout:
https://blog.yexca.net/archives/195
Standard flow, also known as document flow, refers to the default arrangement rules for tags on a page. For example, block elements take up a full line, while inline elements can display multiple per line.
If you don’t want to use standard flow for layout, you can use floats or Flexbox.
Floats
Purpose: To arrange block elements horizontally. The property is float, with two values: left and right.
Elements with float will be taken out of the standard document flow.
Clearing Float Effects
Because floated elements are removed from the standard flow, if a parent element has no explicit height, the floated children won’t expand it, potentially leading to layout issues.
Four common ways to clear floats:
Extra Element Method
Add a block-level element at the end of the parent, and set its CSS property clear:both.
| |
Single Pseudo-element Method
Same principle as above.
| |
Dual Pseudo-element Method
This method solves both parent collapse issues and float effects simultaneously.
| |
Using overflow
Add the CSS property overflow: hidden; to the parent element.
Flexbox
Flexbox, also known as flexible box layout, is a browser-recommended layout model. It’s great for structured layouts, offering powerful space distribution and alignment capabilities.
Plus, the Flex model doesn’t suffer from elements leaving the standard flow like floats do, making web layouts simpler and more flexible.
Components
Flexbox has four main components:
- Flex Container
- Flex Item
- Main Axis: Defaults to horizontal.
- Cross Axis: Defaults to vertical.
Set display: flex; on the parent element to make it a flex container. Its children then become flex items, which can automatically shrink or grow.
Flexbox Properties
| Description | Property |
|---|---|
| Main Axis Alignment | justify-content |
| Cross Axis Alignment | align-items |
| Specific Flex Item Cross Axis Alignment | align-self |
| Change Main Axis Direction | flex-direction |
| Flex Grow/Shrink | flex |
| Flex Item Wrapping | flex-wrap |
| Line Alignment | align-content |
Main Axis Alignment
Property: justify-content
| Value | Effect |
|---|---|
| flex-start | Default, flex items align from the start. |
| flex-end | Flex items align from the end. |
| center | Flex items are centered along the main axis. |
| space-between | Flex items are evenly distributed along the main axis, with space distributed between them. |
| space-around | Flex items are evenly distributed along the main axis, with space distributed around them. |
| space-evenly | Flex items are evenly distributed along the main axis, with equal space between items and the container edges. |
Cross Axis Alignment
Properties
align-items: Controls the cross-axis alignment for all flex items within the current flex container (set on the flex container).align-self: Controls the cross-axis alignment for a single flex item (set on the flex item).
| Value | Effect |
|---|---|
| stretch | Flex items are stretched to fill the container along the cross axis (default stretch if no specific cross-axis size is set on the item). |
| center | Flex items are centered along the cross axis. |
| flex-start | Flex items align from the start of the cross axis. |
| flex-end | Flex items align from the end of the cross axis. |
Changing Main Axis Direction
The main axis defaults to horizontal, the cross axis to vertical.
Property: flex-direction
| Value | Effect |
|---|---|
| row | Horizontal, left to right (default). |
| column | Vertical, top to bottom. |
| row-reverse | Horizontal, right to left. |
| column-reverse | Vertical, bottom to top. |
Flex Grow/Shrink Ratio
Purpose: Controls the size of a flex item along the main axis. Property: flex, added to the child (flex item).
Value: An integer, representing a proportion of the remaining space in the parent.
Flex Item Wrapping
Flex items can automatically shrink or grow. By default, all flex items stay on a single line. Property: flex-wrap, added to the parent (flex container).
Values are wrap (wrap to new lines) and nowrap (no wrapping, default).
Line Alignment
Property: align-content
Values are the same as justify-content (main axis alignment), and only apply to multi-line flex containers.