HTML 和 Body 在 CSS 中的区别
HTML 和 Body 如何关联
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Metadata and such -->
</head>
<body>
<!-- Where the content begins -->
<body>
</html>
根据 标准定义 , <html>
是文档的根元素, <head>
、 <body>
是 <html>
唯一的两个子元素。按照 规范 , <head>
才是和 <body>
相对照、需要加以区别的元素。
因此, <html>
和 <body>
是父子关系。在 HTML 文档中, :root
选择符 对应 <html>
元素。
:root {
}
html {
}
需要注意的是, :root
选择符(伪类)的 优先级 大于 html
选择符 : (0, 0, 1, 0)
vs (0, 0, 0, 1)
。
哪些全局样式应该应用在 HTML
使用 rem
时
html {
font-size: 62.5%;
}
body {
font-size: 1.4rem; /* =14px */
}
h1 {
font-size: 2.4rem; /* =24px */
}
古怪的 background-color
CSS 中有一些古怪的行为 ,将 background-color
应用到 <body>
以后,即便 <body>
里的元素没有占满视口,背景颜色也会 蔓延到整个视口 。
给 html
设置 background-color
可以解决这个问题。
height: 100%
如果 <body>
及其子元素的高度需要设置为窗口高度时, <html>
元素上也需要添加:
html,
body {
height: 100%;
}
哪些全局样式应该应用在 Body
早期的 规范中 , <body>
有以下行内属性:
- background
- bgcolor
- marginbottom
- marginleft
- marginright
- margintop
- text
这些行内属性对应的 CSS 样式应该应用在 <body>
。
Inline Attribute | CSS Property |
background | background |
bgcolor | background background-color |
marginbottom | margin-bottom |
marginleft | margin-left |
marginright | margin-right |
margintop | margin-top |
text | font |
总结
本文列举了一些 <html>
和 <body>
在 CSS 中的区别,在 JavaScript 中同样存在区别,例如 html
对应 document.documentElement
、 body
对应 document.body
。
分类:
html5、css3
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 手把手教你更优雅的享受 DeepSeek
· AI工具推荐:领先的开源 AI 代码助手——Continue
· 探秘Transformer系列之(2)---总体架构
· V-Control:一个基于 .NET MAUI 的开箱即用的UI组件库
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现