Warning: mkdir(): No space left on device in /var/www/tgoop/post.php on line 37

Warning: file_put_contents(aCache/aDaily/post/cosine_front_end/--): Failed to open stream: No such file or directory in /var/www/tgoop/post.php on line 50
cosine - 前端人の日常频道@cosine_front_end P.2857
COSINE_FRONT_END Telegram 2857
#优质博文 #CSS #前端 #grid #layout #course
超级棒的教程。
The Fundamentals of CSS Alignment

AI 摘要:本文深入解析了 CSS 中的对齐 (Alignment) 机制,强调“内容(content)”与“项目(item)”两个层级对齐逻辑在 Grid、Flexbox、Block 以及定位元素中的差异。文章不仅讲述了基础属性(如 align-, justify-, place-* )的适用场景,还对 auto margin 、绝对定位、以及 safe alignment 等进阶知识点逐一拆解,并指出 对齐始终依赖于尺寸 (sizing) 与剩余空间 (free space)**。这是篇对 CSS 布局本质高度还原的长文,尤其适合想彻底理解 Alignment 背后的逻辑的开发者。

[以下是方便搜索索引的大纲(AI 生成),请读原文]
1. 对齐基础理论 (The Alignment Theory)
• CSS 中存在约 10 个对齐相关属性,常令初学者困惑。
• 对齐分为 content level 与 item level
• 常用属性类别:
• place-content(容器内容对齐)
• place-items(所有项目的对齐)
• place-self(单个项目的对齐)
• 属性均存在水平 (justify-*) 与垂直 (align-*) 两个维度。
• place-* 为两者速记写法,但常用分开书写。

2. Grid 布局对齐
Grid Anatomy:容器(container) → 格子单元(cells) → 项目(items)。
内容层级:对齐的是 grid cells 作为整体;项目层级:对齐的是 cell 内单个项目。
• start / end 使用逻辑值而非物理 left / right,支持不同语言排版。
Auto sizing:若设为 auto,grid cells 会填充或收缩,normal 默认值等于 stretch。
auto vs 1fr 区别
• auto 可收缩并依赖对齐调整。
• 1fr 吞下所有剩余空间,导致无法再进行 content-level 对齐。
跨单元格项目 (grid area):对齐是在 grid area 内进行,而非单一 cell。

3. Flexbox 布局对齐
Flexbox Anatomy:容器 → flex lines → flex items。
纵向轴 (cross axis)
• 内容层级对齐:flex lines 可整体对齐。
• 项目层级对齐:item 可在 flex line 内独立对齐 (align-self)。
横向轴 (main axis)
• 仅存在内容层级对齐 (justify-content),项目不可单独对齐 (justify-self 无效)。
flex-wrap
• nowrap 禁止纵向内容层对齐 (align-content 失效)。
• wrap 激活 content-level alignment 即便只有一行。
flex-direction:行列切换会翻转主轴与交叉轴,因此相同属性可作用于不同方向。
• **flex-grow 与对齐关系:填满剩余空间会禁用对应轴上的 content 对齐。

4. Block 容器与内联元素
• 在 Block container 中:
• 水平:item-level 对齐(justify-items, justify-self),没有内容层级对齐。
• 垂直:content-level 对齐 (align-content),没有 item-level。
• inline 元素的特殊情况:
• 单纯 inline → 水平继承 text-align,无需 flex/grid。
• 混合 inline 与 block → 浏览器生成“匿名块盒 (anonymous block box)”封装 inline 元素,但这些盒子不可直接对齐。

5. Auto Margins
• 对齐的是 margin box 而非元素本身。
• margin: auto 会将剩余自由空间转化为 margin,本质是“吞掉空间”的机制。
• 在 Flexbox 主轴下依旧生效,即便没有 item-level alignment。
• 注意:Block 垂直方向 auto margin 无效,需要定义 width 才能触发水平居中。
• 与 place-self: center 的区别:前者 margin box 吞满,后者保持元素周围留有自由空间。

6. 绝对定位元素 (Absolutely-positioned elements)
• 对齐以 containing block 作为参考区域(父级 padding box 或视口)。
• inset 定义内缩区域 (IMCB),对齐作用在该区域内。
• *-self 属性适用,但浏览器兼容性有限。
• 传统写法 ( left: 50%; top: 50%; transform: translate(-50%, -50%))依旧可用,但推荐 inset: 0; place-self: center 更直观。

7. Safe Alignment
• 当内容超出容器(负自由空间)时,可仍然对齐,但滚动条可能导致内容不可达。
• 使用 safe 修饰符(如 justify-self: safe center)可避免此问题。
• margin: auto 的对齐方式天然就是“safe”,因其不作用于负自由空间。

8. 总结
• Alignment 逻辑核心:永远依赖剩余空间和元素尺寸
• Grid、Flexbox、Block 各自有不同的默认行为和适用属性。
• 常见混淆点:
• auto vs normal vs stretch
• justify-items 和 justify-content 适用范围不同
• fr 与 flex-grow 会吞掉剩余空间,从而禁用对齐。


author Temani Afif



tgoop.com/cosine_front_end/2857
Create:
Last Update:

#优质博文 #CSS #前端 #grid #layout #course
超级棒的教程。
The Fundamentals of CSS Alignment

