字体的分类:
使用方法:
<body>
<div id="wrap">
<p class="paragrahp" style="font-family:'Microsoft yahei','宋体';">此处使用宋体书写文字大小p>
div>
body>
单位:px |%|em|rem
注意:
使用方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>字体大小title>
<style type="text/css">
html {
font-size: 100px;
}
#wrap {
font-size: 20px;
}
.paragrahp1 {
/* 相对于父容器中字体%调整,此处是 id为wrap的div容器 */
font-size: 30px;
}
.paragrahp2 {
font-size: 10%;
}
.paragrahp3 {
/* 相对于父级字体大小而言,此处是 id为wrap的div容器 */
font-size: 2em;
}
.paragrahp4 {
/* 相对于根节点(html)字体大小,默认是16px */
font-size: 1rem;
}
style>
head>
<body>
<div id="wrap">
<p class="paragrahp1">aaaap>
<p class="paragrahp2">bbbbp>
<p class="paragrahp3">ccccp>
<p class="paragrahp4">ddddp>
div>
body>
html>
补充: 不同浏览器的默认字体大小不一样,Chrome、Safar默认限制大小为12px,当字体小于12px的时候,浏览器会默认设置为12px。
上述代码用谷歌浏览器实验如下:
解决方法:
.paragrahp1 {
/*在这里,字体大小并没有缩小,缩小的是这段文字的容器*/
font-size: 12px;
transform: scale(0.1);
transform-origin: left;
}
使用方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>字体粗细title>
<style type="text/css">
#wrap {
font-size: 20px;
}
.paragrahp1 {
font-weight: normal;
}
.paragrahp2 {
font-weight: bold;
}
.paragrahp3 {
font-weight: lighter;
}
.paragrahp4 {
font-weight: 600;
}
style>
head>
<body>
<div id="wrap">
<p class="paragrahp1">正常p>
<p class="paragrahp2">加粗p>
<p class="paragrahp3">变细p>
<p class="paragrahp4">关键字p>
div>
body>
html>
使用方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>字体倾斜title>
<style type="text/css">
#wrap {
font-size: 20px;
}
.paragrahp1 {
font-style: normal;
}
.paragrahp2 {
font-style: italic;
}
.paragrahp3 {
font-style: oblique;
}
style>
head>
<body>
<div id="wrap">
<p class="paragrahp1">文字正常p>
<p class="paragrahp2">文字斜体p>
<p class="paragrahp3">文字倾斜p>
div>
body>
html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>字体行高title>
<style type="text/css">
#wrap {
font-size: 20px;
line-height: 30px;
}
style>
head>
<body>
<div id="wrap">
<p class="paragrahp">abcd123p>
<p class="paragrahp">abcd123p>
div>
body>
html>
注意:使用小型大写字体的字母与其余文本相比,其字体尺寸更小。
使用方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>小型字母大写title>
<style type="text/css">
#wrap {
font-variant: small-caps;
}
style>
head>
<body>
<div id="wrap">
<p class="paragrahp">abcd123THZEp>
div>
body>
html>
使用方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文本的大小写title>
<style type="text/css">
#wrap {
font-size: 20px;
line-height: 20px;
}
.paragrahp1 {
text-transform: uppercase;
}
.paragrahp2 {
text-transform: lowercase;
}
.paragrahp3 {
text-transform: capitalize;
}
style>
head>
<body>
<div id="wrap">
<p class="paragrahp1">字母大写:I am studentp>
<p class="paragrahp2">字母小写:I am studentp>
<p class="paragrahp3">首字母大写:I am studentp>
div>
body>
html>
使用方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>对齐方式title>
<style type="text/css">
#wrap {
font-size: 20px;
line-height: 20px;
}
.paragrahp1 {
width: 200px;
text-align: left;
margin: 10px;
float: left;
}
.paragrahp2 {
width: 200px;
text-align: center;
margin: 10px;
float: left;
}
.paragrahp3 {
width: 200px;
text-align: right;
margin: 10px;
float: left;
}
.paragrahp4 {
width: 200px;
text-align: justify;
margin: 10px;
float: left;
}
style>
head>
<body>
<div id="wrap">
<p class="paragrahp1">左对齐:font:字体。在微机系统里通常用“fonts”文件夹存放已安装的字体,自己安装字体时,也需要装入“fonts”文件夹中。p>
<p class="paragrahp2">居中对齐:font:字体。在微机系统里通常用“fonts”文件夹存放已安装的字体,自己安装字体时,也需要装入“fonts”文件夹中。p>
<p class="paragrahp3">右对齐:font:字体。在微机系统里通常用“fonts”文件夹存放已安装的字体,自己安装字体时,也需要装入“fonts”文件夹中。p>
<p class="paragrahp3">两端对齐:font:字体。在微机系统里通常用“fonts”文件夹存放已安装的字体,自己安装字体时,也需要装入“fonts”文件夹中。p>
div>
body>
html>
使用方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首行缩进title>
<style type="text/css">
#wrap {
font-size: 20px;
text-indent: 2em;
width: 300px;
text-align: justify;
}
style>
head>
<body>
<div id="wrap">
<p class="paragrahp1">在微机系统里通常用“fonts”文件夹存放已安装的字体,自己安装字体时,也需要装入“fonts”文件夹中。p>
div>
body>
html>
使用方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> 文本修饰title>
<style type="text/css">
#wrap {
font-size: 20px;
}
.paragrahp1 {
text-decoration: underline;
}
.paragrahp2 {
text-decoration: line-through;
}
.paragrahp3 {
text-decoration: overline;
}
style>
head>
<body>
<div id="wrap">
<p class="paragrahp1">下划线p>
<p class="paragrahp2">中划线p>
<p class="paragrahp3">上划线p>
div>
body>
html>
使用方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>间距title>
<style type="text/css">
#wrap {
font-size: 20px;
}
.paragrahp1 {
letter-spacing: 20px;
}
.paragrahp2 {
word-spacing: 20px;
}
style>
head>
<body>
<div id="wrap">
<p class="paragrahp1">字间距:I am studentp>
<p class="paragrahp2">词间距:I am studentp>
div>
body>
html>
word-break: break-all; 强制到达边框换行
使用方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首行缩进title>
<style type="text/css">
#wrap {
font-size: 20px;
width: 100px;
}
.paragrahp1 {
border: 5px solid red;
margin: 10px;
width: 100px;
white-space: normal;
}
.paragrahp2 {
border: 5px solid red;
margin: 10px;
width: 100px;
white-space: nowrap;
}
.paragrahp3 {
border: 5px solid red;
margin: 10px;
width: 100px;
word-break: break-all;
}
style>
head>
<body>
<div id="wrap">
<div class="paragrahp1">AAAAAAAAAAAAAAAAAAAAAAAAdiv>
<div class="paragrahp2">BBBBBBBBBBBBbr>BBBBBBBBBBBBdiv>
<div class="paragrahp3">CCCCCCCCCCCCCCCCCCCCCCCCdiv>
div>
body>
html>
text-overflow:ellipsis 被修剪的文本用略符号来代表(…)。
使用方法:
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>首行缩进title>
<style type="text/css">
#wrap1 {
font-size: 20px;
width: 100px;
}
.paragrahp1 {
border: 5px solid red;
margin: 10px;
width: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
#wrap2 {
border: 5px solid red;
margin: 10px;
font-size: 20px;
width: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
/* 数字代表几行 */
-webkit-line-clamp: 2;
}
style>
head>
<body>
<div id="wrap1">
<p class="paragrahp1">AAAAAAAAAAAAAAAAAAAAAAAAp>
div>
<div id="wrap2">
<p class="paragrah2">BBBBBBBBBBBBBBBBBBBBBBBBBp>
<p class="paragrah3">CCCCCCCCCCCCCCCCCCCCCCCCCp>
<p class="paragrah4">DDDDDDDDDDDDDDDDDDDDDDDDDp>
div>
body>
html>