CSS Font-Size: em、 px 、pt 、Percent

本文详细介绍了CSS中的字体单位,包括em、px、pt和%,并探讨了它们之间的换算关系及在不同设备上的表现效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、基础介绍

1、“Ems”: em,大小不固定 ,成为相对单位(body则相对浏览器的默认字体设置,子集相对父级), 浏览器默认设置字体大小为16px , 则1em = 16px , 且其可扩展,2em = 32px , 目前常用的字体大小px换算成em ,

16px = 1em; 14px = 0.875em; 12px = 0.75em; 10px = 0.625em

2、“Pixels”: px,大小是固定的,称为绝对单位,在移动端的可访问性差

3、“Points”:pt,大小固定,属于绝对单位,适用于印刷、打印媒体。

4、“Percent”: %,跟em相似,以percent来表示,则当前字体的大小为100% ,使用% 设置字体,你的页面字体在移动设备端的可访问性也很好。


二、关系


一般情况下,1em=12pt=16px=100% . 下面例子前提在body中设置基础字体大小。


由上图可看出,相对单位em 和 % 会随着基础字体大小的变化而变化,而pt 和 px 不会变化,这就是为什么选择em 和 % 设置web文档文本的字体(其在移动端的访问性也很好)。


三、em 与 % ,em与px 的换算

em的特点:

1. em的值并不是固定的; 
2. em会继承父级元素的字体大小。

重写步骤: 

1. body选择器中声明Font-size:62.5%; 
2. 将你的原来的px数值除以10,然后换上em作为单位; 

如果只需要以上两步就能解决问题的话,可能就没人用px了。经过以上两步,你会发现你的网站字体大得出乎想象。因为em的值不固定,又会继承父级 元素的大小,你可能会在content这个div里把字体大小设为1.2em, 也就是12px。然后你又把选择器p的字体大小也设为1.2em,但如果p属于content的子级的话,p的字体大小就不是12px,而是1.2em= 1.2 * 12px=14.4px。这是因为content的字体大小被设为1.2em,这个em值继承其父级元素body的大小,也就是16px * 62.5% * 1.2=12px, 而p作为其子级,em则继承content的字体高,也就是12px。所以p的1.2em就不再是12px,而是14.4px。 

3. 重新计算那些被放大的字体的em数值。避免字体大小的重复声明,也就是避免以上提到的1.2 * 1.2= 1.44的现象。比如说你在#main中声明了字体大小为1.2em,那么在声明p的字体大小时就只能是1em,而不是1.2em, 因为此em非彼em,它因继承#content的字体高而变为了1em=12px。 

 

诡异的12px汉字 


在完成em转换时还会发现一个诡异的现象,就是由以上方法得到的12px(1.2em)大小的汉字在IE中并不等于直接用12px定义的字体大小,而 是稍大一点。这个问题我已经解决,你只需在body选择器中把62.5%换成63%就能正常显示了。

 

参考:

1. http://kyleschaeffer.com/development/css-font-size-em-vs-px-vs-pt-vs/

2. http://www.jb51.net/css/104772.html

