CSS – Logical Properties
前言
续上一篇介绍了各种语言的阅读方向. 这一篇来讲一下 Logical Properties. 它与 left to right, right to left, horizontal, vertical 息息相关.
参考:
Youtube – Write better CSS using logical properties
CSS-Tricks – CSS Logical Properties
The Problem
p {
padding-left: 30px;
writing-mode: horizontal-tb;
direction: ltr;
/* unicode-bidi: bidi-override; */
}
加上一个 padding-left: 30px
效果

现在把 direction 换成 rtl
p {
direction: rtl;
}
效果

padding-left 依然是左边. 但绝大部分情况下, 我们会希望它在右边.
这就类似使用 Flex 的 align-items 那样, 它的值是 flex-start, flex-end, 而不是 left, right. 因为它会依据 flex-direction 而变化.
flex-direction: row 时, flex-start 指的是 top
flex-direction: column 时, flex-start 指的是 left
很多地方都可以看到 start / end 取代 left / right 的影子. Angular Material 前几年也是一点一点换掉 API 接口的.
Logical Properties – inline / block - start / end
为了解决问题, 就有了所谓的 Logical Properties
我们依然用回 padding 的例子.
padding-left 对应 padding-inline-start
padding-right 对应 padding-inline-end
padding-top 对应 padding-block-start
padding-bottom 对应 padding-block-end
start / end 比较好理解, 毕竟 Flex 也是 start / end.
inline / block 呢?

我是这样记住它的, 上面是 inline 的效果, 东西是横向摆放一排的. 如果用手比划, 那么 2 只手是左右来形容一个 box, 那么就是 left right 咯.
反之形容 block 的 box, 2 只手是上下的, 因为 block 的摆放是往下一个一个 row 的.

所以就对应了, inline = left / right, block = top / bottom.
padding / border / margin / width / height / position
但凡之前有 "方向" 概念的都可以替换成 Logical Properties.
div {
padding-inline: 1rem; /* padding-left/right */
padding-block: 1rem; /* padding-top/bottom */
border-inline: 1rem;
border-block: 1rem;
margin-inline-start: 1rem;
margin-block-end: 1rem;
inline-size: 100px; /* width */
max-inline-size: 100px; /* max-width */
block-size: 100px; /* height */
inset-inline-start: 1rem; /* left (for position) */
inset-block-start: 1rem; /* top (for position) */
}
除了 padding, margin, border, 还有 width, height, position 的 top, right, bottom, left 都算有 "方向" 的概念哦.
window.getComputedStyle 获取的值
.btn {
padding-top: 1rem;
padding-bottom: 1.5rem;
}
JavaScript
const btn = document.querySelector('.btn');
console.log('block', window.getComputedStyle(btn).paddingBlock); // "16px 24px"
1rem 变成了 16px.
虽然 CSS 写的是 padding-top 和 padding-bottom, 而 JavaScript 拿的是 paddingBlock 但它还是可以拿到的哦,
如果 top, bottom 值一样, 那么 paddingBlock 就只会一个值.
关于 shorthand property 和 inset
padding-inline = padding-inline-start padding-inline-end
padding-block = padding-block-start padding-block-end
padding = top/bottom right/left 这可不是 logical 的哦, 它叫 physical (logical 的反义词是 physical), logical 没有一次性写完的 shorthand. 参考: This issue.
另外, inset 是 position top, right, bottom, left 的 shorthand property. 它和 margin 是等价的. 它也不是 logical 的.
inset-inline = inset-inline-start inset-inline-end (这个就是 logical)

