Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >CSS魔法堂:你真的懂text-align吗?

CSS魔法堂:你真的懂text-align吗?

作者头像
^_^肥仔John
发布于 2018-01-18 08:55:38
发布于 2018-01-18 08:55:38
1K00
代码可运行
举报
运行总次数:0
代码可运行

前言

也许提及text-align你会想起水平居中,但除了这个你对它还有多少了解呢?本篇打算和大家一起来跟text-align来一次负距离的交往,你准备好了吗?

text-align属性详解

The 'text-align' CSS property describes how inline content like text and inline-level element etc. is aligned in its parent block element.Does not control the alignment of block elements, only their inline content. 'text-align' Value: left | right | center | justify | justify-all | start | end | <string> | match-parent | inherit Initial: start, or a nameless value that acts as 'left' if 'direction' is 'ltr', 'right' if 'direction' is 'rtl' Applies to: block containers Inherited: yes Percentages: N/A Media: visual Computed value: the initial value or as specified, except for the match-parent value which is calculated against its parent's direction value and results in a computed value of either left or right

CSS2中的属性值: left: Align the text to the left side of the line box. right: Align the text to the right side of the line box. center: Align the text to the middle of the line box. justify: Align the text so that the first and last characters or inline-level element are aligned with the left and right line box edge. 示例:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<style type="text/css">
p{
  width: 200px;
  border: dotted 1px #000;
}
.ltr{direction:ltr;}
.rtl{direction:rtl;}
.left{text-align:left;}
.right{text-align:right;}
.center{text-align:center;}
.justify{text-align:justify;}
</style>
<p class="ltr">
direction left:As most young candidates for the pains and penalties
</p>
<p class="rtl">
direction right:As most young candidates for the pains and penalties
</p>
<p class="left">
left:As most young candidates for the pains and penalties
</p>
<p class="right">
right:As most young candidates for the pains and penalties
</p>
<p class="center">
center:As most young candidates for the pains and penalties
</p>
<p class="justify">
justify:As most young candidates for the pains and penalties
</p>

这里我们要注意一下,text-align所设置的是以inline-level box所在的line box作为参考系来进行水平排列对齐,而不是block container所生成的containing block,就更不是以viewport为参考系。CSSRec中写道

A block of text is a stack of line boxes. In the case of 'left', 'right' and 'center', this property specifies how the inline-level boxes within each line box align with respect to the line box's left and right sides; alignment is not with respect to the viewport.

另外对于属性值justify而言,CSS REC中特别说明

In the case of 'justify', this property specifies that the inline-level boxes are to be made flush with both sides of the line box if possible, by expanding or contracting the contents of inline boxes, else aligned as for the initial value. (See also 'letter-spacing' and 'word-spacing'.) If an element has a computed value for 'white-space' of 'pre' or 'pre-wrap', then neither the glyphs of that element's text content nor its white space may be altered for the purpose of justification.

在此我们先要打个岔,先理解letter-spacing,word-spacing,不然无法真正理解text-align:justify的原理。

字形、单词间的水平距离

letter-spacing干嘛了?

letter-spacing就是用于定义两个字形间的水平距离。

'letter-spacing' Value: normal | <length> | inherit Initial: normal Applies to: all elements Inherited: yes Percentages: N/A Media: visual Computed value: 'normal' or absolute length

normal:根据当前字体和字体大小,由UA自定义的水平距离。 <length>:可正可负,字形间最终的水平距离 = UA自定义的水平距离 + length值

word-spacing干嘛了?

word-spacing就是用于定义两个单词间的水平距离。

'word-spacing' Value: normal | <length> | inherit Initial: normal Applies to: all elements Inherited: yes Percentages: N/A Media: visual Computed value: for 'normal' the value '0'; otherwise the absolute length

normal:根据当前字体和字体大小,由UA自定义的水平距离。 <length>:可正可负,单词间最终的水平距离 = UA自定义的水平距离 + length值 注意:word-spacing作用于word-separator characters。而定义的word-separator characters有<SP>(普通空格符,U+0020)和<NBSP>(非断行空格符,U+00A0)两种。word-spacing所指定的距离会紧跟在word-separator characters后面。其他字符均不受word-spacing的影响。——也就是说word-spacing的单词间距只是我们日常当中的“单词间距”的子集而已。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<p>letter-spacing:normal; word-spacing:normal;</p>
<p style="letter-spacing:2px;word-spacing:normal;">letter-spacing:2px; word-spacing:normal;</p>
<p style="letter-spacing:-2px;word-spacing:normal;">letter-spacing:-2px; word-spacing:normal;</p>
<p style="letter-spacing:normal;word-spacing:10px;">letter-spacing:2px; word-spacing:10px;</p>
<p style="letter-spacing:normal;word-spacing:-10px;">letter-spacing:2px; word-spacing:-10px;</p>
<p style="word-spacing:1000px;">word-spacing(1000px)-doesn't-affect-other-characters</p>

