- CSS 教程
- CSS - 首页
- CSS - 路线图
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 包含
- CSS - 测量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图片
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - 块级边框
- CSS - 行内边框
- CSS - 外边距
- CSS - 列表
- CSS - 内边距
- CSS - 光标
- CSS - 轮廓
- CSS - 尺寸
- CSS - 滚动条
- CSS - 行内块
- CSS - 下拉菜单
- CSS - 可见性
- CSS - 溢出
- CSS - 清除浮动
- CSS - 浮动
- CSS - 箭头
- CSS - 调整大小
- CSS - 引号
- CSS - 顺序
- CSS - 定位
- CSS - 连字符
- CSS - 悬停
- CSS - 显示
- CSS - 焦点
- CSS - 缩放
- CSS - 位移
- CSS - 高度
- CSS - 连字符字符
- CSS - 宽度
- CSS - 不透明度
- CSS - Z-index
- CSS - 底部
- CSS - 导航栏
- CSS - 覆盖层
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - 属性选择器
- CSS - 组合器
- CSS - 根元素
- CSS - 盒模型
- CSS - 计数器
- CSS - 剪裁
- CSS - 书写模式
- CSS - Unicode-bidi
- CSS - min-content
- CSS - all
- CSS - inset
- CSS - isolation
- CSS - overscroll
- CSS - justify-items
- CSS - justify-self
- CSS - tab-size
- CSS - pointer-events
- CSS - place-content
- CSS - place-items
- CSS - place-self
- CSS - max-block-size
- CSS - min-block-size
- CSS - mix-blend-mode
- CSS - max-inline-size
- CSS - min-inline-size
- CSS - offset
- CSS - accent-color
- CSS - user-select
- CSS 高级
- CSS - 网格
- CSS - 网格布局
- CSS - Flexbox
- CSS - 可见性
- CSS - 定位
- CSS - 层
- CSS - 伪类
- CSS - 伪元素
- CSS - @规则
- CSS - 文字效果
- CSS - 分页媒体
- CSS - 打印
- CSS - 布局
- CSS - 验证
- CSS - 图片精灵
- CSS - !important
- CSS - 数据类型
- CSS3 教程
- CSS3 - 教程
- CSS - 圆角
- CSS - 边框图片
- CSS - 多重背景
- CSS - 颜色
- CSS - 渐变
- CSS - 盒阴影
- CSS - box-decoration-break
- CSS - caret-color
- CSS - 文本阴影
- CSS - 文本
- CSS - 2D 变换
- CSS - 3D 变换
- CSS - 过渡
- CSS - 动画
- CSS - 多列
- CSS - box-sizing
- CSS - 工具提示
- CSS - 按钮
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 函数
- CSS - 数学函数
- CSS - 遮罩
- CSS - 形状
- CSS - 样式图片
- CSS - 特效性
- CSS - 自定义属性
- CSS 响应式
- CSS RWD - 简介
- CSS RWD - 视口
- CSS RWD - 网格视图
- CSS RWD - 媒体查询
- CSS RWD - 图片
- CSS RWD - 视频
- CSS RWD - 框架
- CSS 工具
- CSS - PX 到 EM 转换器
- CSS - 颜色选择器和动画
- CSS 资源
- CSS - 有用资源
- CSS - 讨论
CSS 数据类型 - <percentage>
CSS 的<percentage>数据类型表示另一个值的比例,通常相对于父元素或其他参考值。百分比经常用于各种 CSS 属性(例如font-size、width、height、margin和padding)来指定尺寸、间距或其他比例值。
语法
<number>%
一个<number>和百分号 (%) 构成<percentage>数据类型。可选地在前面添加一个 + 或 - 符号,但并非所有属性都允许负值。与其他尺寸类型不同,CSS 尺寸在符号和数字之间没有空格。
CSS <percentage> - 字体大小
以下示例演示了使用<percentage>数据类型来设置字体大小。
<html>
<head>
<style>
body, html {
margin: 0;
padding: 0;
}
.container {
width: 90%; /* Set container width to 90% of the viewport width */
background-color: #f0f0f0;
margin: 40px auto;
}
.text {
font-size: 100%; /* Default font size (16px) */
text-align: center;
margin: 20px 0;
}
.text-smaller {
font-size: 80%; /* Font size 80% of the default size */
color: red;
}
.text-larger {
font-size: 120%; /* Font size 120% of the default size */
color: blue;
}
</style>
</head>
<body>
<div class="container">
<p class="text">Font Size with Percentage</p>
<p class="text text-smaller">Smaller Font Size</p>
<p class="text text-larger">Larger Font Size</p>
</div>
</body>
</html>
CSS <percentage> - 盒子大小
以下示例演示了使用<percentage>数据类型来改变容器和盒子的尺寸。
<html>
<head>
<style>
body, html {
margin: 0;
padding: 0;
}
.container {
width: 70%; /* Set container width to 70% of the viewport width */
margin: 20px auto;
background-color: #f4f4f4;
padding: 20px;
}
.box {
width: 40%; /* Box width set to 40% of its container */
height: 200px;
background-color: #3498db;
color: white;
text-align: center;
line-height: 200px;
margin: 0 auto 20px;
}
.box:nth-child(2) {
width: 60%; /* Box width set to 60% of its container */
height: 160px;
background-color: #e74c3c;
line-height: 160px;
}
</style>
</head>
<body>
<div class="container">
<div class="box">
<p>Box 1 - 40% of container width</p>
</div>
<div class="box">
<p>Box 2 - 60% of container width</p>
</div>
</div>
</body>
</html>
广告