CSS-文本属性详解

227 阅读2分钟

1. text-decoration

  • text-decoration用于设置文字的装饰线

    • decoration是装饰/装饰品的意思
  • text-decoration有如下常见取值

    • none:无任何装饰线
      • 可以去除a元素默认的下划线
    • underline:下划线
    • overline:上划线
    • line-through:中划线(删除线)
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <link rel="stylesheet" href="./css/reset.css">
      <style>
        .baidu {
          text-decoration: underline;
          cursor: pointer;
        }
    
        .google {
          text-decoration: line-through;
    
          /* 设置文本的颜色(前景色) */
          color: red;
        }
    
        .bing {
          text-decoration: overline;
        }
    
        a {
          text-decoration: none;
        }
      </style>
    </head>
    <body>
    
      <!-- a元素默认有添加text-decoration -->
      <a href="http://www.baidu.com">百度一下</a>
    
      <!-- span元素也添加装饰线 -->
      <span class="baidu">百度一下</span>
    
      <!-- 装饰线其他的值 -->
      <span class="google">Google一下</span>
      <span class="bing">必应一下</span>
    
      <a href="http://www.taobao.com">淘宝一下</a>
    
    </body>
    </html>
    
    • 效果:

image.png

  • a元素有下划线的本质是被添加了text-decoration属性

2. text-transform

  • text-transform用于设置文字的大小写转换

    • Transform单词是使变形/变换(形变);
  • text-transform有几个常见的值

    • capitalize:(使…首字母大写, 资本化的意思)将每个单词的首字符变为大写
    • uppercase:(大写字母)将每个单词的所有字符变为大写
    • lowercase:(小写字母)将每个单词的所有字符变为小写
    • none:没有任何影响
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>Document</title>
      <style>
        .cap {
          text-transform: capitalize;
        }
    
        .up {
          text-transform: uppercase;
        }
    
        .low {
          text-transform: lowercase;
        }
      </style>
    </head>
    <body>
    
      <div class="cap">A great man is always willing to be little</div>
      <div class="up">A great man is always willing to be little</div>
      <div class="low">A great man is always willing to be little</div>
    
    </body>
    </html>
    
    • 效果: image.png
  • 实际开发中用JavaScript代码转化的更多。

3. text-indent

  • text-indent用于设置第一行内容的缩进

    • text-indent: 2em;刚好是缩进2个文字
  • em 相对于父元素的字体的尺寸

  • 对行内非替换元素无效

    .container {
      width: 500px;
      height: 200px;
      background-color: #ff0;
    }
    
    .inline {
      background-color: #f90;
      text-indent: 2em;
    }
    
    .content {
      background-color: skyblue;
      text-indent: 2em;
    }
    
    <div class="container">
      <span class="inline">风雨过后,眼前会是鸥翔鱼游的天水一色。走出荆棘,前面就是铺满鲜花的康庄大道。登上山顶,脚下便是积翠如云的空蒙山色。在这个世界上,一星陨落,黯淡不了星空灿烂;一花凋零,荒芜不了整个春天。</span>
      <p class="content">风雨过后,眼前会是鸥翔鱼游的天水一色。走出荆棘,前面就是铺满鲜花的康庄大道。登上山顶,脚下便是积翠如云的空蒙山色。在这个世界上,一星陨落,黯淡不了星空灿烂;一花凋零,荒芜不了整个春天。</p>
    </div>
    

    text-indent.png

4. text-align (重要)

  • text-align: 设置文本的对齐方式

  • MDN: 定义 行内内容(例如文字) 如何相对它的块父元素对齐

  • W3C中的解释:

    如果内容没有完全填满,块的内联级内容如何沿着内联轴对齐

    • 理解:行内块元素能够利用该属性居中
  • 常用的值

    • left:左对齐
    • right:右对齐
    • center:正中间显示
    • justify:两端对齐
  • 案例

    • 文字的居中

    • 图片的居中(MDN)

      <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
          .box {
            background-color: skyblue;
            height: 300px;
      
            /* 让图片居中显示 */
            text-align: center;
          }
      
          img {
            width: 200px;
          }
        </style>
      </head>
      <body>
      
        <div class="box">
          <img src="../images/timg.jpg" alt="">
          <!-- 输入框也可以居中 -->
          <div>
            <input type="text">
          </div>
        </div>
      
      </body>
      </html>
      

    image.png

    • div元素的居中(W3C inline-level)

      <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
          .box {
            background-color: #f00;
            height: 300px;
      
            text-align: center;
          }
      
          .content {
            background-color: #0f0;
            height: 200px;
            width: 200px;
            
            /* 设置div为行内块元素 */
            /* display: inline-block; */
            
            /* 块级元素居中的方式 */
            margin: 0 auto;
          }
        </style>
      </head>
      <body>
      
        <div class="box">
          <div class="content"></div>
        </div>
      
      </body>
      </html>
      
      • 效果: image.png

    • justify的使用(了解)

      <!DOCTYPE html>
      <html lang="en">
      <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
          .box {
            width: 200px;
            background-color: #f00;
            color: white;
      
            /* 两端对齐,最后一行不生效 */
            text-align: justify;
            /* 最后一行需要单独设置 */
            text-align-last: justify;
          }
        </style>
      </head>
      <body>
      
        <div class="box">
          This CSS module defines properties for text manipulation and specifies their processing model. It covers line breaking, justification and alignment, white space handling, and text transformation. abc
        </div>
      
      </body>
      </html>
      
    • 效果:

    image.png

5. letter-word-spacing

  • letter-spacing、word-spacing分别用于设置字母、单词之间的间距

    letter-spacing: 10px;
    word-spacing: 30px;
    
    • 效果: image.png