辋川集

木末芙蓉花,山中发红萼,涧户寂无人,纷纷开且落

By - 陳 思敬

CSS text-indent 略谈

之前我在排版电子书的时候,已经多次使用 text-indent,对中文排版特别实用。今日修正邮件签名,发现 text-indent 对 span 可能无效,一番网络搜索,最终解决。
text-indent
Sets or retrieves the indentation of the first line of text in the object.

Syntax
HTML { text-indent : sIndent }
Scripting object.style.textIndent [ = sIndent ]
Remarks
The property can be negative. An indent is not inserted in the middle of an object that was broken by another object, such as br in HTML.

注意:

  1. 只对块级元素有效,如果是 span 等行级元素,可加 display: block
  2. 允许使用负值。如果使用负值,那么首行会被缩进到左边
  3. 常用的缩进值:25px,2em,在邮件签名中有中文冒号,则使用 3em

示例:

<style type="text/css">
    .qt{ text-indent:35px}
</style>
<div class="qt">
<p>第一段开始缩进效果<br />
使用br标签的换行将不会缩进
</p>
<p>第二段使用p标签段落首行也会缩进<br />
第二行使用了br不会缩进<br />
第三行提行使用了br也不会缩进</p>
</div>

JavaScript 语法:

document.getElementById("div1").style.textIndent="50px";

Leave a Reply

Your email address will not be published.
*
*