前端学习笔记之——设置文本样式

设置文本样式

1、应用基本文本样式

1.1、对齐文本

有好几个属性可以用来设计文本内容的对齐方式。

属性说明
text-align指定文本块的对齐方式start
end
left
right
center
justify
text-justify如果 text-justify 属性使用了 justify 值,则该值会指定对齐问文本的规则

text-align 属性相当简单,不过,需要注意的一点是:可以将文本对齐到显式命名的某个边界(使用 left 或者 right 值),或者对齐到语言本来使用的边界(使用 start 和 end 值)。在处理从右到左的语言时,这是一个非常重要的区别。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            #fruittext {
                width: 400px;
                margin: 5px;
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
            }            
        </style>
    </head>
    <body>
        <p id="fruittext">
            There are lots of different kinds of fruit - there are over 500
            varieties of banana alone. By the time we add the countless types of
            apples, oranges, and other well-known fruit, we are faced with
            thousands of choices.
            One of the most interesting aspects of fruit is the
            variety available in each country. I live near London, in an area which is
            known for its apples.
        </p>
        <p>        
            <button>Start</button>
            <button>End</button>
            <button>Left</button>
            <button>Right</button>
            <button>Justify</button>
            <button>Center</button>
        </p>
        <script>
            var buttons = document.getElementsByTagName("BUTTON");
            for (var i = 0; i < buttons.length; i++) {
                buttons[i].onclick = function(e) {
                    document.getElementById("fruittext").style.textAlign =
                        e.target.innerHTML;
                };
            }
        </script>
    </body>
</html>

在这里插入图片描述

如果使用 justify 值,可以使用 text-justify 属性指定文本添加空白的方式。

说明
auto浏览器选择对齐方式,这是最简单的方法,不过,不同浏览器之间的呈现方式会有微小差别
none禁用文本对齐
inter-word空白分布在单词之间,适用于英语等词间有空的语言
inter-ideograph空白分布在单词、表意字之间,且文本两端对齐,适用于汉语、日文哈韩文等语言
inter-cluter空白分布在单词、字形集的边界,适用于泰文等无词间空格的语言
distribute空白分布在单词、字形集的边界,但连续文本或者草体除外
kashida通过拉长选定字符调整对齐方式

1.2、处理空白

空白在 HTML 文档中通常是被压缩或者直接忽略掉。这允许你将 HTML 文档的布局跟页面的外观分离。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            #fruittext {
                width: 400px;
                margin: 5px;
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
            }
        </style>
    </head>
    <body>
        <p id="fruittext">
            There are lots of different kinds of fruit - there are over 500
            varieties
            
            of banana alone. By the time we add the countless types of
            apples, oranges, and other well-known fruit, we are faced with
            thousands of choices.
            
            One     of the      most interesting aspects of fruit is the
            variety available   in each country. I live near London,
            
            in an area which is
            known for its apples.
            
        </p>
    </body>
</html>

上面代码中,文本包含了一些空格、制表符和换行符。浏览器遇到多个空格时,会将它们压缩为一个空格,而换行符等其他空白符则会直接被忽略。浏览器会自动处理文本换行,以便各行都能适应元素边界。

但有时候我们就想在 HTML 原文本中保留文本中的空白。这时,可以使用 whitespace 属性控制浏览器对空白字符的处理方式。

在这里插入图片描述

属性说明
whitespace指定空白字符的处理方式参考下表
说明
normal默认值,空白字符被压缩,文本自动换行
nowrap空白符被压缩,文本行不换行
pre空白符被保留,文本只在遇到换行符时候换行,这跟 pre 的效果一样
pre-line空白符被压缩,文本会在一行排满或遇到换行符时换行
pre-wrap空白符被保留,文本会在一行排满或遇到换行符时换行
<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            #fruittext {
                width: 400px;
                margin: 5px;
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
                white-space: pre-line;
            }
        </style>
    </head>
    <body>
        <p id="fruittext">
            There are lots of different kinds of fruit - there are over 500
            varieties
            
            of banana alone. By the time we add the countless types of
            apples, oranges, and other well-known fruit, we are faced with
            thousands of choices.
            
            One     of the      most interesting aspects of fruit is the
            variety available   in each country. I live near London,
            
            in an area which is
            known for its apples.
            
        </p>
    </body>