深入text-align:justify

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<p style="width:150px;border:dotted 1px #000;word-wrap:break-word;text-align:auto;">
pure alphanumeric 123 432112 12313123124123
</p>
<p style="width:150px;border:dotted 1px #000;word-wrap:break-word;text-align:justify;">
pure alphanumeric 123 432112 12313123124123
</p>
<p style="width:170px;border:dotted 1px #000;word-wrap:break-word;letter-spacing:100px;">
本届集团公司党委由公司党委由
</p>
<p style="width:170px;border:dotted 1px #000;word-wrap:break-word;text-align:justify;">
本届集团公司党委由公司党委由
</p>
<p style="width:160px;border: dotted 1px #000;">
ภ า ษ า ไ ท ย ภ า ษ า ไ ท ย ภ า ษ า ไ ท ย
</p>
<p style="width:160px;border: dotted 1px #000;word-wrap:break-word;text-align:justify;">
ภ า ษ า ไ ท ย ภ า ษ า ไ ท ย ภ า ษ า ไ ท ย
</p>

a组均是text-align:start,而b组则是text-align:justify。可以看到text-align:justify有两个特点:

  1. 通过调整字符或单词间的距离,实现第一个/最后一个inline-level boxes与line box的左右边框对齐;
  2. 最后一个line box内的inline-level boxes采用的不是text-align:justify。而是采用text-align:start

 第1点说到"字符或单词间的距离",自然会联想到上面所说的letter-spacingword-spacing属性,但只要采用text-align:justify,那么上述两者之一的属性值将失效。若调整的是"字符间的距离"则letter-spacing失效,若调整的是"单词间的距离"则是word-spacing失效。  那问题来了,到底什么时候是"字符间的距离"什么时候是"单词间的距离"呢?  CSS3中引入了新属性text-justify:auto|none|inter-word|inter-ideograph|inter-cluster|distribute|kashida用于对text-align:justify具体的对齐算法作调整,虽然现在仅仅IE支持该属性,但不妨外我们通过它来了解具体的对齐算法规则。 inter-word: 采用增加/减少单词间的距离。chrome下英文、泰文等的默认对齐方式,IE5.5~9下CJK的默认对齐方式; inter-ideograph: 等同于inter-letter,采用增加/减少象形文字间的距离。chrome下CJK(中日韩文)等的默认对齐方式; 而text-align:justify的默认方式text-justify:auto: 则是对英文、泰文采用inter-word方式,对CJK采用inter-ideograph方式。 注意:具体对哪类语言采用哪种方式是由浏览器决定!  而第2点说到"最后一个line box内的inline-level box采用的是text-align:start",这个我们也可以参考CSS3的新属性text-align-last。当text-align:justifytext-align-last:auto时,最后一个line box或forced line break后的第一个line box均采用text-align:start。  这里由引入另一个问题了,什么是forced line break呢?首先我们来说说line break吧!line break其实就是原来位于一个line box的inline-level boxes,由于某些原因导致inline-level boxes分散到多个line boxes中,有甚者将某个inline-level box拆分为多个并分布到多个相邻的line box中。而line break中又分为forecd line break和soft wrap break两种。 forecd line break具体就是通过<br/>或block-level box实现。 soft wrap break具体是有line box空间不足所导致的line break。对于soft wrap break而言,还有一个概念是soft wrap opportunity(abbr. SWO),就是可拆分的点。这个涉及到white-spaceword-wrapword-break的具体属性值了。采用normal默认值时,对于英文、泰语、老挝语等以单词划分(word boundary)为SWO,而对于CJK则以音节划分(syllable boundary)为SWO,其实即是以字符划分作为SWO。  另外值得注意的是word boundary不包含标点符号,仅仅包含<SP>(普通空格符,U+0020)和<NBSP>(非断行空格符,U+00A0)两种。  而对于replaced element、display:inline-block等atomic inline-level element而言,它们和CJK一致的SWO。  还有一点是out-of-flow的元素不会引发line break的哦! 更多line break信息可参考line-breaking

CSS3中新增的属性值