<template> <el-dialog title="设备导入" :visible.sync="dialogVisible" width="600px" :close-on-click-modal="false" > <div class="dialog-content"> <!-- 上传区域 --> <div class="upload-section"> <el-upload action="/api/upload" :on-success="handleUploadSuccess" :before-upload="beforeUpload" :limit="5" :on-exceed="handleExceed" :file-list="fileList" multiple drag > <i class="el-icon-upload"></i> <div class="el-upload__text"> <div>点击或拖拽文件到此处上传</div> <div class="el-upload__tip">支持.xlsx、.xls格式文件(最多5个)</div> </div> </el-upload> </div> <!-- 文件列表区域 - 重点突出 --> <div class="file-list-section" v-if="fileList.length"> <div class="section-title"> <i class="el-icon-folder-opened"></i> <span>已上传文件列表</span> <el-tag type="success" size="small">{{ fileList.length }}个文件</el-tag> </div> <div class="file-list"> <div v-for="(file, index) in fileList" :key="index" class="file-item" > <div class="file-icon"> <i class="el-icon-document"></i> </div> <div class="file-info"> <div class="file-name">{{ file.name }}</div> <div class="file-details"> <span class="file-size">{{ formatFileSize(file.size) }}</span> <span class="file-status" :class="file.status">{{ file.status || '已上传' }}</span> </div> </div> <div class="file-actions"> <el-button icon="el-icon-delete" circle size="mini" @click="removeFile(index)" ></el-button> </div> </div> </div> </div> <!-- 空状态提示 --> <div v-else class="empty-state"> <i class="el-icon-folder-opened"></i> <p>尚未上传任何文件</p> </div> </div> <!-- 底部按钮 --> <div slot="footer" class="dialog-footer"> <el-button @click="dialogVisible = false">取 消</el-button> <el-button type="primary" @click="handleSubmit" :disabled="!fileList.length" > 开始导入 ({{ fileList.length }}) </el-button> </div> </el-dialog> </template> <script> export default { data() { return { dialogVisible: false, fileList: [] }; }, methods: { init() { this.dialogVisible = true; this.fileList = []; }, handleSubmit() { // 提交逻辑 console.log('提交文件列表:', this.fileList); this.$message.success(`已提交 ${this.fileList.length} 个文件`); }, handleUploadSuccess(response, file) { // 添加上传状态 file.status = '上传成功'; this.$message.success(`${file.name} 上传成功`); }, beforeUpload(file) { // 文件类型验证 const isValidType = [ 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel' ].includes(file.type); if (!isValidType) { this.$message.error('请上传Excel格式的文件 (.xlsx 或 .xls)'); return false; } // // 文件大小限制 (20MB) // const isLt20M = file.size / 1024 / 1024 < 20; // if (!isLt20M) { // this.$message.error('文件大小不能超过20MB'); // return false; // } // 添加初始状态 file.status = '等待上传'; return true; }, handleExceed(files, fileList) { this.$message.warning(`最多只能上传5个文件,您已选择${fileList.length}个文件`); }, formatFileSize(size) { if (size < 1024) return size + ' B'; if (size < 1024 * 1024) return (size / 1024).toFixed(1) + ' KB'; return (size / (1024 * 1024)).toFixed(1) + ' MB'; }, // 删除文件 removeFile(index) { const removedFile = this.fileList.splice(index, 1)[0]; this.$message.info(`已移除: ${removedFile.name}`); } } }; </script> <style scoped> .dialog-content { padding: 20px; } /* 上传区域 */ .upload-section { margin-bottom: 25px; position: relative; display: flex; justify-content: center; } /* 文件列表区域 - 重点突出 */ .file-list-section { border: 1px solid #ebeef5; border-radius: 8px; padding: 15px; background-color: #f8fafc; margin-top: 20px; box-shadow: 0 2px 8px rgba(0,0,0,0.05); } .section-title { display: flex; align-items: center; margin-bottom: 15px; font-size: 16px; font-weight: 500; color: #409EFF; } .section-title i { margin-right: 8px; font-size: 18px; } .section-title .el-tag { margin-left: 10px; } /* 文件列表 */ .file-list { max-height: 300px; overflow-y: auto; padding-right: 5px; } .file-item { display: flex; align-items: center; padding: 12px; background-color: white; border-radius: 6px; margin-bottom: 10px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); transition: all 0.3s; } .file-item:hover { background-color: #f0f7ff; transform: translateY(-2px); box-shadow: 0 4px 8px rgba(0,0,0,0.08); } .file-icon { margin-right: 15px; } .file-icon i { font-size: 28px; color: #409EFF; } .file-info { flex: 1; min-width: 0; /* 防止内容溢出 */ } .file-name { font-weight: 500; margin-bottom: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .file-details { display: flex; font-size: 12px; color: #909399; } .file-size { margin-right: 15px; } .file-status { font-weight: 500; } .file-status.上传成功 { color: #67C23A; } .file-status.等待上传 { color: #E6A23C; } .file-actions { margin-left: 10px; } /* 空状态 */ .empty-state { text-align: center; padding: 30px 0; color: #909399; border: 1px dashed #ebeef5; border-radius: 8px; margin-top: 20px; background-color: #f8fafc; } .empty-state i { font-size: 48px; margin-bottom: 15px; display: block; color: #dcdfe6; } .empty-state p { margin: 0; font-size: 14px; } /* 底部按钮 */ .dialog-footer { display: flex; justify-content: flex-end; padding: 15px 20px; border-top: 1px solid #ebeef5; } </style> 未正常显示上传文件的列表
07-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值