- CSS 教程
- CSS 简介
- CSS 选择器
- CSS 默认值
- CSS 注释
- CSS 字体
- CSS 字号
- CSS 单位
- CSS 颜色
- CSS 背景
- CSS 高级教程
- CSS 动画
- CSS 伪类
- CSS 伪元素
- CSS 计数器
- CSS 特异位
- CSS 清除浮动
- CSS 图像精灵
- CSS 性能优化
- CSS 属性
- CSS align-content
- CSS align-items
- CSS align-self
- CSS all
- CSS animation
- CSS animation-delay
- CSS animation-direction
- CSS animation-duration
- CSS animation-fill-mode
- CSS animation-iteration-count
- CSS animation-name
- CSS animation-play-state
- CSS animation-timing-function
- CSS backface-visibility
- CSS background
- CSS background-attachment
- CSS background-blend-mode
- CSS background-clip
- CSS background-color
- CSS background-image
- CSS background-origin
- CSS background-position
- CSS background-repeat
- CSS background-size
- CSS border
- CSS border-bottom
- CSS border-bottom-color
- CSS border-bottom-left-radius
- CSS border-bottom-right-radius
- CSS border-bottom-style
- CSS border-bottom-width
- CSS border-collapse
- CSS border-color
- CSS border-image
- CSS border-image-outset
- CSS border-image-repeat
- CSS border-image-slice
- CSS border-image-source
- CSS border-image-width
- CSS border-left
- CSS border-left-color
- CSS border-left-style
- CSS border-left-width
- CSS border-radius
- CSS border-right
- CSS border-right-color
- CSS border-right-style
- CSS border-right-width
- CSS border-spacing
- CSS border-style
- CSS border-top
- CSS border-top-color
- CSS border-top-left-radius
- CSS border-top-right-radius
- CSS border-top-style
- CSS border-top-width
- CSS border-width
- CSS bottom
- CSS box-shadow
- CSS box-sizing
- CSS caption-side
- CSS caret-color
- CSS @charset
- CSS clear
- CSS clip
- CSS clip-path
- CSS color
- CSS column-count
- CSS column-fill
- CSS column-gap
- CSS column-rule
- CSS column-rule-color
- CSS column-rule-style
- CSS column-rule-width
- CSS column-span
- CSS column-width
- CSS columns
- CSS content
- CSS counter-increment
- CSS counter-reset
- CSS cursor
- CSS direction
- CSS display
- CSS empty-cells
- CSS filter
- CSS flex
- CSS flex-basis
- CSS flex-direction
- CSS flex-flow
- CSS flex-grow
- CSS flex-shrink
- CSS flex-wrap
- CSS float
- CSS font
- CSS @font-face
- CSS font-family
- CSS font-kerning
- CSS font-size
- CSS font-size-adjust
- CSS font-stretch
- CSS font-style
- CSS font-variant
- CSS font-weight
- CSS grid
- CSS grid-area
- CSS grid-auto-columns
- CSS grid-auto-flow
- CSS grid-auto-rows
- CSS grid-column
- CSS grid-column-end
- CSS grid-column-gap
- CSS grid-column-start
- CSS grid-gap
- CSS grid-row
- CSS grid-row-end
- CSS grid-row-gap
- CSS grid-row-start
- CSS grid-template
- CSS grid-template-areas
- CSS grid-template-columns
- CSS grid-template-rows
- CSS height
- CSS hyphens
- CSS @import
- CSS justify-content
- CSS @keyframes
- CSS left
- CSS letter-spacing
- CSS line-height
- CSS list-style
- CSS list-style-image
- CSS list-style-position
- CSS list-style-type
- CSS margin
- CSS margin-bottom
- CSS margin-left
- CSS margin-right
- CSS margin-top
- CSS max-height
- CSS max-width
- CSS @media
- CSS min-height
- CSS min-width
- CSS object-fit
- CSS object-position
- CSS opacity
- CSS order
- CSS outline
- CSS outline-color
- CSS outline-offset
- CSS outline-style
- CSS outline-width
- CSS overflow
- CSS overflow-x
- CSS overflow-y
- CSS padding
- CSS padding-bottom
- CSS padding-left
- CSS padding-right
- CSS padding-top
- CSS visibility
- CSS white-space
- CSS width
- CSS word-break
- CSS word-spacing
- CSS word-wrap
- CSS writing-mode
- CSS z-index
- CSS 参考手册
CSS 字号
在 CSS 中,设置文本的字号(字体大小)是通过 font-size
属性来实现的。这个属性用于指定文本字符的大小,可以使用多种单位来表示大小,常见的单位包括像素(px)、百分比(%)、em(相对于父元素的字体大小)、rem(相对于根元素的字体大小)等。
常见的 font-size 单位和用法:
像素单位(px):
相对长度单位,固定大小,不受父元素字体大小的影响。
示例:
p { font-size: 16px; }
百分比单位(%):
相对于父元素的字体大小,例如,如果父元素的字体大小为14px,font-size: 120% 将等同于 font-size: 16.8px。
示例:
p { font-size: 120%; }
相对单位(em 和 rem):
em 是相对于父元素的字体大小,rem 是相对于根元素(html元素)的字体大小。
示例:
p { font-size: 1.2em; /* 相对于父元素字体大小的1.2倍 */ } h1 { font-size: 2rem; /* 相对于根元素字体大小的2倍 */ }
其他单位:
vw(视窗宽度的百分比)和 vh(视窗高度的百分比)等相对于视窗大小的单位也可以用于字号设置,但使用不如前面提到的单位普遍。
常见的字号选择建议:
对于主体文本,通常推荐使用相对单位(如em或rem),这样可以确保在不同设备上保持一致的相对大小。
对于标题和重要信息,通常使用固定大小(如像素单位),以确保其在各种屏幕尺寸下都能够达到预期的视觉效果。
使用百分比单位可以帮助实现响应式设计,使得文本能够根据父元素的字体大小进行自适应调整。
综上所述,font-size 是控制文本字号的核心CSS属性,使用不同的单位可以满足不同的设计需求和响应式布局要求。