要不要用呢?
参考: Medium – Why CSS Logical Properties Aren’t Ready for Use!
至于要不要用呢? 其实绝大部分项目是没有需求的.
完全支持也是不久前的事情. 但是网站不可能只支持 model browser 吧. 至少也要顾虑到 3-5 年前的设备丫.
像流行框架 Tailwind CSS 就没有build-in 支持, 只是有plugin而且.
还有 shorthand property 到现在草案都还不确定. 所以总结来看, 目前 2022 年用的话还有点早.
个人建议, 如果项目有 logical 需求, 肯定得用.
如果没有, 那么混用也是可以的, 就是写来 for future 或者美观而已. 但不要认为它支持 logical. 这样的 trade-off 也是 ok 的.
CSS – Logical Properties的更多相关文章
- 【CSS】357- 坚定地使用 CSS Custom Properties
自定义属性(Custom Properties)是一个很有魅力的 CSS 新特性,现代浏览器广泛 支持.但是遇到那些不支持 CSS Custom Properties 的老掉牙浏览器我们该怎么办?等着 ...
- CSS 世界中的方位与顺序
在 CSS 中,我们经常会与各种方向方位打交道. 譬如 margin.padding,它们就会有 margin-left.margin-right 或者是 padding-left.padding-r ...
- Change :hover CSS properties with JavaScript
I need to find a way to change CSS :hover properties using JavaScript. For example, suppose I have t ...
- CSS 笔记二(Text/Fonts/Links/Lists)
CSS Text 1> Text Color used to set the color of the text 2> Text Alignment used to set the hor ...
- CSS 笔记一(Selectors/ Backgrounds/ Borders/ Margins/ Padding/ Height and Width)
Selectors/ Backgrounds/ Borders/ Margins/ Padding/ Height and Width CSS Introduction: CSS stands for ...
- 【转】Styling And Animating SVGs With CSS
原文转自:http://www.smashingmagazine.com/2014/11/03/styling-and-animating-svgs-with-css/?utm_source=CSS- ...
- CSS知识点-- Padding
The CSS padding properties define the space between the element border and the element content. Padd ...
- CSS 声明( Declarations )
CSS 声明1可以为空,或者由 CSS 特性( property ),后加一个冒号 ":",跟着是一个特性的值构成.中间可以有空格将它们隔开. 可用以下方式表达: property ...
- 3 Ways to Preload Images with CSS, JavaScript, or Ajax---reference
Preloading images is a great way to improve the user experience. When images are preloaded in the br ...
- CSS变量(自定义属性)实践指南
本文翻译自:https://www.sitepoint.com/practical-guide-css-variables-custom-properties/ 转载请注明出处:葡萄城官网,葡萄城为开 ...
随机推荐
- 「Pygors跨平台GUI」1:Pygors跨平台GUI应用研究
「Pygors系列」一句话导读: Python.Go.Rust.C程序跨平台GUI框架研究. 一.问题 Pygors是什么? Pygors是我自己创造的一个词,就是Python.Go.Rust.C四种 ...
- 最简GIF解析代码gif_jumper,用于stb_image的小改进
gif jumper gif支持多帧动画,但是没有存储总帧数,解析gif直到结束才能知道总帧数. 所以gif解析代码,要么采用链表,要么不停realloc()分配内存,stb_image的代码就是如此 ...
- 玄机-第一章 应急响应- Linux入侵排查
目录 前言 简介 应急开始 准备工作 步骤 1 步骤 2 步骤 3 步骤 4 步骤5 总结 前言 作者这一次也是差一点一次过,因为没有经验的原因,或者说题目对问题描述不太对,如果说是求黑客反连的ip的 ...
- Bootstrip HTML 查询搜索常用格式模版
Bootstrip HTML 查询搜索常用格式模版 <form class="form-inline my-3 d-flex align-items-center justify-co ...
- 全网最适合入门的面向对象编程教程:20 类和对象的 Python 实现-组合关系的实现与 CSV 文件保存
全网最适合入门的面向对象编程教程:20 类和对象的 Python 实现-组合关系的实现与 CSV 文件保存 摘要: 本文主要介绍了在使用 Python 面向对象编程时,如何实现组合关系,同时对比了组合 ...
- Django model层之执行原始SQL查询
Django model层之执行原始SQL查询 by:授客 QQ:1033553122 测试环境 Win7 Django 1.11 执行原始SQL查询 Manager.raw(raw_query, ...
- 英伟达又向开源迈了一步「GitHub 热点速览」
大家是否还记得 2012 年,Linux 之父 Linus Torvalds 在一次活动中"愤怒"地表达了对英伟达闭源 Linux GPU 驱动的不满?这个场景曾是热门表情包,程序 ...
- 打开电脑属性 设置 windows
组合键:win+R,输入sysdm.cpl,然后运行. 右键"此电脑",选择属性,然后点击高级系统设置. 组合键:win+Pause/Break. 在命令提示符中输入SystemP ...
- python os.path 模块详解
python os.path 模块详解 os.path.basename() 返回最后一项,通常是文件名os.path.dirname() 返回的是目录,不包含文件名os.path.split() 返 ...
- 【Kafka】01 基于Docker环境的单例Kafka搭建
安装参考: https://www.cnblogs.com/vipsoft/p/13233045.html 环境安装需要 Zookeeper + Kafka 要学习Kafka还需要繁琐的安装配置,所以 ...