HTML 和 Body 在 CSS 中的区别

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        
backgroundbackground        
bgcolorbackground           background-color        
marginbottommargin-bottom        
marginleftmargin-left        
marginrightmargin-right        
margintopmargin-top        
textfont        

总结

本文列举了一些    <html> 和    <body> 在 CSS 中的区别,在 JavaScript 中同样存在区别,例如 html 对应    document.documentElement 、    body 对应    document.body

转载于:https://www.cnblogs.com/smile1313113/p/5718617.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值