</html>

在这里插入图片描述

1.3、指定文本方向

direction 属性告诉浏览器文本块的排列方向。

属性说明
direction设置文本方向ltr
rtl
<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            #first {
                direction: ltr;
            }
        
            #second {
                direction: rtl;
            }
        </style>
    </head>
    <body>
        <p id="first">
            This is left-to-right text
        </p>
        <p id="second">
            This is right-to-lefttext
        </p>
    </body>
</html>

在这里插入图片描述

1.4、指定单词、字母、行之间的间距

可以告诉浏览器单词与单词、字母与字母、行与行之间的间距。

属性说明
letter-spacing设置字母之间的间距normal
<长度值>
word-spacing设置单词之间的间距Normal
<长度值>
line-height设置行高Normal
<数值>
<长度值>
<%>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>        
            #fruittext {
                margin: 5px;
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
                word-spacing: 10px;
                letter-spacing: 2px;
                line-height: 3em;
            }
        </style>
    </head>
    <body>
        <p id="fruittext">
            There are lots of different kinds of fruit - there are over 500
            varieties of banana alone. By the time we add the countless types of
            apples, oranges, and other well-known fruit, we are faced with
            thousands of choices.
        </p>
    </body>
</html>

在这里插入图片描述

1.5、控制断词

word-wrap 属性告诉浏览器当一个单词的长度超过包含块的宽度时如何处理。

说明
normal单词不断开,即使无法完全放入包含块元素
break-word断开单词,使其放入包含块元素
<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>        
            p {
                width:150px;
                margin: 15px;
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
                float:left;
            }
            
            #first {
                word-wrap: break-word;
            }
            
            #second {
                word-wrap: normal;
            }
        </style>
    </head>
    <body>
        <p id="first">
            There are lots of different kinds of fruit - there are over 500 
            varieties of madeupfruitwithaverylongname alone.
        </p>
        <p id="second">
            There are lots of different kinds of fruit - there are over 500 
            varieties of madeupfruitwithaverylongname alone.
        </p>
    </body>
</html>

在这里插入图片描述

1.6、首行缩进

text-indent 属性用于指定文本块首行缩进,值可以是长度值,也可以相对于元素宽度的百分数值。

属性说明
text-indent设置文本首行缩进<长度值>
<%>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>        
            p {
                margin: 15px;
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
                float:left;
                text-indent: 15%;
            }
        </style>
    </head>
    <body>
        <p>
            There are lots of different kinds of fruit - there are over 500
            varieties of banana alone. By the time we add the countless types of
            apples, oranges, and other well-known fruit, we are faced with
            thousands of choices.
            One of the most interesting aspects of fruit is the
            variety available in each country. I live near London, in an area which is
            known for its apples.
        </p>
    </body>
</html>

在这里插入图片描述


2、文本装饰与大小写转换

text-decoration 和 text-transform 两个属性分别允许我们装饰文本和转换文本大小写。

属性说明
text-decoration为文本块应用装饰效果none
underline
overline
line-through
blink
text-transform为文本块转换大小写none
capitalize
uppercase
lowercase
<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            p {
                border: medium double black;
                background-color: lightgrey;
                text-decoration: line-through;
                text-transform: uppercase;
            }
        </style>
    </head>
    <body>
        <p>
            There are lots of different kinds of fruit - there are over 500
            varieties of banana alone. By the time we add the countless types of
            apples, oranges, and other well-known fruit, we are faced with
            thousands of choices.
            One of the most interesting aspects of fruit is the
            variety available in each country. I live near London, in an area which is
            known for its apples.
        </p>
    </body>
