前言

上一篇介绍了各种语言的阅读方向. 这一篇来讲一下 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

padding-inline / padding-block 是 shorthand property.

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 年前的设备丫.

 Can i use...

像流行框架 Tailwind CSS 就没有build-in 支持, 只是有plugin而且.

还有 shorthand property 到现在草案都还不确定. 所以总结来看, 目前 2022 年用的话还有点早.

个人建议, 如果项目有 logical 需求, 肯定得用.

如果没有, 那么混用也是可以的, 就是写来 for future 或者美观而已. 但不要认为它支持 logical. 这样的 trade-off 也是 ok 的.

CSS – Logical Properties的更多相关文章

  1. 【CSS】357- 坚定地使用 CSS Custom Properties

    自定义属性(Custom Properties)是一个很有魅力的 CSS 新特性,现代浏览器广泛 支持.但是遇到那些不支持 CSS Custom Properties 的老掉牙浏览器我们该怎么办?等着 ...

  2. CSS 世界中的方位与顺序

    在 CSS 中,我们经常会与各种方向方位打交道. 譬如 margin.padding,它们就会有 margin-left.margin-right 或者是 padding-left.padding-r ...

  3. 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 ...

  4. 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 ...

  5. CSS 笔记一(Selectors/ Backgrounds/ Borders/ Margins/ Padding/ Height and Width)

    Selectors/ Backgrounds/ Borders/ Margins/ Padding/ Height and Width CSS Introduction: CSS stands for ...

  6. 【转】Styling And Animating SVGs With CSS

    原文转自:http://www.smashingmagazine.com/2014/11/03/styling-and-animating-svgs-with-css/?utm_source=CSS- ...

  7. CSS知识点-- Padding

    The CSS padding properties define the space between the element border and the element content. Padd ...

  8. CSS 声明( Declarations )

    CSS 声明1可以为空,或者由 CSS 特性( property ),后加一个冒号 ":",跟着是一个特性的值构成.中间可以有空格将它们隔开. 可用以下方式表达: property ...

  9. 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 ...

  10. CSS变量(自定义属性)实践指南

    本文翻译自:https://www.sitepoint.com/practical-guide-css-variables-custom-properties/ 转载请注明出处:葡萄城官网,葡萄城为开 ...

随机推荐

  1. django python 获取当天日期

    from datetime import date today = date.today() print(today) 在Python中,你可以使用datetime模块来获取当前日期.具体获取当前日期 ...

  2. [oeasy]python0116_文字的起源_苏美尔文明_楔形文字_两河流域

    文字起源 回忆上次内容 上次回顾了西里尔字符的编码过程 KOI-7 KOI-8   ISO-8859 系列进行总结 字符扩展 ascii 共 16 种 由iso组织制定 从 iso-8859-1 到 ...

  3. 从Mybatis-Plus开始认识SerializedLambda

    从Mybatis-Plus开始认识SerializedLambda 背景 对于使用过Mybatis-Plus的Java开发者来说,肯定对以下代码不陌生: @TableName("t_user ...

  4. OI-Wiki 学习笔记

    算法基础 \(\text{Update: 2024 - 07 - 22}\) 复杂度 定义 衡量一个算法的快慢,一定要考虑数据规模的大小. 一般来说,数据规模越大,算法的用时就越长. 而在算法竞赛中, ...

  5. 自己在本地搭建 git 版本仓库服务器

    请确保你安装了 git 的图形化工具和 git 软件 首先先创建一个目录作为你的项目工程目录,比如 e:/gitTest 其次右键 git init. 然后指定一个 git 服务器目录,例如:e:/g ...

  6. 数据结构:Deuque

    #include <iostream> #include <stdio.h> #include <string> using namespace std; stru ...

  7. 常用的adb命令(重要)

    常用的adb命令(重要)

  8. 腾讯云免费申请SSL证书配置https

    证书申请 1.进入腾讯云官网,在上方直接搜索SSL,搜索到后点击立即选购: 2.点击进去后选择自定义配置,加密标准选择默认的国际标准,证书种类选择域名免费版(DV),勾选同意服务条款后选择免费快速申请 ...

  9. 【Maven】概念知识

    怎么在Windows安装Maven https://www.cnblogs.com/mindzone/p/12701416.html 学到一个新的指令:查看相对系统变量 echo %XXX_HOME% ...

  10. 【SpringBoot】15 数据访问P3 整合Mybatis

    重新建立一个SpringBoot工程 选择依赖组件 然后删除不需要的Maven&Git文件 还是先查看我们的POM文件 整合Mybatis的组件多了这一个,默认的版本是3.5.4 然后再看看整 ...