AI 摘要:本文深入解析了 CSS 中的对齐 (Alignment) 机制,强调“内容(content)”与“项目(item)”两个层级对齐逻辑在 Grid、Flexbox、Block 以及定位元素中的差异。文章不仅讲述了基础属性(如 align-, justify-, place-* )的适用场景,还对 auto margin 、绝对定位、以及 safe alignment 等进阶知识点逐一拆解,并指出 对齐始终依赖于尺寸 (sizing) 与剩余空间 (free space)**。这是篇对 CSS 布局本质高度还原的长文,尤其适合想彻底理解 Alignment 背后的逻辑的开发者。

[以下是方便搜索索引的大纲(AI 生成),请读原文]
1. 对齐基础理论 (The Alignment Theory)
• CSS 中存在约 10 个对齐相关属性,常令初学者困惑。
• 对齐分为 content level 与 item level
• 常用属性类别:
• place-content(容器内容对齐)
• place-items(所有项目的对齐)
• place-self(单个项目的对齐)
• 属性均存在水平 (justify-*) 与垂直 (align-*) 两个维度。
• place-* 为两者速记写法,但常用分开书写。

2. Grid 布局对齐
Grid Anatomy:容器(container) → 格子单元(cells) → 项目(items)。
内容层级:对齐的是 grid cells 作为整体;项目层级:对齐的是 cell 内单个项目。
• start / end 使用逻辑值而非物理 left / right,支持不同语言排版。
Auto sizing:若设为 auto,grid cells 会填充或收缩,normal 默认值等于 stretch。
auto vs 1fr 区别
• auto 可收缩并依赖对齐调整。
• 1fr 吞下所有剩余空间,导致无法再进行 content-level 对齐。
跨单元格项目 (grid area):对齐是在 grid area 内进行,而非单一 cell。

3. Flexbox 布局对齐
Flexbox Anatomy:容器 → flex lines → flex items。
纵向轴 (cross axis)
• 内容层级对齐:flex lines 可整体对齐。
• 项目层级对齐:item 可在 flex line 内独立对齐 (align-self)。
横向轴 (main axis)
• 仅存在内容层级对齐 (justify-content),项目不可单独对齐 (justify-self 无效)。
flex-wrap
• nowrap 禁止纵向内容层对齐 (align-content 失效)。
• wrap 激活 content-level alignment 即便只有一行。
flex-direction:行列切换会翻转主轴与交叉轴,因此相同属性可作用于不同方向。
• **flex-grow 与对齐关系:填满剩余空间会禁用对应轴上的 content 对齐。

4. Block 容器与内联元素
• 在 Block container 中:
• 水平:item-level 对齐(justify-items, justify-self),没有内容层级对齐。
• 垂直:content-level 对齐 (align-content),没有 item-level。
• inline 元素的特殊情况:
• 单纯 inline → 水平继承 text-align,无需 flex/grid。
• 混合 inline 与 block → 浏览器生成“匿名块盒 (anonymous block box)”封装 inline 元素,但这些盒子不可直接对齐。

5. Auto Margins
• 对齐的是 margin box 而非元素本身。
• margin: auto 会将剩余自由空间转化为 margin,本质是“吞掉空间”的机制。
• 在 Flexbox 主轴下依旧生效,即便没有 item-level alignment。
• 注意:Block 垂直方向 auto margin 无效,需要定义 width 才能触发水平居中。
• 与 place-self: center 的区别:前者 margin box 吞满,后者保持元素周围留有自由空间。

6. 绝对定位元素 (Absolutely-positioned elements)
• 对齐以 containing block 作为参考区域(父级 padding box 或视口)。
• inset 定义内缩区域 (IMCB),对齐作用在该区域内。
• *-self 属性适用,但浏览器兼容性有限。
• 传统写法 ( left: 50%; top: 50%; transform: translate(-50%, -50%))依旧可用,但推荐 inset: 0; place-self: center 更直观。

7. Safe Alignment
• 当内容超出容器(负自由空间)时,可仍然对齐,但滚动条可能导致内容不可达。
• 使用 safe 修饰符(如 justify-self: safe center)可避免此问题。
• margin: auto 的对齐方式天然就是“safe”,因其不作用于负自由空间。

8. 总结
• Alignment 逻辑核心:永远依赖剩余空间和元素尺寸
• Grid、Flexbox、Block 各自有不同的默认行为和适用属性。
• 常见混淆点:
• auto vs normal vs stretch
• justify-items 和 justify-content 适用范围不同
• fr 与 flex-grow 会吞掉剩余空间,从而禁用对齐。


author Temani Afif

BY cosine - 前端人の日常频道




Share with your friend now:
tgoop.com/cosine_front_end/2857

View MORE
Open in Telegram


Telegram News

Date: |

On June 7, Perekopsky met with Brazilian President Jair Bolsonaro, an avid user of the platform. According to the firm's VP, the main subject of the meeting was "freedom of expression." Don’t publish new content at nighttime. Since not all users disable notifications for the night, you risk inadvertently disturbing them. Content is editable within two days of publishing Users are more open to new information on workdays rather than weekends. How to create a business channel on Telegram? (Tutorial)
from us


Telegram cosine - 前端人の日常频道
FROM American