</html>

在这里插入图片描述


3、创建文本阴影

属性说明
text-shadow为文本块应用阴影<h-shadow> <v-shadow> <blur> <color>

blur 值定义了阴影的模糊程度。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>
            h1 {
                text-shadow: 0.1em .1em 1px lightgrey;
            }
            p {
                text-shadow: 5px 5px 20px black;
            }
        </style>
    </head>
    <body>
        <h1>Thoughts about Fruit</h1>
        <p>
            There are lots of different kinds of fruit - there are over 500
            varieties of banana alone. By the time we add the countless types of
            apples, oranges, and other well-known fruit, we are faced with
            thousands of choices.
        </p>
    </body>
</html>

在这里插入图片描述


4、使用字体

属性说明
font-family指定文本块采用的字体名称参见下表
font-size指定文本块的字体大小参见下表
font-style指定字体样式Normal
italic
oblique
font-variant指定字体是否以小型大写字母显示Normal
smallcaps
font-weight设置字体粗细Normal
bold
bolder
lighter
font在一条声明中设置字体的简写属性

font 属性值的格式如下:

font: <font-style> <font-variant> <font-weight> <font-size> <font-family>

4.1、选择字体

font-family 属性指定使用的字体,按照优先顺序排列。浏览器从字体列表中的第一种开始尝试,直到发现合适的字体为止。

通用字体系列实现字体系列
serifTimes
sans-serifHelvetica
cursiveZapf-Chancery
fantasyWestern
monospaceCourier
<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>        
            p {
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
                margin: 2px;
                float: left;
                font-family: "HelveticaNeue Condensed", monospace;
            }
        </style>
    </head>
    <body>
        <p>
            There are lots of different kinds of fruit - there are over 500
            varieties of banana alone. By the time we add the countless types of
            apples, oranges, and other well-known fruit, we are faced with
            thousands of choices.
        </p>
    </body>
</html>

在这里插入图片描述

4.2、设置字体大小

font-size 属性用来指定字体大小。

说明
xx-small
x-small
small
medium
large
x-large
xx-large
设置字体大小。浏览器会决定每个值代表具体大小。
smaller
larger
设置字体相对于父元素字体的大小
<length>使用 CSS 长度值精确设置字体大小
<%>将字体大小表示为父元素字体大小的百分数
<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>        
            p {
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
                margin: 2px;
                float: left;
                font-family: sans-serif;
                font-size: medium;
            }
            #first {
                font-size: xx-large;
            }
            #second {
                font-size: larger;
            }
        </style>
    </head>
    <body>
        <p>
            There are lots of different kinds of fruit - there are over 500
            varieties of <span id="first">banana</span> alone. By the time we add the
            countless types of <span id="second">apples, oranges, and other
            well-known fruit, we are faced with thousands of choices</span>.
        </p>
    </body>
</html>

在这里插入图片描述

4.3、设置字体样式和粗细

可以使用 font-style 属性设置字体粗细。font-style 属性允许我们在正常字体、斜体和假斜体(倾斜字体)三种之间选择。

<!DOCTYPE HTML>
<html>
    <head>
        <title>Example</title>
        <meta name="author" content="Adam Freeman"/>
        <meta name="description" content="A simple example"/>
        <link rel="shortcut icon" href="favicon.ico" type="image/x-icon" />
        <style>        
            p {
                padding: 5px;
                border: medium double black;
                background-color: lightgrey;
                margin: 2px;
                float: left;
                font-family: sans-serif;
                font-size: medium;
            }
            #first {
                font-weight: bold;
            }
            #second {
                font-style: italic;
            }
        </style>
    </head>
    <body>
        <p>
            There are lots of different kinds of fruit - there are over 500
            varieties of <span id="first">banana</span> alone. By the time we add the
            countless types of <span id="second">apples, oranges, and other
            well-known fruit, we are faced with thousands of choices</span>.
        </p>
    </body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值