justify-all: Same as justify, but also forces the last line to be justified. start: The same as left if direction of block container is left-to-right and right if that is right-to-left. end: The same as right if direction of block container is left-to-right and left if that is right-to-left. match-parent: This value behaves the same as inherit (computes to its parent’s computed value) except that an inherited start or end keyword is interpreted against its parent’s direction value and results in a computed value of either left or right.  理解了justify后自然就理解justify-all了,可惜现在还没有浏览器实现了justify-all属性值。那有没有办法pollyfill呢?必须有的,虽然实现起来有些蛋疼:(

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<style type="text/css">
 /* polyfill 4 justify-all*/
 .justify-all{
   text-align:justify;
   line-height:0;
 }
 .justify-all .content{
   line-height:normal;
 }
 .justify-all .polyfill{
   display:inline-block;
   *display:inline;
   zoom:1;

   width:100%;
   vertical-align:top;
 }
</style>
<p style="width:150px;border:dotted 1px blue;text-align:justify;">
  Hi there, welcome 2 fsjohnhuang's blog! Enjoy FE pls. cu.
</p>
<p style="width:150px;border:dotted 1px red;" class="justify-all">
  <span class="content">Hi there, welcome 2 fsjohnhuang's blog! Enjoy FE pls. cu.</span><i class="polyfill"></i>
</p>

具体原理请结合CSS魔法堂:深入理解line-height和vertical-align理解吧。

另外@张鑫旭老师还妙用text-align:justify了一回,

请看display:inline-block/text-align:justify下列表的两端对齐布局

总结

上述内容若有纰漏请各位指正,谢谢!

感谢

CSS text-align 属性 text-align CSS3 text-justify 属性 CSS Text Module Level 2 CSS Text Module Level 3 CSS direction属性简介与实际应用

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-03-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
全栈之前端 | 8.CSS3基础知识之文本样式学习
描述: 通过前面几章的学习,相信大家已经对CSS有了一个简单的了解吧,现在我们又回到使用频率较高的 文本 text、图像 images、表单 table 等元素CSS样式的设置,此章节主要讲解针对文本的相关CSS属性以其使用的示例演示。
全栈工程师修炼指南
2023/10/31
6740
全栈之前端 | 8.CSS3基础知识之文本样式学习
CSS魔法堂:重新认识Box Model、IFC、BFC和Collapsing margins
前言                                   盒子模型作为CSS基础中的基础,曾一度以为掌握了IE和W3C标准下的块级盒子模型即可,但近日在学习行级盒子模型时发现原来当初是
^_^肥仔John
2018/01/18
1.1K0
CSS魔法堂:重新认识Box Model、IFC、BFC和Collapsing margins
css的样式,选择器和框模型
派生选择器: li strong {color:red;} id 选择器: #red {color:red;} class选择器 .center {color:red;} 属性选择器 [title]{color:red;} 针对全部的带有title属性的元素 [title=W3School] {color:red;} 针对带有title属性且值为W3School的元素 [title~=hello] { color:red; } 针对带有title属性且值带有hello以并且由空格分隔的元素,如<h2 title="hello world">Hello world</h2> [title|=hello] { color:red; } 针对带有title属性且值带有hello以并且由连字符分隔的元素,如<h2 title="hello-world">Hello world</h2>
用户7962184
2020/11/20
1.6K0
css的样式,选择器和框模型
CSS魔法堂:说说Float那个被埋没的志向
前言  定位系统中第一难理解就是Normal flow,而第二就非Float莫属了,而Float难理解的原因有俩,1. 一开头我们就用错了;2. 它跟Normal flow靠得太近了。本文尝试理清Fl
^_^肥仔John
2018/01/18
8430
CSS魔法堂:说说Float那个被埋没的志向
CSS3魔法堂:说说Multi-column Layout
前言  是否记得《读者文摘》中那一篇篇优美感人的文章呢?那除了文章内容外,还记得那报刊、杂志独有的多栏布局吗?  当我们希望将报刊、杂志中的阅读体验迁移到网页上时,最简单直接的方式就是采用多栏布局来对内容排版,然而在过去我们仅能通过float+positioned来模拟多栏布局,而且效果不尽人意。而CSS3引入新的Multi-column Layout模型,从底层支持多栏布局。  文本作为学习笔记,以便日后查阅。 通栏布局与多栏布局  在深入Multi-column Layout模型之前,我们先看看疗效
^_^肥仔John
2018/01/18
9930
CSS3魔法堂:说说Multi-column Layout
CSS笔记
link rel=”stylesheet” href=”文件名” margin:0px auto;(块级元素居中)和width:的组合 写2个数:第一个代表上下,第二个代表左右 写3个数:第一个代表上,第二个代表左右,第三个下 写4个数:上,右,下,左 text-align:center;(行级元素居中) list-style:none (去除符号) clear:both(去除浮动) overflow:hidden;(超过大小就不显示) overflow:scroll 滚动条 border:none;outline:none;去除按钮边框
小城故事
2023/02/27
8590
49个常用的CSS代码片段,建议整理收藏
text-shadow 为网页字体添加阴影,通过对text-shadow属性设置相关的属性值。
前端达人
2021/07/16
2.3K0
css样式大全
字体属性:(font) 大小 {font-size: x-large;}(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX、PD 样式 {font-style: oblique;}(偏斜体) italic;(斜体) normal;(正常) 行高 {line-height: normal;}(正常) 单位:PX、PD、EM 粗细 {font-weight: bold;}(粗体) lighter;(细体) normal;(正常) 变体 {font-variant: small-c
程序员互动联盟
2018/03/16
4.8K0
第92天:CSS3中颜色和文本属性
rgba是代表Red(红色) Green(绿色) Blue(蓝色)和 Alpha透明度。虽然它有的时候被描述为一个颜色空间
半指温柔乐
2018/09/11
9320
css基本样式1(7.1)
2.盒模型 盒模型内容的width、height、padding内边距、border、margin外边距。
bamboo
2019/01/29
8870
css基本样式1(7.1)
CSS 换行_css不允许换行
4、最近遇到的一个需求,v-html渲染文本的时候要求,单行里面有数字的时候文字左右对齐,间距自动拉伸
全栈程序员站长
2022/11/17
4K0
Android框架-Google官方Gson解析
而 JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写,同时也易于机器解析和生成,广泛应用于各种数据的交互中,尤其是服务器与客户端的交互。
Android技术干货分享
2019/03/26
1.1K0
Android框架-Google官方Gson解析
CSS(二)
当使用多个值时,font-family 属性值字体列表定义浏览器应选择字体系列的优先级。 浏览器将在用户的计算机或者任何 @font-face 资源中查找每个字体。 所使用的字体优先级按从左到右的排序: 如果可用,它将使用第一个值,否则跳到下一个值,直到最后。默认字体系列由浏览器首选项定义。 在上面的示例中,浏览器将首先尝试使用 Source Sans Pro。如果不可用,它会尝试使用 Arial。如果也不可用,它将使用浏览器的 sans-serif 字体族中的可用字体。
1ess
2021/10/29
5440
CSS(二)
CSS样式更改——文本Content
上篇文章主要讲述了CSS样式更改中的背景Background,这篇文章我们来谈谈文本Content内容的基础用法。
前端皮皮
2020/11/26
1.9K0
bootstrap 中的 less
Bootstrap 的 CSS 文件是通过 Less 源码编译而来的。Less 是一门预处理语言,支持变量、mixin、函数等额外功能。
用户5760343
2019/07/05
9280
markdown样式代码保存
/*此样式是没任何效果的,留给你填写的 你可以随意修改,组合你想要的css样式 没有最好的,只有最合适的, 看看后面的示例,你就会懂得写个你最爱的样式,并保存了的! 建议先复制某一你喜欢的css模板样式到此样式下,再在此基础上自定义自己的css样式。 来试试吧! ^_^*/
xing.org1^
2020/02/14
7140
Zookeeper 安装
ZooKeeper服务器是用Java创建的,它在JVM上运行。你需要使用JDK 6或更高版本。
用户5760343
2019/10/25
5820
Etcd 服务发现
此外,discovery URL 应该仅仅用于集群的初始化启动。在集群已经运行之后修改集群成员,阅读 运行时重配置 指南。
用户5760343
2019/10/29
7440
CSS学习笔记一
CSS 选择器: CSS id选择器: id选择器可以为标有特定 id的HTML元素指定特定的样式 id选择器是以 “#” 来定义的 <!DOCTYPE html> <html> <head> <title>Node</title> <style type="text/css"> #red {color: red;} #green {color: green;} </style> </head> <body> <p id="red">id选择器:red -- 红色</p> <p
Mirror王宇阳
2020/11/13
3.6K0
[C++]C风格、C++风格和C++11特性的线程池
thread pool就是线程的一种使用模式,一个线程池中维护着多个线程等待接收管理者分配的可并发执行的任务。
TOMOCAT
2022/04/01
5660
[C++]C风格、C++风格和C++11特性的线程池
相关推荐
全栈之前端 | 8.CSS3基础知识之文本样式学习
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验